6

I'm running a console app (myApp.exe) which outputs a pseudo localized (unicode) string to the standard output. If I run this in a regular command prompt(cmd.exe), the unicode data gets lost. If I run this in a unicode command prompt(cmd.exe /u) or set the properties of the console to "Lucida Console" then the unicode string is maintained.

I'd like to run this app in C# and redirect the unicode string into a local variable. I'm using a Process object with RedirectStandardOutput = true, but the unicode string is always lost.

How can I specify to persist this unicode info?

        private static int RunDISM(string Args, out string ConsoleOutput)
        {
            Process process = new Process();
            process.StartInfo.FileName = "myApp.exe";
            process.StartInfo.Arguments = Args;

            try
            {
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.WorkingDirectory = Environment.CurrentDirectory;

                process.Start();
                process.WaitForExit(Int32.MaxValue);
            }
            catch (Exception e)
            {
                WEX.Logging.Interop.Log.Assert("Failure while starting or running process.\nERROR: " + e.Message);
                ConsoleOutput = null;
                return EXITCODE_ERROR;
            }

            ConsoleOutput = process.StandardOutput.ReadToEnd();
            return process.ExitCode;
        } 
Jason Bunting
  • 58,249
  • 14
  • 102
  • 93
Kenn
  • 2,379
  • 2
  • 29
  • 38
  • Can you provide us with more details on the unicode content you're trying to get out of myApp.exe, particularly a sample string that isn't coming through properly? That might help us get to a working solution. – Charlie Oct 17 '08 at 04:09
  • On another note: you should call ReadToEnd() BEFORE WaitToExit(). See: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx – Ben Sep 09 '09 at 17:29

3 Answers3

3

It looks like you need to change the encoding on the StandardOutput stream from your console app, using the StandardOutputEncoding property on ProcessStartInfo. Try adding the following code inside your try/catch block, before starting the process:

process.StartInfo.StandardOutputEncoding = Encoding.Unicode;

You might have to experiment with different encodings to see which is the right one for your case.

Charlie
  • 44,214
  • 4
  • 43
  • 69
  • 1
    I have tried all the encodings and the data is always lost or misrepresented. :( – Kenn Oct 16 '08 at 19:17
  • Don't you mean the StandardInputEncoding? If myApp.exe is emitting "Unicode" (whatever that means in this context) then it's the application running myApp.exe that must attempt to parse that correctly. – bzlm Oct 19 '08 at 21:28
  • The way to think of it is that StandardOutput is a stream that the C# app is going to be reading from. This stream happens to contain the data that myApp writes to its own stdout. Since the data will be in unicode form, the stream needs its encoding set accordingly. – Charlie Oct 20 '08 at 00:02
2

Get the bytes out and see if they make any sense:

var b = p.StandardOutput.CurrentEncoding.GetBytes(p.StandardOutput.ReadToEnd());

Once you figured out the actual encoding you can use the standard encoding APIs to convert the bytes into a string.

mtmk
  • 6,176
  • 27
  • 32
0

First set process.StartInfo.StandardOutputEncoding = Encoding.Default.

Then goto Control Panel > Region > Administrative > Change system locale... and set encoding of your pseudo localized string there.

M. Jahedbozorgan
  • 6,914
  • 2
  • 46
  • 51