Rakesh K
Rakesh K

Reputation: 8505

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, Rakesh.

Upvotes: 13

Views: 44211

Answers (5)

sbsohrabi
sbsohrabi

Reputation: 11

If you are using C++ standard I/O functions you can use these two useful MACROs:

 _RPT1(0, "%f\n", sum);
 TRACE("The value of x is %f\n", sum);

Here f is double and the result can be viewed in DebugView.

P.S.: I'm using this code in MS Visual Studio 2019 C++;

Upvotes: 0

matrixanomaly
matrixanomaly

Reputation: 6947

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.

Upvotes: 0

Greg Najda
Greg Najda

Reputation: 1035

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.

Upvotes: 11

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798626

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.

Upvotes: 5

Francis
Francis

Reputation: 12008

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

Upvotes: 18

Related Questions