Hello,
Can a normal application (i.e. NOT a console-app) detect that it was invoked from a Dos Box prompt (instead of its icon being double-clicked), and consequently print strings on that Dos Box console?

Greets,
Mav
Posted on 2002-01-21 18:24:47 by Maverick
interesting problem ...

I found something in the knowledge base about "Determining Whether App Is Running as Service or .EXE"

An alternative method is to check the return value from GetConsoleMode().
For example:

ret = GetConsoleMode (GetStdHandle (STD_OUTPUT_HANDLE), &mode);

Although the std handles may exist, they almost certainly will not be

console handles unless there is a console attached. GetConsoleMode() will
fail (with ERROR_INVALID_HANDLE) for non-console handles.

dont know if this helps you, maybe its worth a try
Posted on 2002-01-22 04:14:53 by beaster

interesting problem ...

I found something in the knowledge base about "Determining Whether App Is Running as Service or .EXE"

[..]

dont know if this helps you, maybe its worth a try

Thanks pal, I will work on this direction.

Greets,
Maverick
Posted on 2002-01-22 05:23:04 by Maverick
Sound nice, beaster. Maverick, if you get it working, post a small
"HOWTO:" in the algorithms&source section. There was a similar
question earlier, iirc, but it didn't end up in a solution.
Posted on 2002-01-22 08:06:26 by f0dder

Sound nice, beaster. Maverick, if you get it working, post a small
"HOWTO:" in the algorithms&source section. There was a similar
question earlier, iirc, but it didn't end up in a solution.


Yes, but unfortunately it will take me some days.. have VisualC++ not installed right now, and I was waiting to finish a part of my programming language's compiler to test this there instead.
Posted on 2002-01-22 15:47:50 by Maverick
Hi Beaster & F0dder,

Unfortunately that doesn't work because GetStdHandle(STD_OUTPUT_HANDLE) will work only if the app's PE subsystem was already a console one.. otherwise it will return an error (and -1 as handle).
What would be required to reach our goal is to understand if the app was invoked from a COMMAND/CMD prompt, or from the GUI icon. Any other possible ideas?

Greets,
Maverick
Posted on 2002-03-10 09:01:27 by Maverick
I just did a quick test in C and this code seems to work on my win2k box, it isn't a documentent way though..


STARTUPINFO si;
si.cb = sizeof(si);
GetStartupInfo(&si);
if (si.hStdOutput==(void*)-1)
{
MessageBox(0,"Console","mode",0);
}
else
{
MessageBox(0,"GUI","mode",0);
}


I've included the exe so you can test it on your system.

Thomas

edit: already found a problem. When you run the file right out of the zip, it says 'console', when you unpack it and run it, it says GUI... Same thing when I run it in visual studio.
Posted on 2002-03-10 09:27:49 by Thomas
This one worked in all my test cases:



STARTUPINFO si;
si.cb = sizeof(si);
GetStartupInfo(&si);
if (si.dwFlags&STARTF_USESHOWWINDOW)
{
MessageBox(0,"GUI","mode",0);
}
else
{
MessageBox(0,"Console","mode",0);
}
ExitProcess(0);


Thomas
Posted on 2002-03-10 09:41:58 by Thomas
Thomas: You had a great intuition.. but unfortunately on my WinME box none of them works. So I investigated more, and found that I get always the same identical parameters (from GetStartupInfoA) either I launch the program from a Dos Box or from a double-clicked icon. :(
I didn't test it under Win98 or Win2000 because anyway on WinME it fails, and this makes it un-usable, practically.

BTW: I found a bug in WIN32N.INC:

STRUC STARTUPINFO
.cb RESD 1
.lpReserved RESD 1
.lpDesktop RESD 1
.lpTitle RESD 1
.dwX RESD 1
.dwY RESD 1
.dwXSize RESD 1
.dwYSize RESD 1
.dwXCountChars RESD 1
.dwYCountChars RESD 1
.dwFillAttribute RESD 1
.dwFlags RESD 1
.wShowWindow RESW 1
.cbReserved2 RESW 1
.lpReserved2 RESB 1
.hStdInput RESD 1
.hStdOutput RESD 1
.hStdError RESD 1
ENDSTRUC

.lpReserved2 should be RESD, not RESB. Otherwise a crash is near.

Greets,
Maverick
Posted on 2002-03-10 18:18:02 by Maverick
I experimented with GetActiveWindow, in the hope that it could let us discern between a console window and an Exploder window.. but no hope, it always returns the HWND 0 (i.e. the Desktop?).
Posted on 2002-03-10 18:57:56 by Maverick
GetActiveWindow:

[..]

Return Value

If the function succeeds, the return value is the handle of the active window associated with the thread that calls the function. If the calling thread does not have an active window, the return value is NULL.
That says it all.. an useless function for our purpose :(
Posted on 2002-03-10 19:01:49 by Maverick
Same limitation with GetFocus..

G'Night for now,
Maverick
Posted on 2002-03-10 19:05:28 by Maverick
Hmm same thing on a win98 box here. I've searched usenet and have found several posts saying that it's impossible... :(

Thomas
Posted on 2002-03-11 01:22:23 by Thomas
Hi Thomas :)

Impossible? That makes it more appealing to solve ;)

Just a thought.. maybe EnumWindows (enumerate all top-level windows) would work to find the active window, which will most probably be either a console window or a Explorer's window. What do you think?

Greets,
Mav
Posted on 2002-03-11 16:06:36 by Maverick

Just a thought.. maybe EnumWindows (enumerate all top-level windows) would work to find the active window, which will most probably be either a console window or a Explorer's window. What do you think?
What if I run your console app from a script, or from the task scheduler? There doesn't have to be a console window for a console app?
Posted on 2002-03-11 16:20:13 by bitRAKE
No, the point is that I do not want to make a console app, but a normal app. But, if my app was invoked from a console, I wanna know: so I can output something to it, for example.

That's the whole utility of this "challenge".

Greets,
Maverick
Posted on 2002-03-11 16:29:07 by Maverick
I'm sorry, I was not clear. I meant that I wanted to execute your app with the console functionality from a script or the task scheduler.
Posted on 2002-03-11 16:37:56 by bitRAKE
You are clear.. it's me that don't get it. What kind of script? (I've experience only with BAT files on PC). Also, I don't have clear what is the Task Scheduler (you know, we get localized names of the Windows components on non-English versions of the OS).

Greets,
Mav
Posted on 2002-03-11 16:47:22 by Maverick
Why don't you just make the program so you have to have a commandline, or else is is a GUI app? This is very easy solution. ;)
Posted on 2002-03-11 17:10:30 by bitRAKE
Yes, thought that too.. but why limiting ourselves? :P

For example, I'd like to invoke it from a Dos Box when I want debug output info. That may be useful sometimes.. although, of course, none of this is very important.
Posted on 2002-03-11 17:15:26 by Maverick