Search code examples
wixwindows-installerwix3.5custom-action

How can I read custom action's executable output in WiX?


I'm making MSI installer in WiX. During installation I want to run an executable from a custom action and get its standard output (not the return code) for later use during installation (with Property element, supposedly).

How can I achieve it in WiX (3.5)?


Solution

  • I used this code for a similar task (its a C# DTF custom action):

    // your process data
    ProcessStartInfo processInfo = new ProcessStartInfo() {
       CreateNoWindow = true,
       LoadUserProfile = true,
       UseShellExecute = false,
       RedirectStandardOutput = true,
       StandardOutputEncoding = Encoding.UTF8,
       ...
    };
    
    Process process = new Process();
    process.StartInfo = processInfo;
    process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
      {
         if (!string.IsNullOrEmpty(e.Data) && session != null)
         {
             // HERE GOES THE TRICK!
             Record record = new Record(1);
             record.SetString(1, e.Data);
             session.Message(InstallMessage.ActionData, record);
          }
      };
    
    process.Start();
    process.BeginOutputReadLine();
    process.WaitForExit();
    
    if (process.ExitCode != 0) {
       throw new Exception("Execution failed (" + processInfo.FileName + " " + processInfo.Arguments + "). Code: " + process.ExitCode);
    }
    
    process.Close();