我可以在InitInstance中使用此代码来确认哪个可执行文件与给定的扩展名相关联:

TCHAR szRegisteredEXE[_MAX_PATH];
DWORD dwBufferLen = _MAX_PATH;

HRESULT  hRes = AssocQueryString(ASSOCF_NONE, ASSOCSTR_EXECUTABLE,
                    _T("MeetSchedAssist.MWB"), NULL, szRegisteredEXE, &dwBufferLen);
if (hRes == S_OK)
{
    // TODO
}

它工作正常。

我的软件安装了32位版本和64位版本的可执行文件。因此,如果注册的exe不是 Activity 的exe,我想执行的代码是提示更新关联。

我知道如何获取 Activity 的exe路径以及如何确认它是否与szRegisteredEXE相匹配,但是我该如何处理文件关联的更新(假设用户对要关联的提示说"is")?

最佳答案

第一步是创建一个Inno Setup安装程序,该安装程序将为我们管理注册表的调整:

; SignTool parameters
#define SignedDesc "$qMeeting Schedule Assistant File Associations Tool$q"
#define SignedPfx "$q~~~~~$q"
#define SignedTimeStamp "$qhttp://timestamp.verisign.com/scripts/timestamp.dll$q"
#define SignedPw "$q~~~~~$q"

#define AppURL "https://www.publictalksoftware.co.uk"
#define AppPublisher "~~~~~"

#define AppVerText() \
   ParseVersion('..\Meeting Schedule Assistant\Release\Meeting Schedule Assistant.exe', \
       Local[0], Local[1], Local[2], Local[3]), \
   Str(Local[0]) + "." + Str(Local[1]) + "." + Str(Local[2])

[Setup]
DisableReadyPage=True
DisableReadyMemo=True
DisableFinishedPage=True
UsePreviousSetupType=False
UsePreviousTasks=False
UsePreviousLanguage=False
FlatComponentsList=False
AlwaysShowComponentsList=False
ShowComponentSizes=False
AppName=Meeting Schedule Assistant File Associations Tool
AppVersion={#AppVerText}
CreateAppDir=False
Uninstallable=no
OutputBaseFilename=MSATweakFileAssociations
SignTool=SignTool /d {#SignedDesc} /du $q{#AppURL}$q /f {#SignedPfx} /p {#SignedPw} /t {#SignedTimeStamp} /v $f
AppId={{~~~~~}

[registry]
Root: HKCR; SubKey: "MeetSchedAssist.MWB\Shell\Open\Command"; ValueType: string; ValueData: """{param:ExePath}"" ""%1""";
Root: HKCR; SubKey: "MeetSchedAssist.SRR\Shell\Open\Command"; ValueType: string; ValueData: """{param:ExePath}"" ""%1""";

然后,在MFC中,我们运行工具,如下所示:
void COptionsDlg::OnBnClickedMfcbuttonFileAssociations()
{
    // Try to run the help installer
    CString strSetupExe = _T("~~~~~.exe");
    CString strProgramFolder = theApp.GetProgramPath();
    CString strParams = _T("");

    strParams.Format(_T("/SP- /VERYSILENT /ExePath=\"%s\""), (LPCTSTR)theApp.GetProgramEXE());
    if (!theApp.ExecuteProgram(strProgramFolder + strSetupExe, strParams))
    {
        // Problem running the installer
        AfxMessageBox(_T("Unable to change the file associations"), MB_OK | MB_ICONERROR);
        return;
    }
}

BOOL CMeetingScheduleAssistantApp::ExecuteProgram(CString strProgram, CString strArguments)
{
    SHELLEXECUTEINFO    se = { 0 };
    MSG                 sMessage;
    DWORD               dwResult;

    theApp.SetProgramExecuting(true);

    se.cbSize = sizeof(se);
    se.lpFile = strProgram;
    se.lpParameters = strArguments;
    se.nShow = SW_SHOWDEFAULT;
    se.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShellExecuteEx(&se);

    if (se.hProcess != nullptr)
    {
        do
        {
            dwResult = ::MsgWaitForMultipleObjects(1, &(se.hProcess), FALSE,
                INFINITE, QS_ALLINPUT);
            if (dwResult != WAIT_OBJECT_0)
            {
                while (PeekMessage(&sMessage, nullptr, NULL, NULL, PM_REMOVE))
                {
                    TranslateMessage(&sMessage);
                    DispatchMessage(&sMessage);
                }
            }
        } while ((dwResult != WAIT_OBJECT_0) && (dwResult != WAIT_FAILED));

        CloseHandle(se.hProcess);
    }

    theApp.SetProgramExecuting(false);

    if ((DWORD_PTR)(se.hInstApp) < 33)
    {
        // Throw error
        AfxThrowUserException();
        return FALSE;
    }

    SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);

    return TRUE;
}

瞧!它更新注册表。

关于c++ - 使用MFC以编程方式更改文件扩展名关联?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61698570/

10-15 22:22