[英]Run MsiExec.exe from c++? Windows
MsiExec.exe / X {9BA100BF-B59D-4657-9530-891B6EE24E31};
我需要通过main中的cpp项目运行此命令。 这是软件的新版本,需要在安装之前删除旧版本。 我想使用应用程序注册表中的“卸载字符串”来执行此操作。 有没有办法在cpp中做到这一点? 我正在使用Qt 5.5。 谢谢。
最简单的方法之一就是使用系统功能。
即:
int result = system("MsiExec.exe /X{9BA100BF-B59D-4657-9530-891B6EE24E31}");
Windows的其他更多特定方式涉及使用CreateProcess或ShellExecute Windows Win32 API函数。
是否可以通过在注册表中查找匹配的DisplayName来搜索卸载密钥? 然后,如果您通过DisplayName找到了GUID,运行上面的卸载字符串? – RGarland
当然有。 您可以使用本机Windows API来操纵注册表,也可以根据需要使用一些现有的C ++包装器对该API进行操作。
我写了小的易于使用的注册表包装器 ,它支持枚举注册表项。
我认为您可能会发现解决您的问题很有用。
#include <Registry.hpp>
using namespace m4x1m1l14n;
std::wstring GetProductCodeByDisplayName(const std::wstring& displayName)
{
std::wstring productCode;
auto key = Registry::LocalMachine->Open(L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
key->EnumerateSubKeys([&](const std::wstring& subKeyName) -> bool
{
auto subKey = key->Open(subKeyName);
if (subKey->HasValue(L"DisplayName"))
{
if (displayName == subKey->GetString(L"DisplayName"))
{
// Product found! Store product code
productCode = subKeyName;
// Return false to stop processing
return false;
}
}
// Return true to continue processing subkeys
return true;
});
return productCode;
}
int main()
{
try
{
auto productCode = GetProductCodeByDisplayName(L"VMware Workstation");
if (!productCode.empty())
{
// Uninstall package
}
}
catch (const std::exception& ex)
{
std::cout << ex.what() << std::endl;
}
return 0;
还应注意 ,某些软件包不是通过其软件包代码存储在“卸载”注册表项下,而是通过它们的名称存储,并且要卸载它们,必须在特定子项中搜索UninstallString值,然后调用此软件包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.