使用MSI的函数可以检测软件是不是安装,获得安装版本信息等,条件是软件为.msi文件安装的。
#include <Windows.h>
#include <Msi.h>
#pragma comment(lib,"Msi.lib")
1. 检测软件是不是安装,upgradeCode用于标示从1个版本升级到另外一个版本,1般可以用于判断是不是是同1个软件。bool CheckExistSoftware(wchar_t *upgradeCode) //可以通过工具Orca查看安装包相应的upgradeCode
{
wchar_t productCode[39];
if (ERROR_SUCCESS == MsiEnumRelatedProductsW(upgradeCode,productCode))
{
return true;
}
return false;
}
2. 获得软件的安装路径
void GetSoftwareInstallPath(const wchar_t *upgradeCode,wchar_t *installPath,DWORD *pathLength)
{
DWORD i = 0;
UINT result;
wchar_t productCode[39];
for (i = 0; ERROR_NO_MORE_ITEMS != (result = MsiEnumRelatedProductsW(upgradeCode,i,productCode)); i++)
{
if (ERROR_SUCCESS == result)
{
if (ERROR_SUCCESS == MsiGetProductInfoW(productCode,INSTALLPROPERTY_INSTALLLOCATION,installPath,pathLength))
{
return;
}
}
}
wcscpy_s(installPath,2,L"");
}