一、获取安装程序信息

 #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. Android Studio 使用教程(二十五)之运行Android Studio工程

    一.Android虚拟设备入口 上期我们使用了Android Studio创建了HeloWorld工程,要想运行该工程,首先需要一个Android虚拟设备来模拟Android程序的运行. 重新打开An ...

  2. 数据库版本管理工具Flyway——基础篇

    Flyway 默认规约 SQL 脚本文件默认位置是项目的源文件夹下的db/migration 目录. Java 代码默认位于db.migration 包. SQL 脚本文件及Java 代码类名必须遵循 ...

  3. C++重载加号运算符实现两个结构体的相加

    #include<iostream> #include<string> using namespace std; struct S { int a, b; string str ...

  4. C# opcode 查询源码

    Add|将两个值相加并将结果推送到计算堆栈上.Add.Ovf|将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上.Add.Ovf.Un|将两个无符号整数值相加,执行溢出检查,并且将结果推送到计算 ...

  5. ant的condition任务

    1.istrue isfalse:断言 真 假 <project name="testCondition"> <target name="test&qu ...

  6. ICO图标在线生成,php生成ICO图标在线制作源码

    我们做web系统的时候,每个浏览器的tab这里都会有一个图标,这个图标叫favicon图标,favicon.ico文件放在系统的根目录 如果程序员没有ICO制作工具,那么要如何生成图标呢?可以用程序来 ...

  7. Qt5.9 官方发布的新版本亮点的确不胜枚举(而且修复2000+ bugs)

    作者:Summer Fang链接:https://www.zhihu.com/question/60486611/answer/177584284来源:知乎著作权归作者所有.商业转载请联系作者获得授权 ...

  8. WPF 超长文本的来回滚动

    原文:WPF 超长文本的来回滚动 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Vblegend_2013/article/details/8362 ...

  9. 彩色图像--色彩空间 RGB系列

    学习DIP第62天 转载请标明本文出处:http://blog.csdn.net/tonyshengtan ,出于尊重文章作者的劳动,转载请标明出处!文章代码已托管,欢迎共同开发:https://gi ...

  10. Apache 配置两个域名匹配的文件夹和配置多个Web站点

    Apache的虚拟主机是一种同意在同一台机器上,执行超过一个站点的解决方式,同一时候也就能够邦迪二级域名到指定的文件夹.虚拟主机有两种.一种叫基于IP的(IP-based),还有一种叫基于名字的(na ...