开发者

Printing messages to console from C++ DLL

I have an application which uses C# for front end and C++ DLL for the logic part. I would want to print error messages on console screen from my C++ DLL even when the C# GUI is present. Please let me know how to do this.

Thanks开发者_StackOverflow中文版, Rakesh.


You can use OutputDebugString in C++ DLL, and then execute DebugView to get the messages


You can use AllocConsole() to create a console window and then write to standard output.

If you are using C or C++ standard I/O functions (as opposed to direct win32 calls), there are some extra steps you need to take to associate the new console with the C/C++ standard library's idea of standard output. http://www.halcyon.com/~ast/dload/guicon.htm explains what you have to do and why, with complete code.


Unless the application is started from a console, stdin, stdout, and stderr won't even exist and any attempt to use e.g. printf() will fail. Either open your own console or use a debugging mechanism such as OutputDebugString() suggested earlier.


If dealing with DLLs and Service EXEs like COM/DCOM or any other ATL project, you can also use this line of code to print out diagnostic messages in the form of MessageBox windows as an alternative to printing messages to the console:

MessageBox(NULL, L"Com Object Function Called", L"COMServer", MB_OK | MB_SETFOREGROUND);

Example cases where I have used this include the _tWinmain function, as well as constructors and destructors to keep track of instances.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