簡體   English   中英

從C ++運行MsiExec.exe嗎? 視窗

[英]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的其他更多特定方式涉及使用CreateProcessShellExecute 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM