Determine if program started from command prompt

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
 
May 20, 2008
11,431
99
Syracuse, NY, USA
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.
 
May 20, 2008
11,431
99
Syracuse, NY, USA
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
Thread starter Title Forum Replies Date
MickeyF how to determine which program will execute a file Support 10
Joe Caverly How to? Determine size of file via download link Support 13
Jay Sage How to Determine INI File Used by TCMD Support 6
C Determine process svchost is running Support 13
M How to? How to determine if this is the first time that... Support 5
C How to determine if system is 32 or 64 bit? Support 5
M How to? Determine a whether vars beginning with certain characters exist... Support 5
C Add link to ewriter help to program group... Support 3
H Behavior change re piping a string to a Java program Support 1
S How to? Runs start /w in invisible mode OR run program after exit of another one Support 3
W Starting program for word to pdf conversion (difference cmd and tcc) Support 13
MickeyF Why doesn't my python program get executed without specifying its path? Support 2
R How to? START program as top window? Support 2
M "less" program that works in TC 18 Support 12
MickeyF running program from script and (not) returning to script Support 12
C INSTALLED returnes same program multiple times Support 14
C Recommend a Print program Support 1
MickeyF running a program from cmdline versus in batch file Support 1
C tcmd.ini not loading from program dir? Support 5
C WAD TCC: (Sys) C:\Program Files\JPSoft\TCMD17\tcc.exe is not a valid Win32 application. Support 6
C Win7 Program Data.... Support 2
M How to? Start a program on log on elevated abovenormal Support 5
D New 64-bit install goes to Program Files x86 Support 3
M How is this program found? Support 9
A How to? Run a TCC command from external program Support 2
C "Program Files" .vs. "Program Files (x86)" Support 2
BobK "do you want to allow the following program to make changes to this computer" Support 3
M How to? Get the return code from an external program... Support 8
C start /wait /pgm "program name" ^ ketstack n/w Support 3
L File association: System cannot find the associated program Support 30
vefatica c:\program? Support 4
D Start program without waiting and redirect output Support 6
nikbackm Alias - launch external program - batch file Support 16
Joe Caverly Program gives error if %_row > 25 Support 0
dcantor Program abort with tab bar Support 2
fpefpe escapes getting into program Support 10
M Invoking a DOS command within a C++ program Support 1
J 4NT Error when running 'system' from a program to execute a .BTM Support 3
C start/tab of a GUI program hangs TCMD Support 6
WadeHatler Just started getting a message about Cloud Storage every time I start TCC 19 Support 3
D Started suggestions, not in this version (yet?) Support 1

Similar threads