使用MSI的函数可以检测软件是否安装,获取安装版本信息等,前提是软件为.msi文件安装的。
在使用前建议加上如下头文件及库依赖:
#include <Windows.h>#include <Msi.h>#pragma comment(lib, "Msi.lib")1. 检测软件是否安装,upgradeCode用于标示从一个版本升级到另一个版本,一般可以用于判断是否是同一个软件。bool CheckExistSoftware(wchar_t *upgradeCode) //可以通过工具Orca查看安装包相应的upgradeCode{ wchar_t productCode[39]; if (ERROR_SUCCESS == MsiEnumRelatedProductsW(upgradeCode, 0, 0, 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, 0, i, productCode)); i++){if (ERROR_SUCCESS == result){if (ERROR_SUCCESS == MsiGetProductInfoW(productCode, INSTALLPROPERTY_INSTALLLOCATION, installPath, pathLength)){return;}}}wcscpy_s(installPath, 2, L"");}
,旅行要学会随遇而安,淡然一点,