一、获取安装程序信息

 #include <windows.h>
#include <iostream>
#include <string>
#include <vector> using namespace std; //记录安装软件信息的结构体
struct ApplicationInfoA
{
string strName; //软件名
string strDisplayName; //显示的软件名
string strPublisher; //发布者
string strVersion; //版本
string strDisPlayVersion; //显示的版本
string strInstallLocation; //安装的位置
}; int GetAppInfoA(HKEY hKey, LPCSTR lpszAppName, LPCSTR lpszKeyValueName, string &strKeyValue)
{
int iRet;
HKEY hInstallAppKey; iRet = RegOpenKeyExA(hKey, lpszAppName, , KEY_ALL_ACCESS, &hInstallAppKey);
if (iRet != ERROR_SUCCESS)
{
return -;
} DWORD dwKeyValueType = REG_SZ;
DWORD dwKeyValueDataSize = ; iRet = RegQueryValueExA(hInstallAppKey, lpszKeyValueName, NULL, &dwKeyValueType, NULL, &dwKeyValueDataSize);
if (iRet == ERROR_FILE_NOT_FOUND)
{
RegCloseKey(hInstallAppKey);
return ;
}
else if (iRet != ERROR_SUCCESS)
{
RegCloseKey(hInstallAppKey);
return -;
} if (dwKeyValueType != REG_SZ)
{
RegCloseKey(hInstallAppKey);
return ;
} LPSTR lpszKeyValueData = new char[dwKeyValueDataSize + ];
memset(lpszKeyValueData, , dwKeyValueDataSize + ); iRet = RegQueryValueExA(hInstallAppKey, lpszKeyValueName, NULL, &dwKeyValueType, (LPBYTE)lpszKeyValueData, &dwKeyValueDataSize);
if (iRet != ERROR_SUCCESS)
{
delete []lpszKeyValueData;
RegCloseKey(hInstallAppKey);
return -;
} strKeyValue = lpszKeyValueData;
delete []lpszKeyValueData;
RegCloseKey(hInstallAppKey); return ;
} int GetAllInstalledAppInfoA(vector<ApplicationInfoA> &vecAppInfo)
{
int iRet;
HKEY hKey;
LPCSTR lpszSubKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
iRet = RegOpenKeyExA(HKEY_LOCAL_MACHINE, lpszSubKey, , KEY_ALL_ACCESS, &hKey);
if (iRet != ERROR_SUCCESS)
{
return -;
} DWORD dwSubKeysCnt;
DWORD dwMaxSubKeyNameLen;
DWORD dwKeyValueCnt;
DWORD dwMaxKeyValueNameLen;
DWORD dwMaxKeyValueDataLen; iRet = RegQueryInfoKeyA(
hKey,
NULL,
NULL,
NULL,
&dwSubKeysCnt,
&dwMaxSubKeyNameLen,
NULL,
&dwKeyValueCnt,
&dwMaxKeyValueNameLen,
&dwMaxKeyValueDataLen,
NULL,
NULL);
if (iRet != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return -;
} DWORD dwIndex;
DWORD dwNameLen = dwMaxSubKeyNameLen + ;
LPSTR lpszSubKeyName = new char[dwNameLen]; for (dwIndex = ; dwIndex < dwSubKeysCnt; ++dwIndex)
{
dwNameLen = dwMaxSubKeyNameLen + ;
memset(lpszSubKeyName, , dwNameLen); iRet = RegEnumKeyExA(hKey, dwIndex, lpszSubKeyName, &dwNameLen, NULL, NULL, NULL, NULL);
if (iRet != ERROR_SUCCESS)
{
RegCloseKey(hKey);
delete []lpszSubKeyName;
return -;
} ApplicationInfoA appInfo;
appInfo.strName = lpszSubKeyName;
GetAppInfoA(hKey, lpszSubKeyName, "DisplayName", appInfo.strDisplayName);
GetAppInfoA(hKey, lpszSubKeyName, "Publisher", appInfo.strPublisher);
GetAppInfoA(hKey, lpszSubKeyName, "Version", appInfo.strVersion);
GetAppInfoA(hKey, lpszSubKeyName, "DisplayVersion", appInfo.strDisPlayVersion);
GetAppInfoA(hKey, lpszSubKeyName, "InstallLocation", appInfo.strInstallLocation);
vecAppInfo.push_back(appInfo);
} delete []lpszSubKeyName;
RegCloseKey(hKey); return ;
} int main(int argc, char **argv)
{
cout << "Application Information" << endl; vector<ApplicationInfoA> vecAppInfo;
cout << GetAllInstalledAppInfoA(vecAppInfo) << endl; for (vector<ApplicationInfoA>::const_iterator iter = vecAppInfo.begin(); iter != vecAppInfo.end(); ++iter)
{
cout << "-------------------------------------------------------------------------------" << endl;
cout << "Name:" << iter->strName << endl;
cout << "DisplayName:" << iter->strDisplayName << endl;
cout << "Publisher:" << iter->strPublisher << endl;
cout << "Version:" << iter->strVersion << endl;
cout << "DisplayVersion:" << iter->strDisPlayVersion << endl;
cout << "InstallLocation:" << iter->strInstallLocation << endl;
cout << "-------------------------------------------------------------------------------" << endl;
cout << endl;
cout << endl;
} return ;
}

