VC++获取Windows已安装软件——注册表方式

Windows把卸载信息保存在注册表键HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Uninstall之下。我们可以枚举这个键的子键来了解哪个软件已经安装且可以卸载。在这个键的下面,每个子键代表一个可卸载项,每个子键下都列出一系列和相关软件有关的信息,重要的是以下信息:
 
DisplayName: 显示名称,这是我们看到的该软件的名字,例如,Adobe Acrobat 5.0
DisplayVersion: 显示版本,软件的版本号
InstallLocation:软件安装目录
InstallSource:该软件从哪个位置安装(例如网络共享或者安装光盘)
Publisher: 发布该软件的厂商
UninstallPath:卸载该软件的路径
UninstallString:卸载该软件的命令行
URLinfoabout:介绍该软件的网页
UrlUpdateInfo:网络更新该软件的位置


2. C++版本
 
 // listsoft.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream.h>
#include <windows.h>
#include <tchar.h>
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)
    {
        cout << _T("Cannot open key ") << szKey << _T("(") << lr << _T(")") << endl;
        return;
    }
  
    size = sizeof(buffer);
    lr = GetValue(hKey, _T("DisplayName"), &buffer[0], &size);
    if(lr == ERROR_SUCCESS)
    {
         if(size > 0)
         {
              cout << _T("Display Name:" ) << buffer << endl;
         }
    }else{
         size = sizeof(buffer);
         lr = GetValue(hKey, _T("QuietDisplayName"), &buffer[0], &size);
         if(ERROR_SUCCESS== lr && size > 0 )
         {
              cout << _T("Display Name:" ) << buffer << endl;
         }
    }
 
     size = sizeof(buffer);
     lr = GetValue(hKey, _T("InstallLocation"), &buffer[0], &size);
     if(ERROR_SUCCESS == lr && size > 0)
     {
           cout << _T("Installation Location:") << buffer << endl;
     }
    
     size = sizeof(buffer);
     lr = GetValue(hKey, _T("InstallSource"), &buffer[0], &size);
     if(ERROR_SUCCESS == lr && size > 0)
     {
           cout << _T("Installation Source:") << buffer << endl;
     }
 
     size = sizeof(buffer);
     lr = GetValue(hKey, _T("UninstallPath"), &buffer[0], &size);
     if(ERROR_SUCCESS == lr && size > 0)
     {
           cout << _T("Uninstall Path:") << buffer << endl;
     }
 
     size = sizeof(buffer);
     lr = GetValue(hKey, _T("UninstallString"), &buffer[0], &size);
     if(ERROR_SUCCESS == lr && size > 0)
     {
             cout << _T("Uninstall String:") << buffer << endl;
     }
     RegCloseKey(hKey);
}

int main(int argc, char* argv[])
{
    unsigned long index;
    TCHAR buffer[MAX_PATH];
    HKEY hKey;
    LRESULT lr;
    lr = RegOpenKey(HKEY_LOCAL_MACHINE, _T("SOFTWARE//Microsoft//Windows//CurrentVersion//Uninstall"), & hKey);
    if(lr != ERROR_SUCCESS)
    {
         cout << _T("Cannot open the key ") << lr << endl;
         return -1;
    }
 
     for(index =0; ;index++)
     {
            lr = RegEnumKey(hKey, index, &buffer[0], sizeof(buffer));
            switch(lr)
            {
                 case ERROR_SUCCESS:
                      DumpSoftware(buffer,hKey);
                      break;
                 case ERROR_NO_MORE_ITEMS:
                      cout<<_T("Finish enumerate softwares") << endl;
                      RegCloseKey(hKey);
                      return 0;
                 default:
                      cout<<_T("Cannot enumerate software ") << lr << endl;
                      RegCloseKey(hKey);
                 return -2;
             }
       }
       cout<<_T("Finish enumerate softwares") << endl;
       RegCloseKey(hKey);
       return 0;
}

猜你喜欢

转载自blog.csdn.net/Fenglin6165/article/details/6387783