1


I have a string in a WPF app which I pass as a command line argument to a native application using Process.StartInfo.Arguments property. I know that C# stores strings in UTF-16 encoding. And also I know that native application does nothing about encoding and expects that it is default which is windows-1251 in my country. So we have UTF-16 in C# app and 1251 in a native one. And this actually works somehow.


The question is where the convertion took place and how reliable it is. Is there a way to force Process class use specific encoding for the command line arguments?

3
  • Process Proc = new Process(); Proc.StartInfo.StandardOutputEncoding = Encoding.UTF8; Commented Dec 19, 2019 at 8:33
  • 1
    @Clint Are you sure that has an effect on the process arguments? Commented Dec 19, 2019 at 8:34
  • oops, its the wrong context Arguments are received in UTF-16, I think the same is true for .NET strings Commented Dec 19, 2019 at 8:40

1 Answer 1

3

On a Unicode operating system (i.e. any modern version of Windows), the Process.Start method calls the Win32 CreateProcessW function, which also uses a UTF-16 representation for the command line arguments.

The character conversion (if any is needed) is performed by the operating system when it launches the process. If the native application's entry point accepts a Unicode command line then no conversion is needed, otherwise the OS converts it to an ANSI string. I do not believe you as the caller have the ability to control which codepage is used here.

Sign up to request clarification or add additional context in comments.

2 Comments

So the CreateProcessW call will see that the entry point of native application accepts char* and convert UTF-16 string to ANSI string using default code page of OS? How can I control the code page being used?
The answer to this question would imply that the codepage of the new process inherits the locale of the current thread in the parent process: stackoverflow.com/questions/421409/… - so you may be able to control it by changing Thread.CurrentThread.CurrentCulture or Thread.CurrentThread.CurrentUICulture before you start the process (but I have not tested this). If not, then I don't think you can change the codepage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.