二、添加环境变量

 #define _CRT_SECURE_NO_WARNINGS

 #include <windows.h>
#include <iostream>
#include <string> using namespace std; int AddPathEnvValue(LPCSTR lpszPathValue)
{
int iRet;
HKEY hKey;
LPCSTR lpszSubKey = "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"; iRet = RegOpenKeyExA(HKEY_LOCAL_MACHINE, lpszSubKey, , KEY_ALL_ACCESS, &hKey);
if (ERROR_SUCCESS != iRet)
{
cout << "RegOpenKeyExA Error!" << endl;
return -;
} LPCSTR lpszKeyValueName = "Path";
DWORD dwKeyValueType = REG_EXPAND_SZ;
DWORD dwKeyValueDataSize = ; iRet = RegQueryValueExA(hKey, lpszKeyValueName, NULL, &dwKeyValueType, NULL, &dwKeyValueDataSize);
if (ERROR_FILE_NOT_FOUND != iRet)
{
iRet = RegSetValueExA(hKey, lpszKeyValueName, , REG_EXPAND_SZ, (const BYTE*)"", );
if (ERROR_SUCCESS != iRet)
{
cout << "RegSetValueExA Error!" << endl;
RegCloseKey(hKey);
return -;
}
}
else if (ERROR_SUCCESS != iRet)
{
cout << "RegSetValueExA Error!" << endl;
RegCloseKey(hKey);
return -;
}
else if (REG_EXPAND_SZ != dwKeyValueType)
{
cout << "It is impossible!" << endl;
cout << dwKeyValueType << endl;
RegCloseKey(hKey);
return -;
} char *lpszKeyValueData = new char[dwKeyValueDataSize + ];
memset(lpszKeyValueData, , dwKeyValueDataSize + ); iRet = RegQueryValueExA(hKey, lpszKeyValueName, NULL, &dwKeyValueType, (LPBYTE)lpszKeyValueData, &dwKeyValueDataSize);
if (ERROR_SUCCESS != iRet)
{
cout << "RegQueryValueExA Error!" << endl;
RegCloseKey(hKey);
delete []lpszKeyValueData;
return -;
} unsigned int iLen = strlen(lpszPathValue);
iLen += strlen(lpszKeyValueData);
char *lpszKeyValueData_New = new char[iLen + ];
memset(lpszKeyValueData_New, , iLen + );
sprintf(lpszKeyValueData_New, "%s%s", lpszKeyValueData, lpszPathValue);
iRet = RegSetValueExA(hKey, lpszKeyValueName, , REG_EXPAND_SZ, (const BYTE*)lpszKeyValueData_New, iLen + );
if (ERROR_SUCCESS != iRet)
{
cout << "RegSetValueExA Error!" << endl;
RegCloseKey(hKey);
delete []lpszKeyValueData;
delete []lpszKeyValueData_New;
return -;
} delete []lpszKeyValueData_New;
delete []lpszKeyValueData; RegCloseKey(hKey); return ;
} int main(int argc, char *argv)
{
cout << AddPathEnvValue(";HelloKitty") << endl;
return ;
}

