News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Started from console?

Started by gfalen, January 03, 2005, 01:30:23 AM

Previous topic - Next topic

gfalen

I have seen several posts asking how to determine if the current process was
started from a dos prompt.  Well here is one solution:


; Returns eax == 0 if NOT started from dos prompt

_inconsole PROC
    local sif:STARTUPINFO, contitle[260]:byte

    invoke GetStartupInfo, addr sif
    invoke GetConsoleTitle, addr contitle, 260
    invoke StrStr, addr contitle, sif.lpTitle    ; from shlwapi.inc

    ; eax== 0 here if new console -or-

    lea edx, contitle
    .if eax == edx    ; found at [console[0] if started from "run" command                               
        and eax, 0
    .endif
    ret
_inconsole endp


donkey

I would think that if the console was created by the app, even if it was started from explorer it would still show as a console app with that method. You can use my system library to tell absolutely, even if it is a GUI only app, that it was started from the console...

invoke GetCurrentProcessId
invoke GetParentPIDNT, eax, offset ParentName (invoke GetParentPID9x, eax, offset ParentName)

If it was started from the console ParentName will be C:\WINNT\system32\cmd.exe, C:\WINNT\Explorer.EXE if it was started from explorer. Otherwise it will return the name of the program that spawned it ie C:\RadASM\RadASM.EXE.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

gfalen

From my experience the .lpTitle of STARTUPINFO is never used if a new console is created.    Works every time for me.

[Edit] There are ways other than explorer to run a program.  What then?

donkey

Hi gfalen,

I will bow to your experience, I have not actually tried it, that was the reason for the "I would think" but if you are confident with it then I will assume it works perfectly.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

gfalen

I have edited my original post.  I found that if started with the "run" command StrStr returns non-zero.  In this case though .lpTitle is found at the 1'st char (contitle[0]) so it is detectable.