Inside a C# Console application, I'm importing a native C++ DLL methods. for example:
[DllImport("MyDll.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern int MyMethod(IntPtr somePointer);
When executed, MyMethod()
is printing output to the Console, which I would like to be hidden.
Assuming I can't change the DLL, how can I still suppress it's output?
Modified from http://social.msdn.microsoft.com/Forums/vstudio/en-US/31a93b8b-3289-4a7e-9acc-71554ab8fca4/net-gui-application-native-library-console-stdout-redirection-via-anonymous-pipes
I removed the part where they try to redirect it because if you read further, it says they were having issues when it was called more than once.
and usage sample:
The only way you can hope to do this is to redirect the standard output whenever you call into the DLL. I've never attempted this and have no idea whether or not it works.
Use
SetStdHandle
to direct standard output to some other place. For example a handle to the nul device would do. If you need to restore the original standard output handle after calls to the DLL return, that takes another call toSetStdHandle
.You'll need to jump through these hoops for each and every call to the DLL. Things would get even more complex if you have threads and/or callbacks.