3
votes

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)?

2
Do you mean your custom actions calls some EXE which writes to console, and you'd like to grab that output?Yan Sklyarenko
Yan, yes, that's what I meant. Question edited.foka

2 Answers

3
votes

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();
3
votes

This is called "screen scraping" and while it's technically possible to create the infrastructure to run an EXE out of process, scrape it's output and then marshal the data back into the MSI context, it'll never be a robust solution.

A better solution would be to understand what the EXE does and how it does it. Then write a C# o C++ custom action that runs in process with access to the MSI handle so that you can do the work and set the properties you need to set.