Windows Perl (icon v command line)

John Costello cos at indeterminate.net
Sun Dec 11 20:09:27 GMT 2005


On Sun, 11 Dec 2005, Robert Rothenberg wrote:
> On 12/09/2005 10:02 AM Steve Mynott wrote:
> 
> > But I was wondering, and I guess this is more a windows question than
> > perl, if anyone knew of a simple way on Windows of knowing whether the
> > exe had been ran as an icon or via a cmd.exe box?
> 
> I no longer use Windows, but I believe you can check the existence of
> certain environment variables that are instantiated when you are in the
> CMD shell.

You ought to check for three situations, depending on what OS versions 
you plan to run your program.  They are:

- CMD
- COMMAND
- GUI

COMMAND emulates DOS mode and does not support long filenames, among other 
oddities.  

CMD sets an environment variable "ComSpec" which on this XP system is set 
to "C:\Windows\system32\cmd.exe".  COMMAND sets "ComSpec" to 
C:\WINDOWS\SYSTEM32\COMMAND.COM.

Since the system path may not be the same on all versions of Windows
(might be \WinNT instead of \Windows), I'd evaluate $ENV{'ComSpec'} for 
the executable (cmd.exe, command.com) alone.  A case-insensitive match 
would be a safe bet.

I don't know if "ComSpec" exists in the Win95/98/ME OS line.

Before you go to far down the ComSpec path, I'd check to see what the 
value is set to when a GUI program is run.

The 'SESSIONNAME' variable looked promising for a second (it says 
"Console" on my local box, leading me to think that it would distinguish 
between command and GUI), but on an RDP session it is set to "RDP-Tcp#18" 
which isn't that helpful to you.
 
> I also recall there being a module on CPAN which uses this method to
> determine if it should pause when a program halts or not.

-----
John Costello - cos at indeterminate dot net
"You cannot propel yourself forward by patting yourself on the back."--Unknown



More information about the london.pm mailing list