How can I capture the console output of dpinst when running silently?

2.6k Views Asked by At

I've got a process (written in C#) that runs with administrative rights and invokes dpinst.exe to perform an automated driver installation. It writes its own custom dpinst.xml file to specify things like suppressing the EULA page, suppressing the wizard, running a "quiet install," and searching in all subdirectories.

When I invoke this manually from the command line (see example below), it seems to work fine. Because of the command line switches I'm using it prints a bunch of INFO level log messages in the console.

C:\Path\To\Drivers> dpinst.exe /C /L 0x409

I want to log what gets printed in the console, so my C# code looks something like this:

var process = new Process
{
    StartInfo = new ProcessStartInfo
    {
        FileName = @"C:\Path\To\Drivers\dpinst.exe",
        Arguments = "/C /L 0x409",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true,
        Verb = "runas"
    }
};

string output;
process.Start();

using (var reader = process.StandardOutput)
{
    output = reader.ReadToEnd();
    reader.Close();
}

However, when I run that code, the value of output is always blank. So, for my next experiment, I tried using the command line directly to pipe the output to a file, like this:

C:\Path\To\Drivers> dpinst.exe /C /L 0x409 > test.log 2>&1

That created the test.log file, but it was also blank. Interestingly enough I could still see all of the console output that dpinst.exe generates in that same console window; for some reason it didn't get redirected to the file that I specified. So the symptom is the same regardless of how I invoke the dpinst executable; it doesn't want to redirect output. I'm not sure what the underlying reason for that is, nor how to solve it. How can I capture the console output?

EDIT: If anyone wants a copy of dpinst.exe to run locally and test out themselves, I've provided one at this link, bundled with the dpinst.xml file I'm using. Remember that you need to invoke it with the /C command line switch in order to generate any command line output. Alternatively, if you're paranoid and don't want to download an executable from some random Stack Overflow question, you can get the dpinst.exe utility as part of the Windows Driver Kit from Microsoft. It's a free download, but you have to extract the utility (which is only 500 KB) from the full WDK ISO (which is ~700 MB). Up to you. :)

2

There are 2 best solutions below

0
On

I my answer will help other. When you run Dpinst.exe and you add the swicth /C to dump the log to console, it also creates a log file in this directory "C:\Windows\DPINST.LOG"

You can locate the log file there..

4
On

Your code runs perfectly fine with standard tools (like "ping") for example. So maybe for some reason dpinst writes to standard error instead? You can set RedirectStandardError = true and read StandardError to check if that is the case.

UPDATED in case anyone else will hit this problem:

It seems dpinst does not write to standard output but logs messages to console in some other way. Only way to achieve your goal which comes to my mind is: remember size of "%SystemRoot%\DPINST.LOG" file, run your command, wait for exit, then read everything between remembered position and end of file. This way you will get your logs.