0

I have a ATL exe module, which I want to print some output to the console. This module is called by another C# console application, so it should already has a console.

In ATL:

extern "C" int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR , int nShowCmd)
{
    printf("test");                                   // <- this did nothing
    MessageBox(nullptr, L"test", L"caption", MB_OK);  // <- this work
    //...
}
Dia
  • 851
  • 1
  • 15
  • 35
  • You state "This module is called by another C# console application", however this appears to be a separate Windows application (not a console application), given that you have a _tWinMain and not a main entry function. See https://stackoverflow.com/questions/18709403/winmain-vs-main-c for details. You should be able to change the project type from Windows to Console, and then see your printf output. – holtavolt Jan 17 '18 at 03:55
  • @holtavolt it is separate COM object - so it is fine to have it as EXE (assuming OP wants process isolation for some reason or maybe sharing same COM object between multiple clients in the same session) – Alexei Levenkov Jan 17 '18 at 04:37
  • @Alexei Levenkov - I see (I missed the COM mention in the title) and upvoted your answer. – holtavolt Jan 17 '18 at 05:24

1 Answers1

2

EXE COM server will be launched by itself and not attached to console of the caller - it may be even already launched by some previous client which may not even be console app.

If you need to send output to caller's console you'd better just return text to caller.

It may be possible to pass caller's console handle to the COM object as method parameter and try to configure default output to use that console, but I'd personally will not even try. I.e. you'd have to figure out how multiple clients can provide multiple console handles to set as default...

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 1
    @Dia - if your goal is to be able to add text strictly for development debugging purposes of the COM server, you might consider adding a file logger, and run a "tail -f" utility in your console window. Alternately, you can use OutputDebugString and DebugView utility (or just the Output window of Visual Studio) to get console-like logging captured. – holtavolt Jan 17 '18 at 05:30
  • @holtavolt I'm using a logging file now. Thanks for your comment. – Dia Jan 17 '18 at 05:39