473,804 Members | 3,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hi,

(*) I am writing an unmanaged application that hosts managed assemblies (
e.g. CorBindToRuntim eEx ) 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 1709
"Nadav" <Na***@discussi ons.microsoft.c om> wrote in message
news:5C******** *************** ***********@mic rosoft.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)GetStdHan dle( STD_OUTPUT_HAND LE ), 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***@discussi ons.microsoft.c om> wrote in message
news:5C******** *************** ***********@mic rosoft.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)GetStdHan dle( STD_OUTPUT_HAND LE ), 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***@discussi ons.microsoft.c om> wrote in message
news:5C******** *************** ***********@mic rosoft.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)GetStdHan dle( STD_OUTPUT_HAND LE ), 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.Subsyst em

however, console apps (=IMAGE_SUBSYST EM_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
3006
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? I would like to program a Python program that can play for instance the tty version of nethack 3.4.3 on Windows, simulating a human player: for nethack it would not be possible to know if a human or a computer program is playing it. It is a...
1
5389
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 application (VB.NET) will start a process, redirect its stdout and capture that process' output, displaying it in a window. I've written a component for this, and a test application for the component. It allows me to specify a command to execute,...
2
22039
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 using the Console.Write command. The question is how can I see the outputs in the client machine ? Is there a specific programm to do it or Can I use the prompt window ?
7
4396
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 to Console Application. I use C# Standard Edition and I can't change the output type files from Windows Application to Console Application. I can' t find how tho change it although I follow this procedure: Menu Project -> Properties -> Common...
5
11262
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 to avoid the overhead of writing to the console if it is not visible, eg I am running inside a WinForm application. thanks
17
4241
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 console using System.Console.WriteLine if thge project has its "Output Type" to "Windows Application". However I can write to stdio if i set output type to "Console Application". When I do this I unfortunately get a "console box" as well
8
18661
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 available in the same form for users to enter commands and display outputs if some prefer to use character based user interface. I would like to implement the user interface in vb .net. Now I have menus and toolbars ready, but do not now how to get a...
4
10518
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 doesn't seem to write to the screen in the way I would expect. The output is:
6
1724
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 the micq and then act upon them. I've been using the System.Diagnostics.Process class and redirecting the input and output stream. R eading the output works fine, but I am not able to send any commands to the application. Or better, micq doesn't...
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.