Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2005-08

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

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Moz
to Unknown author: I use fpc to compile programs intended to be run from the command line in a cmd window…

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

Reply by email

Unknown author

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

Reply by email

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?

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

Reply by email

Marco van de Voort
to Jeff Pohlmeyer
Show quoted text 18 lines
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

Reply by email

Jeff Pohlmeyer
to Marco van de Voort
> > 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?
Show quoted text 13 lines
> {=====================} > 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.
{===========================}

Reply by email

Jeff Miller
to Jeff Pohlmeyer
Show quoted text 22 lines
> > > 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

Reply by email

Jeff Pohlmeyer
to Jeff Miller
Show quoted text 18 lines
> {===========================} > 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

Reply by email