#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <tchar.h>
using namespace std;
int nCount = 0;
//
LRESULT GetValue(HKEY hKey, LPCTSTR name, LPTSTR value, LPLONG size)
{
return ::RegQueryValueEx(hKey, name, NULL, NULL, (LPBYTE)value, (LPDWORD)size);
}
//
void DumpSoftware(LPCTSTR szKey, HKEY hParent)
{
LRESULT lr;
HKEY hKey;
LONG size;
TCHAR buffer[MAX_PATH];
lr = RegOpenKey(hParent, szKey, &hKey);
//不能打开注册表
if (lr != ERROR_SUCCESS)
{
wcout << _T("Cannot open key ") << szKey << _T("(") << lr << _T(")") << endl;
return;
}
size = sizeof(buffer);
lr = GetValue(hKey, _T("SystemComponent"), &buffer[0], &size);
if (lr == ERROR_SUCCESS) {
return;
}
size = sizeof(buffer);
lr = GetValue(hKey, _T("ParentKeyName"), &buffer[0], &size);
if (lr == ERROR_SUCCESS) {
return;
}
nCount++;
size = sizeof(buffer);
lr = GetValue(hKey, _T("DisplayName"), &buffer[0], &size);
if (lr == ERROR_SUCCESS)
{
if (size > 0)
{
wcout << _T("Display Name: ") << buffer << endl;
}
}
else
{
size = sizeof(buffer);
lr = GetValue(hKey, _T("QuietDisplayName"), &buffer[0], &size);
if (ERROR_SUCCESS == lr && size > 0)
{
wcout << _T("Display Name: ") << buffer << endl;
}
}
/*size = sizeof(buffer);
lr = GetValue(hKey, _T("InstallLocation"), &buffer[0], &size);
if (ERROR_SUCCESS == lr && size > 0)
{
wcout << _T("Installation Location: ") << buffer << endl;
}
size = sizeof(buffer);
lr = GetValue(hKey, _T("InstallSource"), &buffer[0], &size);
if (ERROR_SUCCESS == lr && size > 0)
{
wcout << _T("Installation Source: ") << buffer << endl;
}
size = sizeof(buffer);
lr = GetValue(hKey, _T("UninstallPath"), &buffer[0], &size);
if (ERROR_SUCCESS == lr && size > 0)
{
wcout << _T("Uninstall Path: ") << buffer << endl;
}
size = sizeof(buffer);
lr = GetValue(hKey, _T("UninstallString"), &buffer[0], &size);
if (ERROR_SUCCESS == lr && size > 0)
{
wcout << _T("Uninstall String: ") << buffer << endl;
}*/
RegCloseKey(hKey);
}
#define UNINSTALL_SOFT _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall")
//
int main(int argc, char* argv[])
{
std::wcout.imbue(std::locale("chs"));
unsigned long index;
TCHAR buffer[MAX_PATH];
HKEY hKey;
HRESULT hr = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, UNINSTALL_SOFT, 0, /*KEY_ALL_ACCESS*/KEY_READ, &hKey);
if (hr != ERROR_SUCCESS)
{
wcout << _T("Cannot open the key :") << hr << endl;
return -1;
}
for (index = 0; ERROR_NO_MORE_ITEMS != hr; index++)
{
hr = RegEnumKey(hKey, index, &buffer[0], sizeof(buffer));
if (ERROR_SUCCESS == hr)
{
DumpSoftware(buffer, hKey);
}
}
RegCloseKey(hKey);
hr = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, UNINSTALL_SOFT, 0, /*KEY_ALL_ACCESS*/KEY_READ | KEY_WOW64_64KEY, &hKey);
if (hr != ERROR_SUCCESS)
{
wcout << _T("Cannot open the key :") << hr << endl;
return -1;
}
for (index = 0; ERROR_NO_MORE_ITEMS != hr ; index++)
{
hr = RegEnumKey(hKey, index, &buffer[0], sizeof(buffer));
if (ERROR_SUCCESS == hr)
{
DumpSoftware(buffer, hKey);
}
}
RegCloseKey(hKey);
hr = ::RegOpenKeyEx(HKEY_CURRENT_USER, UNINSTALL_SOFT, 0, /*KEY_ALL_ACCESS*/KEY_READ | KEY_WOW64_64KEY, &hKey);
if (hr != ERROR_SUCCESS)
{
wcout << _T("Cannot open the key :") << hr << endl;
return -1;
}
for (index = 0; ERROR_NO_MORE_ITEMS != hr; index++)
{
hr = RegEnumKey(hKey, index, &buffer[0], sizeof(buffer));
if (ERROR_SUCCESS == hr)
{
DumpSoftware(buffer, hKey);
}
}
wcout << _T("Finish enumerate softwares\r\n") << endl;
RegCloseKey(hKey);
system("pause");
return 0;
}