ڼС
梦回起点
做你害怕做的事,你会发现:不过如此
本站基于WordPress—主题by 设计窝
冀ICP备15003737号
梦回起点
Copyright © 2015-2024 All rights reserved.

Win32 C++通过注册表获取已经安装的软件

#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;
}
2022-05-15
                         
暂无评论

发表回复