foka
foka

Reputation: 860

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

Upvotes: 4

Views: 2539

Answers (2)

Christopher Painter
Christopher Painter

Reputation: 55581

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.

Upvotes: 4

Yan Sklyarenko
Yan Sklyarenko

Reputation: 32240

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

Upvotes: 4

Related Questions