views:

150

answers:

2

Hello,

I am trying to redirect the output from my DLL to an external console window for easy debugging.

I have been told about AllocConsole but I am not able to reproduce it, i.e. the console window does not appear.

My current environment is Visual Studio 2005.

I tried the following example which is gotten off the Internet,

AllocConsole();
HANDLE han = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsole(han,"hello",6,new DWORD,0);

yet nothing happens. Can someone point me in the right direction if creating a console window via DLL is possible in the first place.

Thanks in advance!

+1  A: 

Once loaded, there is nothing special about DLLs, so there is no way that allocating consoles would be any different for a DLL than for the EXE that originally loaded it.

Having said that, a process can be associated with only one console at a time, so if there is already a console attached to the process, then allocating a new one is not going to do anything (I assume you're checking the return value of AllocConsole? What does it return? What does GetLastError return?)

There are some other possibilities. For example, if your DLL is loaded into a service, then the service will (likely) be running under a different window station to the currently logged-in user so if you create a console window, you won't be able to see it.

Dean Harding
+2  A: 

The proper way to output debug strings is via OutputDebugString(), with an appropriate debugging tool listening for output strings.

Ignacio Vazquez-Abrams
That, or write it to a file or something. I agree.
Dean Harding
@Ignacio Vazquez-Abrams What sort of debugging tool do you recommend? I do not have the option/privilege to test out with a debugging tool as the application framework is hosted on another server, in which I am not able to perform any installation except the transfer of DLL plugins for testing. Please advice. Thanks! <br>@codeka I could write the output to the file, but then I will have to reopen the file each time there is a change and I am expecting live changes every second.
http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx
Ignacio Vazquez-Abrams