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

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?
 
W

William DePalo [MVP VC++]

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
 
G

Guest

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
 
G

Guest

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..... ?
 
B

Ben Schwehn

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top