Search code examples
wixshellexecutewix3.8

Open PDF file after WiX installation, without showing any errors


I want to open a PDF file after my WiX installer completes.

The relevant WiX XML I currently have is:

<Property Id="WixShellExecTarget" Value="[#Manual.pdf]" />

<CustomAction Id="ShowManual" 
    Return="ignore" 
    BinaryKey="WixCA"
    DllEntry="WixShellExec" 
    Impersonate="yes" />

<InstallExecuteSequence>
    <Custom Action="ShowManual" After="InstallFinalize">NOT Installed</Custom>
</InstallExecuteSequence>

This all works fine on machines where a PDF reader is installed. But if not, Windows is flashing up a message saying 'Windows can't open this type of file'.

Is there any way to get WiX to only attempt the call to ShellExecute if there is an application associated with PDF files? Or is it possible to get the call to fail silently without displaying any errors?


Solution

  • I solved this by creating an 'immediate' managed custom action that runs after InstallFinalize and uses FindExecutable to check if an application is associated with PDF files before trying to open it:

    [DllImport("shell32.dll", EntryPoint = "FindExecutable")]
    private static extern long FindExecutable(string lpFile, string lpDirectory, StringBuilder lpResult);
    
    [CustomAction]
    public static ActionResult ShowPdf(Session session)
    {
        var installDir = session["INSTALLDIR"];
        var pdfPath = Path.Combine(installDir, @"My Dir\My.pdf");
    
        var pdfReaderPath = new StringBuilder(1024);
        long lngResult = FindExecutable(pdfPath, String.Empty, pdfReaderPath);
    
        if ((lngResult >= 32) && (!String.IsNullOrWhiteSpace(pdfReaderPath.ToString())))
        {
            Process.Start(pdfPath);
        }
    
        return ActionResult.Success;
    }