I finally got to run this on a Windows machine...and it didn't work:
ProcessCount:=0;
ProcessCount:=GetConsoleProcessList(ProcessIDs,high(ProcessIDs));
Returns 0 when running from either Explorer or Command Prompt
Returns 0 when running from either Explorer or Command Prompt
GetWindowThreadProcessId(GetConsoleWindow, @pid);
Returns 0 when running from either Explorer or Command Prompt
So, I went off investigating other Windows API calls and found:
This returns the command as run from the command prompt. As it happens, when you double click on an exe in Explorer, this contains the complete path enclosed in quotes. But, when you type in the exe name in command prompt, what you type in is contained here, without quotes (unless you enter the quotes). So, this appears to work:
function IsRunFromConsole: Boolean;
{$IFDEF Windows}
var
cmdLine: String;
{$ENDIF}
{$IFDEF Linux}
var
ParentProcess: String;
{$ENDIF}
{$IFDEF Darwin}
var
path: String;
{$ENDIF}
begin
Result:=False;//Default, if not covered by Windows, Linux or Darwin
{$IFDEF Windows}
cmdLine:=GetCommandLineA;
Result:=(cmdLine[1]<>'"')AND(cmdLine[Length(cmdLine)]<>'"');
{$ENDIF}
{$IFDEF Linux}
ParentProcess:=fpReadLink('/proc/'+fpGetppid.ToString+'/exe');
if ParentProcess<>'' then Result:=True else Result:=False;
{$ENDIF}
{$IFDEF Darwin}
path:=ParamStr(0);
if path[1]='.' then Result:=True else Result:=False;
{$ENDIF}
end;
However, I'm not entirely happy about either the Windows method or the macOS method. There must be another, more confident, way of determining this.
Thoughts anyone?