Can a program find out how it was started?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Can a program find out how it was started?

Jeff Miller
Greetings from New Zealand,

I use fpc to compile programs intended to be run from the command line in a cmd
window (Windows 2000, etc).  Sometimes, though, new users try to start the
programs by double-clicking on the exe file (application icon). When the
program starts, I would like to be able to tell if they have done that,
so that I can present a special error message.

So my question is:
  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?

I tried checking ParamStr(0) and DOSVersion, but these have the same values
regardless of how the program was started.

Any suggestions?

Thank you for your time,

Jeff Miller

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Moz
Reply | Threaded
Open this post in threaded view
|

Re: Can a program find out how it was started?

Moz
[hidden email] said:
> Sometimes, though, new users try to start the programs by
> double-clicking on the exe file (application icon). When the program
> starts, I would like to be able to tell if they have done that, so
> that I can present a special error message.

One trivial solution is to drop a readline at the end of the help
routine. That way the cmd window stays open until they hit enter
(which you should tell them to do). Mildly annoying for people at the
command line if there's only a few lines of help, but solves the
problem.

Moz


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Can a program find out how it was started?

Jeff Pohlmeyer
In reply to this post by Jeff Miller
> 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?

Under Win98, there are some subtle differences in the output of the program
below, when run from a DOS window  vs.  double-clicking the icon.
I don't know if this will work for NT-based OS, though....

{=====================}
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.
{=====================}

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re: Can a program find out how it was started?

Marco van de Voort
>
> Under Win98, there are some subtle differences in the output of the program
> below, when run from a DOS window  vs.  double-clicking the icon.
> I don't know if this will work for NT-based OS, though....
>
> {=====================}
> 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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re: Can a program find out how it was started?

Jeff Pohlmeyer
> > > 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.
{===========================}

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re: Can a program find out how it was started?

Jeff Miller
> > > > 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?
 

> 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.
> {===========================}

This looks really attractive, but it doesn't work.
I get the same value of info.hStdInput (4294967295)
whether I run the program from the console or explorer.

So far I have been unable to find any information
on the fields of tSTARTUPINFO, so I can't figure out
how to elaborate on this idea.  (Google gives lots of hits, but they all
just seem to use tSTARTUPINFO, not to document it.)
If you have any further guidance on that, I'd appreciate it.

Thanks for your efforts, in any case.

Jeff Miller



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re: Can a program find out how it was started?

Jeff Pohlmeyer
> > {===========================}
> > program contest;
> > uses windows;
> >
> > var
> >   info:tSTARTUPINFO;
> >
> > begin
> >   GetStartupInfo(@info);
> >   if ( info.hStdInput = 0 )
> >   then {Started from Explorer}
> >   else {Started from Console}
> > end.
> > {===========================}

> This looks really attractive, but it doesn't work.
> I get the same value of info.hStdInput (4294967295)
> whether I run the program from the console or explorer.

Sorry, I tested in Win98 and it works fine, but Win2k is a
whole different animal.


> So far I have been unable to find any information on
> the fields of STARTUPINFO, so I can't figure out how
> to elaborate on this idea.
> If you have any further guidance on that, I'd appreciate it.

Maybe this will help:
http://msdn.microsoft.com/library/en-us/dllproc/base/startupinfo_str.asp
http://msdn.microsoft.com/library/en-us/dllproc/base/getstartupinfo.asp

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal