一、获取安装程序信息

 #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. CSS 中的高度百分比

    CSS 中可以使用%来给定指定元素的大小,也就是高度.宽度.margin,padding 等等,但是相信很多人都对百分比表示法的具体含义并不清楚,那么不懂就练,毕竟是检验真理的唯一标准(考研党举个手我 ...

  2. 采用navicat导出表结构及数据insert声明

    旧navicat有一段时间,查找navicat真的很方便,它可以支持各种数据库的. 他一直认为无处不在sql文件比较麻烦,每个表会生成一个sql档,不方便开展进口业务.今天,它已突然发现了一个批次sq ...

  3. 定制Octopress

    在 github pages 上搭建好 octopress 博客之后,博客的基本功能就能使用了.如果想自己定制也是没问题的,octopress 有较详尽的官方文档,原则上有问题求助官方即可:octop ...

  4. Android中去掉标题的方法总结

    方法一:也一般入门的时候经常使用的一种方法在setContentView()方法的前面插入代码: requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标 ...

  5. Delphi Bpl包学习

    对于BPL包,我个人理解是:就是一种封装方式,和DLL,EXE类似,把代码放到包(package)里面保存而已. 一.先说说如何创建BPL包 1.   打开delphi IDE(delphi7 为例) ...

  6. Matlab Tricks(十八)—— 矩阵间元素距离的计算

    两个矩阵间元素(向量)距离的度量,首先想到的是遍历,循环的方式,显然 matlab 下的编程并不推荐,matlab 下矩阵向量化编程效率尤高. 先考虑两个向量距离的计算: ∥x−y∥2=∥x∥2+∥y ...

  7. 机器学习: Tensor Flow +CNN 做笑脸识别

    Tensor Flow 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数 ...

  8. socket函数集-----网络编程必备值得拥有

    accept(接受socket连线) 相关函数 socket,bind,listen,connect 表头文件 #include<sys/types.h> #include<sys/ ...

  9. WPF DataGrid自定义列DataGridTextColumn.ElementStyle和DataGridTemplateColumn.CellTemplate

    <Window x:Class="DataGridExam.MainWindow"        xmlns="http://schemas.microsoft.c ...

  10. .NET中System.Diagnostics.Stopwatch、System.Timers.Timer、System.Threading.Timer 的区别

    1.System.Diagnostics.Stopwatch Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间. 在典型的 Stopwatch 方案中,先调用 ...