std console output for windows based app (???)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

(*) I am writing an unmanaged application that hosts managed assemblies (
e.g. CorBindToRuntimeEx ) my app may host window based PEs and Console based
PEs, the host is a single executable and should be able to host the two types
of the PEs.
(*) When Hosting a consol based app ( managed ) I can't see the std output
generated by the app.
(*) The host is compiled as a Win32 project (windows based) so it doesn't
support the std consol output, I wonder how can I enable support of the std
output for Win32 windows based projects?
 
Nadav said:
(*) The host is compiled as a Win32 project (windows based) so it doesn't
support the std consol output, I wonder how can I enable support of the
std
output for Win32 windows based projects?

Yes, you can. The o/s and runtime don't create a console for windowed
application but you can do that at any time with AllocConsole(). If you are
willing to use the console API in windows to write to, and read from, the
console then you are done. If you want to address the console use runtime
try this little bit of voodoo:

#include <io.h>
#include <stdio.h>

void DoIt()
{
int fd;
FILE *fp;

AllocConsole();

fd = _open_osfhandle( (long)GetStdHandle( STD_OUTPUT_HANDLE ), 0);
fp = _fdopen( fd, "w" );

*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );

printf("This is a test");
}

This should work if called from an executable. It is not guaranteed to work
if you put it in a DLL but try to do output from the executable.

Even though it only explicitly wires the Win32 console handle to the C
runtime standard output device you should be able to use C++ I/O streams.

Regards,
Will
 
ThanX

William DePalo said:
Yes, you can. The o/s and runtime don't create a console for windowed
application but you can do that at any time with AllocConsole(). If you are
willing to use the console API in windows to write to, and read from, the
console then you are done. If you want to address the console use runtime
try this little bit of voodoo:

#include <io.h>
#include <stdio.h>

void DoIt()
{
int fd;
FILE *fp;

AllocConsole();

fd = _open_osfhandle( (long)GetStdHandle( STD_OUTPUT_HANDLE ), 0);
fp = _fdopen( fd, "w" );

*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );

printf("This is a test");
}

This should work if called from an executable. It is not guaranteed to work
if you put it in a DLL but try to do output from the executable.

Even though it only explicitly wires the Win32 console handle to the C
runtime standard output device you should be able to use C++ I/O streams.

Regards,
Will
 
Hi Will,

Thanks for your previous response, it is very helpful, still, I have one
issue open: is it possible to know if a certain executable runs as a console
before running it? e.g. browsing it's PE ( headers information ) or so..... ?
 
Thanks for your previous response, it is very helpful, still, I have one
issue open: is it possible to know if a certain executable runs as a console
before running it? e.g. browsing it's PE ( headers information ) or so..... ?


PIMAGE_OPTIONAL_HEADER.Subsystem

however, console apps (=IMAGE_SUBSYSTEM_WINDOWS_CUI = 3) can still spawn
windows, an example is ildasm (works on the commandline but opens a
window if called w/o cmdline parameters)
 
Back
Top