Natalie Carr
Natalie Carr

Reputation: 3797

Error handling from DoAction Custom Actions

I am trying to use an error dialog from a DoAction CustomAction. I know I cannot use MsiProcessMessage, at the moment I am using a MessageBoxbut I know this is not recommended. Can anyone tell me how to use the error table from a DoAction CustomAction?

What I want to do is check if a number is greater than 10,000 and if it is throw an error:

    WcaGetIntProperty(L"DIAGNOSTICCHANNELS",&szDiagnosticChannels);
    if (szDiagnosticChannels > 10000) MessageBox(NULL, szError, szTitle, MB_OK|MB_ICONEXCLAMATION);

Upvotes: 1

Views: 804

Answers (1)

Rob Mensching
Rob Mensching

Reputation: 35946

Very frustrating that the Windows Installer doesn't support MsiProcessMessage() from a DoAction. Fortunately, the wcautil.lib in the WiX toolset has some helper methods that will make it not too hard to replicate. It'd look a lot like this:

int nDiagnosticChannels = 0;
PMSIHANDLE hErrorTable;
PMSIHANDLE hErrorMessage;
LPWSTR sczError = NULL;
LPWSTR sczErrorFormatted = NULL;

hr = WcaGetIntProperty(L"DIAGNOSTICCHANNELS",&nDiagnosticChannels);
ExitOnFailure(hr, "Failed to read DIAGNOSTICCHANNELS property.");

if (nDiagnosticChannels > 10000)
{
    // "12345" is your custom error number.
    hr = WcaOpenExecuteView("SELECT `Message` FROM `Error` WHERE `Error`=12345", &hErrorTable); 
    ExitOnFailure(hr, "Failed to query error table.");

    hr = WcaFetchSingleRecord(hErrorTable, hErrorMessage);
    ExitOnFailure(hr, "Failed to get message from error table.");

    hr = WcaGetRecordString(hErrorMessage, 1, &sczError);
    ExitOnFailure(hr, "Failed to get error message");

    hr = WcaGetFormattedString(sczError, &sczErrorFormatted);
    ExitOnFailure(hr, "Failed to format error message");

    ::MessageBox(NULL, sczErrorFormatted, szTitle, MB_OK|MB_ICONEXCLAMATION);
}

LExit:
    ReleaseStr(sczError);
    ReleaseStr(sczErrorFormatted);
    return hr;

It'd be nice if there were a couple more helper functions there to cut down a few functions calls but it will work.

Upvotes: 1

Related Questions