4
votes

I have a C++ dll reporting lots of useful information via console output using printf or cout. I'm linking this dll to my GUI application written in MFC and want to access its output.

Sounds pretty simple, yeah? Well, I was surprised when after hours of searching internet and implementing several solutions none of them worked!

So maybe some additional information about application is needed. I have a huge host application to which I have no access. Actually, I'm writing plugin to it. It links my plugin dll at runtime (using LoadLibrary(), I suppose). My dll at its turn uses external dll which output I want to acquire. This dll is linked to my statically via corresponding lib file, so CRT does all the loading.

As far as I know, any application (including all loaded in whatever way dll's) has single stdout stream (single stdin and single stderr). And that's when things are going too complicated for me. I've tried to redirect this stdout stream (obviously, for whole application/process) and in some cases it worked for MY dll, but it still doesn't work for EXTERNAL dll. I.e. if I use something like cout << "Hey!"; from my dll, I can see this in file to which I've redirected stream. However, the same call from inside external dll (yes, I have it's sources but I really don't want to change them) does nothing. God knows where this output is done.

I've tried:

  1. Good old freopen() trick. Worked for my dll, no effect for external dll.
  2. WinAPI SetStdHandle() call. Doesn't work at all. Seems to have some troubles with Win7.
  3. AllocConsole() + _open_osfhandle(). Same as 1. See my output in console, see no output of external dll.
  4. cout.rdbuf() reassign. Same as 3 but only for cout.

Well, guys, I really need help. Seems like I'm stuck :-/

Additional info:
Host application, my dll and external dll was developed using MSVS'05. All dlls have "Use MFC in a Static Library" setting. Testing was done under Win7 x64. I do have ALL sources but I have no permission to change them.

2
Your best bet would probably be SetStdHandle() - what exactly is not working with it? Do you get any errors?Martin Ba
GUI applications have no std handles by default under Win7 if started not from the command prompt social.msdn.microsoft.com/forums/en-us/…Mikhail
Interesting thread you link to - however this is an unresolved msdn forum thread, so no definite info from there. Also: You comment says "no std handles by default" and the msdn forum thread seems to imply that SetStdHandle is simply not working under Win7/no console!Martin Ba

2 Answers

1
votes

You are IIRC getting screwed up by the "Use MFC in a Static Library" setting. Setting/reopening the int (POSIX style) handles won't work because both your DLL and your application are using differenet/separate run time libraries. Solution: Use the "Use MFC in a Shared DLL" setting. Using static run time libraries on Windows is retarded because of exactly this kind of problems.

0
votes

Did you try _dup2? The example in the MSDN documents shows how to redirect stdout. (I don't know if this works at the library level or at the OS level in Windows, so it may not work for the whole process.)

Of course, the DLL may be writing to stderr as well.