MsiExec.exe /X{9BA100BF-B59D-4657-9530-891B6EE24E31};
我需要在 main.js 中通过我的 cpp 项目运行这个命令。这是一个新版本的软件,需要在安装前删除旧版本。我想使用应用程序注册表中的卸载字符串来执行此操作。有没有办法在cpp中做到这一点?我正在使用 Qt 5.5。谢谢。
MsiExec.exe /X{9BA100BF-B59D-4657-9530-891B6EE24E31};
我需要在 main.js 中通过我的 cpp 项目运行这个命令。这是一个新版本的软件,需要在安装前删除旧版本。我想使用应用程序注册表中的卸载字符串来执行此操作。有没有办法在cpp中做到这一点?我正在使用 Qt 5.5。谢谢。
有没有办法通过在注册表中查找匹配的 DisplayName 来搜索卸载密钥?然后,如果您通过 DisplayName 找到 GUID,是否像上面那样运行卸载字符串?– 加兰
当然有。您可以使用本机 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;
您还应该知道,某些包不是由其包代码存储在 Uninstall 注册表项下,而是由它们的名称存储,要卸载它们,您必须在特定子项中搜索 UninstallString 值并调用此包。
最简单的方法之一是使用系统函数。
IE:
int result = system("MsiExec.exe /X{9BA100BF-B59D-4657-9530-891B6EE24E31}");
其他更多 Windows 特定方法涉及使用CreateProcess或ShellExecute Windows Win32 API 函数。