注册表操作总结(二)

2014-11-24 08:30:50 · 作者: · 浏览: 1
------------------
HKEY hKey;
LPCSTR lpszSubKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (ret != ERROR_SUCCESS)
{
return -1;
}
// 获取子键&键值信息-------------------------------------------------------
DWORD dwSubKeysCnt; // 子键数量
DWORD dwMaxSubKeyNameLen; // 子键名字的最大长度(not including the terminating null character)
DWORD dwKeyValueCnt; // 键值的数量
DWORD dwMaxKeyValueNameLen; // 键值名字的最大长度(not including the terminating null character)
DWORD dwMaxKeyValueDataLen; // 键值数据的最大长度(in Bytes)
ret = RegQueryInfoKey(
hKey,
NULL,
NULL,
NULL,
&dwSubKeysCnt,
&dwMaxSubKeyNameLen,
NULL,
&dwKeyValueCnt,
&dwMaxKeyValueNameLen,
&dwMaxKeyValueDataLen,
NULL,
NULL);
if (ret != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return -1;
}
// 枚举子键信息------------------------------------------------------------
DWORD dwIndex;
LPSTR lpszSubKeyName = new char[dwMaxSubKeyNameLen + 1];
DWORD dwNameLen = dwMaxSubKeyNameLen + 1;
for (dwIndex = 0; dwIndex < dwSubKeysCnt; ++dwIndex)
{
dwNameLen = dwMaxSubKeyNameLen + 1;
memset(lpszSubKeyName, 0, dwMaxSubKeyNameLen + 1);
ret = RegEnumKeyEx(
hKey,
dwIndex,
lpszSubKeyName,
&dwNameLen,
NULL,
NULL,
NULL,
NULL);
if (ret != ERROR_SUCCESS)
{
RegCloseKey(hKey);
delete[] lpszSubKeyName;
return -1;
}
//************获取具体的程序的安装信息BEG*************
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);
vAppInfo.push_back(appInfo);
//************获取具体的程序的安装信息END*************
}
delete[] lpszSubKeyName;
// 关闭注册表键------------------------------------------------------------
RegCloseKey(hKey);
return 0;
}
int main()
{
cout << "Reg Demo Test" << endl;
vector vAppInfo;
cout << GetAllInstalledAppInfoA(vAppInfo) << endl;
//输出到文件
vector::iterator iter = vAppInfo.begin();
FILE *fp = fopen("D:\\InstalledAppInfo.txt", "a");
while (iter != vAppInfo.end())
{
fprintf(fp, "----------------\n");
fprintf(fp, "Name: %s\n DisplayName: %s\n Publisher: %s\n Version: %s\n DisplayVersion: %s\n InstallLocation: %s\n",
iter->strName.c_str(), iter->strDisplayName.c_str(),
iter->strPublisher.c_str(), iter->strVersion.c_str(),
iter->strDisplayVersion.c_str(), iter->strInstallLocation.c_str());
fprintf(fp, "----------------\n\n");
++iter;