Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Determine if program started from command prompt

Aug
1,916
68
I am using;
Code:
TCC  17.00.67  Windows Vista [Version 6.0.6002]
TCC Build 67  Windows Vista Build 6002  Service Pack 2

When I run this code from CMD.EXE, the program returns;
Code:
Started from command prompt

When I run it from TCC, either in TCMD, or in a detached TCC session, it returns;
Code:
NOT started from command prompt

Code:
#COMPILE EXE
#INCLUDE "win32api.inc"

FUNCTION StartedFromConsole() AS LONG
LOCAL lpStartupInfo AS STARTUPINFO
lpStartupInfo.cb = SIZEOF(STARTUPINFO): GetStartupInfo lpStartupInfo
IF lpStartupInfo.dwXSize <> 0 OR lpStartupInfo.dwYSize <> 0 THEN FUNCTION = %TRUE
END FUNCTION

FUNCTION PBMAIN () AS LONG
IF StartedFromConsole = %TRUE THEN
? "Started from command prompt"
ELSE
? "NOT started from command prompt"
SLEEP 2000 '// add a delay here to give the user time to read the console before it ends
END IF
END FUNCTION

Why does this work properly in CMD.EXE, but not in TCC.EXE?

Joe
 
I don't know what "work properly" means in this case. I'd expect very unreliable results from that code. Any parent app, windowed or console, may or may not use STARTF_USESIZE (if it uses CreateProcess at all), and may or may not specify dwXSize or dwYSize as non-zero. Perhaps I've missed something.

I'd guess the best you could do (not perfect) is (1) get the parent process's PID, (2) if (big if) the parent process is still running, get the name of its EXE, and (3) plow through the headers of that EXE to determine if it's a console app or a GUI app.
 
This ought to work also (assuming the parent process still exists).

Get the parent process's PID (CreateToolhelp32Snapshot() perhaps) and then attempt AttachConsole(dwParentPid).
 

Similar threads

Back
Top