473,416 Members | 1,739 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,416 software developers and data experts.

Detect whether I am being run from a Console application ?

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

Barry Mossman
Nov 16 '05 #1
5 11166
You shouldn't write to the console directly from a library, instead, raise an
event and have the .exe that called it either write to the console, or
display a message, depending on whether it is a console or windows app.
"Barry Mossman" wrote:
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

Barry Mossman

Nov 16 '05 #2
Not sure if I remember this right. Don't all windows apps start with a
hidden console?
Detaching the apps default console, and attaching to "thee" Console that
started the WinForm app is a major pain that has some other issues IIRC.
Most folks solve this by distributing two loader apps. MyApp.exe and
MyApp.com in the same directory. If your at the console, myapp.com is found
first unless you explicitly type *.exe. You could also include a "-win"
switch in the console app to start the myapp.exe win app in the same
directory. Some of the MS apps are like this. Naturally people can just
create an icon pathed to myapp.exe to start the win app directly as normal.

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Barry Mossman" <BM************@gmail.com> wrote in message
news:ua**************@TK2MSFTNGP09.phx.gbl...
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

Barry Mossman


Nov 16 '05 #3
"William Stacey [MVP]" wrote:
Not sure if I remember this right. Don't all windows apps start with a
hidden console?
Detaching the apps default console, and attaching to "thee" Console that
started the WinForm app is a major pain that has some other issues IIRC.
Are you sure you're not thinking of Unix there? What you've described is
pretty much how it works for Unix applications that want to run without a
console, but it's definitely not how Windows works.

In Windows, you always have a standard output stream, but whether it's
attached to a console or not is usually determined by some flags in your
executable's header. (I say 'usually' because it is possible to write a
program that launches a process, hooking up whatever it likes as standard
input and output streams, but I assume we're talking about when programs are
launched in the usual way.)

The way it works is that if you are a console application you'll have a
console, if you're not, you won't. You can find out whether a given EXE is
a console app with the 'dumpbin' utility. (It's part of the Win32 SDK.)
Look at the 'subsystem' header value.

If the 'subsystem' is 3 ("Windows CUI") you're a console application. If
it's 2 ("Windows GUI") you're not.

You can't actually detach from the console once you've got it in Windows.
This is the whole reason people end up having to write two executables - a
..COM and a .EXE when they want to support both console and no-console
operation, as you describe here:
Most folks solve this by distributing two loader apps. MyApp.exe and
MyApp.com in the same directory.
If you initially had a console which you later detached from you wouldn't
need two executables. The reason for having two is so you can have one that
gets a console, and one that doesn't.

(Note that while you can create a console and attach your app to it after
launching, it'll be a whole new console window. The only way to attach to
the console of the command prompt from which you were launched is if you are
compiled as a console application.)
To answer one of Barry's original questions:
"Barry Mossman" wrote:
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.
Well if you do a Console.WriteLine and there's no console, it will still
write the text to the standard output stream, whatever that is connected to.
This is because it might still be directed somewhere useful. For example,
WinDBG actually captures stdout and prints it to the debug output window for
applications it debugs. So you wouldn't necessarily want the runtime to
avoid writing console output to the standard output stream - just because
there's no console doesn't mean that output isn't being used by something.

As for the original question:
can I detect whether my class is running within the context of a Console
application, vs say a WinForm's application ?
I don't think you can do this without interop. I've just looked at how
VS.NET 2005 (which has explicit support for interacting with the console
window) works it out, and it just uses the various console APIs. So you
could call the GetConsoleScreenBufferInfo API, for example, although you'd
need to get the raw console handle...

There isn't a managed API to do this.
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/
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.

Nov 16 '05 #4
> Are you sure you're not thinking of Unix there? What you've described is
pretty much how it works for Unix applications that want to run without a


Probably was. I just checked AllocConsole and it states GUI apps are
created without a console as you said. Each process can have one console
only. Many processes can attach to same console. So console.out on GUIss
must point to a null stream (bit bucket) by default and VS probably attaches
a stream to console.out to get the output window?

You can create a new console and attach it to GUI, but that is not want most
folks want. Normally they want to attach to the console that started the
exe. I think I have seen that done in a hackish way, but think it had some
issues after closing console and/or gui or some other strange issues so not
sure there is fool proof way to do it currently. If there was, that sure
would be a nice feature to change in windows. No expert here, but I think
the loader could just place a handle to the current console somewhere so the
windows app could find it *directly if needed and attach to it. I think
this could eliminate the need to create two apps. Please advise if I am
in left field here. tia.

--
William Stacey, MVP
http://mvp.support.microsoft.com
Nov 16 '05 #5
"Ian Griffiths [C# MVP]" <ia*************@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Well if you do a Console.WriteLine and there's no console, it will
still write the text to the standard output stream, whatever that is
connected to. This is because it might still be directed somewhere
useful. For example, WinDBG actually captures stdout and prints it to
the debug output window for applications it debugs. So you wouldn't
necessarily want the runtime to avoid writing console output to the
standard output stream - just because there's no console doesn't mean
that output isn't being used by something.


Thanks for the response. I was writing something to produce trace output
of exceptions. I have attached a Listener to output to a file. If the
application that was using me was going to have a visible console I was
going add a Listener for the console screen.
can I detect whether my class is running within the context of a
Console
application, vs say a WinForm's application ?


I don't think you can do this without interop. I've just looked at how
VS.NET 2005 (which has explicit support for interacting with the
console window) works it out, and it just uses the various console
APIs. So you could call the GetConsoleScreenBufferInfo API, for
example, although you'd need to get the raw console handle...

There isn't a managed API to do this.


Thanks. I may look into this just an interesting exercise.

Barry Mossman
Nov 16 '05 #6

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

Similar topics

1
by: new pip | last post by:
In Windows, when I double click on my .py file, the program runs with a console. How can I detect when the console is closed? Any code samples are appreciated. Thank you.
8
by: Hayato Iriumi | last post by:
Hello, I'm trying to find a way to detect whether CD-ROM was inserted into the CD-ROM drive using C#. Does anyone have any idea how to do this? WMI? Win32 API? TIA
4
by: sanjana | last post by:
hi can anyone help me with this i want to write a code in c#.net which detects usb storage and media card insertion can anyone help me with this?? thanx
6
by: sanjana | last post by:
hi i wanna detect if a anything is connected to the usb port I am using system.management class for tht purpose this is my code class usbdetect { public static void Main() { usbdetect we =...
4
by: Michael D. Ober | last post by:
Is there anyway the VB Compiler can detect if a program is a console application? I have some libraries that need to write to the console if the program is a console app. Thanks, Mike Ober.
8
by: BJ | last post by:
Problem: How can I code up a client side process to detect if the network is available? Synopsis: I am writing ASP.NET input forms for a Panasonic Tuff book. The users will be walking around...
2
by: bz | last post by:
Hi, I need to detect at runtime if the application was started from VS 2005. Is it possible? Also, how can I know at runtime of app is built for Debug or Release? Thanks
0
by: Ontrace | last post by:
hello, I have Problem with Client Server Application when I unplug the client network cable, the server still read the network stream what function should I use to make server detect that the...
3
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgRGVzYXJyb2xsbw==?= | last post by:
Hi all mister, Which is THE BEST WAY IN THE WORLD AROUND for: 1. detect Network
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.