首页 资讯 社群 我的社区 搜索

获取Windows 上已安装的软件列表

木须虎
2020-07-05 12:45:32

同期的另一个需求,要获取这台 Windows 上的机器上安装了哪些软件,理论上这些资料微软应该有提供 SDK 和 WMI 来查询到;

但是我一时没有查到具体应该调用什么 API ,简单点直接读取注册吧,Windows 上已经安装的软件,在注册表中都有记录。

计算机HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionUninstall

SOFTWAREMicrosoftWindowsCurrentVersionUninstall 中就记录这些信息

从注册表中取到的这个信息可以和 Windows 控制面板中的“控制面板程序程序和功能” 对应上

 

明确资料源,直接上代码:

int GetSoftListInfo()
{
	const int  MAX_LEG = 256 * sizeof(TCHAR);
	HKEY hKey;
	DWORD dwRegNum = MAX_LEG;
	TCHAR regBufferName[MAX_LEG] = { 0 };

	int softCount = 0;

	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"),
		NULL, KEY_READ, &hKey) == ERROR_SUCCESS)
	{
		if (RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwRegNum, NULL, NULL, NULL,
			NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
		{
			for (ULONG i = 0; i < dwRegNum; i++)
			{
				DWORD dwRegSize = MAX_LEG;
				RegEnumKeyEx(hKey, i, regBufferName, &dwRegSize, NULL, NULL,
					NULL, NULL);
				DWORD dwType;
				HKEY hSubKey;
				if (RegOpenKeyEx(hKey, regBufferName, NULL, KEY_READ, &hSubKey) == ERROR_SUCCESS)
				{
			
					TCHAR DisplayName[MAX_LEG] = { 0 };
					dwRegSize = MAX_LEG;
					ZeroMemory(DisplayName, MAX_LEG);
					RegQueryValueEx(hSubKey, _T("DisplayName"), 0, &dwType, (LPBYTE)DisplayName, &dwRegSize);


					TCHAR DisplayVersion[MAX_LEG] = { 0 };
					dwRegSize = MAX_LEG;
					ZeroMemory(DisplayVersion, MAX_LEG);
					RegQueryValueEx(hSubKey, _T("DisplayVersion"), 0, &dwType, (LPBYTE)DisplayVersion, &dwRegSize);

					TCHAR Publisher[MAX_LEG] = { 0 };
					dwRegSize = MAX_LEG;
					ZeroMemory(Publisher, MAX_LEG);
					RegQueryValueEx(hSubKey, _T("Publisher"), 0, &dwType, (LPBYTE)Publisher, &dwRegSize);

					TCHAR InstallDate[MAX_LEG] = { 0 };
					dwRegSize = MAX_LEG;
					ZeroMemory(InstallDate, MAX_LEG);
					RegQueryValueEx(hSubKey, _T("InstallDate"), 0, &dwType, (LPBYTE)InstallDate, &dwRegSize);

					if ( lstrlen( DisplayName ) > 0   )
					{
                        printf( "%lst%lst%lst%ls n" , DisplayName, DisplayVersion, Publisher , InstallDate );

						softCount++;
					}
					
				}
			}
		}
	}
	else
	{
		return FALSE; //
	}
	RegCloseKey(hKey);

	return softCount;
}
用户评论