473,326 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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
http://www.ddevel.com
Nov 17 '05 #1
4 1692
"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:5C**********************************@microsof t.com...
(*) 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
Nov 17 '05 #2
ThanX

"William DePalo [MVP VC++]" wrote:
"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:5C**********************************@microsof t.com...
(*) 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

Nov 17 '05 #3
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..... ?

"William DePalo [MVP VC++]" wrote:
"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:5C**********************************@microsof t.com...
(*) 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

Nov 17 '05 #4
> 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)
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Will Stuyvesant | last post by:
I never used the popen or popen2 libraries but it is my understanding that they can capture the output of console based programs. Is it also possible to send keystrokes to console base programs? ...
1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
2
by: Boba | last post by:
Hi, I'm programming a WinForm application. I would like to enter commands that will send output that will help me to locate bugs in the future. I know that there is a way to send output by...
7
by: ajikoe | last post by:
Hello All, It is said that : Enabling the console window is easy. From Microsoft Visual Studio®, right-click on the project and choose Properties. Change the output type from Windows Application...
5
by: Barry Mossman | last post by:
Hi, can I detect whether my class is running within the context of a Console application, vs say a WinForm's application ? also does anyone know whether the compiler or runtime is smart enough...
17
by: MumboJumbo | last post by:
Hi I have a really basic question hopefully some can help me with: Can you write a (i.e. one) C# project that works from the cmd line and gui? I seems if i write a GUI app it can't write to...
8
by: Alison | last post by:
Hi, Al I am trying to design a user interface which provides both menus and toolbars for some users to click on whatever they want to do, at the same time, I would like to have a console window...
4
by: Peter Nimmo | last post by:
Hi, I am writting a windows application that I want to be able to act as if it where a Console application in certain circumstances, such as error logging. Whilst I have nearly got it, it...
6
by: dolulob | last post by:
Hi, I'm trying to communicate with a console application through a c# program. the console application is micq a console based ICQ client. I want to be able to send an receive messages through...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.