注册表Demo的更多相关文章

  1. form注册表单圆角 demo

    form注册表单圆角 <BODY> <div class="form"> <ul class="list"> <li& ...

  2. 弥补学生时代的遗憾~C#注册表情缘

    记得当时刚接触C#的时候,喜欢编写各种小软件,而注册表系列和网络系列被当时的我认为大牛的必备技能.直到我研究注册表前一天我都感觉他是那么的高深. 今天正好有空,于是就研究了下注册表系列的操作,也随手封 ...

  3. delphi 注册表操作(读取、添加、删除、修改)完全手册

    DELPHI VS PASCAL(87)  32位Delphi程序中可利用TRegistry对象来存取注册表文件中的信息. 一.创建和释放TRegistry对象 1.创建TRegistry对象.为了操 ...

  4. C#注册表

    C#注册表情缘   记得当时刚接触C#的时候,喜欢编写各种小软件,而注册表系列和网络系列被当时的我认为大牛的必备技能.直到我研究注册表前一天我都感觉他是那么的高深. 今天正好有空,于是就研究了下注册表 ...

  5. Delphi的注册表操作

    转帖:Delphi的注册表操作 2009-12-21 11:12:52 分类: Delphi的注册表操作 32位Delphi程序中可利用TRegistry对象来存取注册表文件中的信息.     一.创 ...

  6. Eclipse出错不断,注册表不能乱改

    Eclipse打不开,始终报错,还能不能开心的敲代码了??? 首先说下造成我这个愚蠢错误的起源:电脑是win10系统,本来是可以正常使用的.某一天,我正在使用python,打开命令提示符,看见开头是中 ...

  7. 入侵检测中需要监控的注册表路径研究(Windows Registry Security Check)

    1. Windows注册表简介 注册表(Registry,繁体中文版Windows称之为登录档)是Microsoft Windows中的一个重要的数据库,用于存储系统和应用程序的设置信息.早在Wind ...

  8. 【干货】从windows注册表读取重要信息-----这种技能非常重要,占电子取证的70%

    也就是说,当我拿着U盘启动盘,从你电脑里面拷贝了注册表的几个文件,大部分数据就已经到我手中了.一起来感受一下吧. 来源:Unit 6: Windows File Systems and Registr ...

  9. 基于HTML5手机登录注册表单代码

    分享一款基于HTML5手机登录注册表单代码.这是一款鼠标点击注册登录按钮弹出表单,适合移动端使用.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class=&qu ...

随机推荐

  1. VS2017 Linux 上.NET Core调试

    调试Linux 上.NET Core Visual Studio 2017 通过SSH 调试Linux 上.NET Core 应用程序. 本文环境 开发环境:Win10 x64 Visual Stud ...

  2. python 垃圾回收装置

    转载: https://www.cnblogs.com/pinganzi/p/6646742.html 简要描述Python的垃圾回收机制(garbage collection). 答案 这里能说的很 ...

  3. c语言学习笔记(7)——数组

    一.为什么需要数组1.为了解决大量同类型的数据存储和使用2.为了模拟现实世界二.数组的分类1.一维数组为n个变量连续分配存储空间所有的变量数据类型必须相同所有变量所占的字节大小必须相等初始化:完全初始 ...

  4. Windows搭建Eclipse+JDK+SDK的Android --安卓开发入门级

     一 相关下载 (1) java JDK下载: 进入该网页: http://java.sun.com/javase/downloads/index.jsp (或者直接点击下载)例如以下图: 选择 ...

  5. [MVVM Light]Messenger 的使用

    原文:[MVVM Light]Messenger 的使用 当我们使用MVVM开发模式进行开发时,ViewModel之间的通信常常是很头疼的事情,好在MVVM Light提供了Messenger类可以轻 ...

  6. iOS8的APP过渡过程

    1. 2. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWluX3hpYW53ZWk=/font/5a6L5L2T/fontsize/400/fill/ ...

  7. ASP.NET Core 路由 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 路由 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 路由 前两章节中,我们提到 ASP.NET Core 支持 MVC 开发 ...

  8. 亲串 (hdu 2203 KMP)

    亲串 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  9. 阿凡达是脸,教你的脸在线(包括URL和使用)

    官方网站:http://www.mcdonalds.at/avatar/ 英文版本号:lid=finland" target="_blank">http://www ...

  10. Cocostudio学习笔记(3) ImageView + Slider

    此记录使用两个控制流:ImageView 和 Slide. ---------------------------------------------------------------------- ...