简体   繁体   English

从C ++运行MsiExec.exe吗? 视窗

[英]Run MsiExec.exe from c++? Windows

MsiExec.exe /X{9BA100BF-B59D-4657-9530-891B6EE24E31}; MsiExec.exe / X {9BA100BF-B59D-4657-9530-891B6EE24E31};

I need to run this command through my cpp project in main. 我需要通过main中的cpp项目运行此命令。 This is a new version of a piece of software that needs to remove the older version before installing. 这是软件的新版本,需要在安装之前删除旧版本。 I want to do this using the Uninstall String from the application's registry. 我想使用应用程序注册表中的“卸载字符串”来执行此操作。 Is there a way to do this in cpp? 有没有办法在cpp中做到这一点? I'm using Qt 5.5. 我正在使用Qt 5.5。 Thanks. 谢谢。

One of the simplest ways is to use the system function. 最简单的方法之一就是使用系统功能。

Ie: 即:

int result = system("MsiExec.exe /X{9BA100BF-B59D-4657-9530-891B6EE24E31}");

Other more Windows specific ways involve the use of CreateProcess or ShellExecute Windows Win32 API functions. Windows的其他更多特定方式涉及使用CreateProcessShellExecute Windows Win32 API函数。

Is there a way to search out the uninstall key by looking in the registry for a matching DisplayName? 是否可以通过在注册表中查找匹配的DisplayName来搜索卸载密钥? Then, if you find the GUID by DisplayName, run the uninstall string like above? 然后,如果您通过DisplayName找到了GUID,运行上面的卸载字符串? – RGarland – RGarland

Of course there is. 当然有。 You can use native Windows API for manipulating registry or if you prefer you can use some existing C++ wrapper around that API. 您可以使用本机Windows API来操纵注册表,也可以根据需要使用一些现有的C ++包装器对该API进行操作。

I wrote small easy to use Registry wrapper which supports enumerating registry keys. 我写了小的易于使用的注册表包装器 ,它支持枚举注册表项。

I think you may find it useful to solve your problem. 我认为您可能会发现解决您的问题很有用。

#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;

Also you should be aware , that some packages is not stored by its package code under Uninstall registry key, but by their names and to uninstall them you must search for UninstallString value within specific subkey and call this package instead. 还应注意 ,某些软件包不是通过其软件包代码存储在“卸载”注册表项下,而是通过它们的名称存储,并且要卸载它们,必须在特定子项中搜索UninstallString值,然后调用此软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM