VC++获取Windows已安装软件——注册表方式
2011-05-03 17:52
549 查看
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;
}
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;
}
相关文章推荐
- VC 获取Windows已安装软件(注册表方式)
- VC 获取Windows已安装软件(注册表方式)
- VC++获取Windows已安装软件——遍历方式
- C++访问注册表获取已安装软件信息列表示例代码
- Windows 7安装软件时无法将注册值写入注册表
- C#通过windows注册表获取软件清单的方法
- c#获取安装的软件和路径(注册表)
- Windows 7安装软件时无法将注册值写入注册表的处理方法
- VC 读注册表获取安装程序
- C# 获取常见软件的安装路径(调用注册表)
- VC通过注册表获取获取某个应用程序的安装路径
- C# 获取常见软件的安装路径(调用注册表)
- 调用Windows接口访问注册表来获取程序安装路径
- 1.python获取windows软件安装列表
- c#获取安装的软件和路径(注册表)
- 获取软件在注册表的安装信息
- Windows 注册表系统软件默认安装路径修改方法
- VC 读注册表获取安装程序
- windows下使用Cmake和VC 2013安装mongodb C driver Windows下安装mongodb的C语言程序驱动必须借助交叉编译软件CMake,但是官网上的文档对于一些
- 获取软件所有快捷方式名称,判断本地是否安装,并打开