[fpc-pascal] Re: Can a program find out how it was started?

Jeff Pohlmeyer yetanothergeek at gmail.com
Thu Aug 4 03:12:46 CEST 2005


> > > Is it possible for a program to tell whether it was started 
> > > from the command line or started by double-clicking on the 
> > > application icon?


> > {=====================}
> > program envtest;
> > uses Dos;
> > var
> >   F:text;
> >   i:LongInt;
> > begin
> >   assign(F, 'envlist.txt');  
> >   rewrite(F);
> >   for i:=0 to EnvCount-1 do WriteLn(F, EnvStr(i));
> >   close(F);
> > end.
> > {=====================}


> I'd try to search the win32 api, and find a way to find the parent of my process,
> either by a direct call, or enumerating all processes. And then check if it is 
> cmd.exe or command.com or explorer.exe

Indeed, I guess I am still geared to thinking "command line" means
"dos mode" :-)

But I am still not sure that simply checking the name of the parent process is
a reliable way to test for a console. If you want to use Win API calls, I think 
this would be a better (and simpler) way to do it:


{===========================}
program contest;
uses windows;

var
  info:tSTARTUPINFO;

begin
  GetStartupInfo(@info);
  if ( info.hStdInput = 0 ) 
  then {Started from Explorer} 
  else {Started from Console}
end.
{===========================}




More information about the fpc-pascal mailing list