Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

WIN32 exec mystery

by major tom (Novice)
on Jul 02, 2002 at 18:10 UTC ( #178936=perlquestion: print w/ replies, xml ) Need Help??
major tom has asked for the wisdom of the Perl Monks concerning the following question:

I only wanted to start an application program from perl, then have the perl console window go away..

Why is / is not the parent process (and console window) killed when:

my $prg='"c:/winnt/notepad.exe"'; #CONSOLE WINDOWS IS NOT CLOSED my $prg='c:/winnt/notepad.exe'; #CONSOLE WINDOWS IS CLOSED


in the script below? I need to use the " quotes because the application program I want to run is located in the c:/program files/... directory and filenames with spaces need to be enclosed with the " quotes in windows. Are there any easier / better way to do this? Is there any way to start the program without showing the console window at all?

The script:

my $prg='"c:/winnt/notepad.exe"'; #CONSOLE WINDOWS IS NOT CLOSED #my $prg='c:/winnt/notepad.exe'; #CONSOLE WINDOWS IS CLOSED if ($pid = fork) { #PARENT-process sleep 5; #WAIT FOR CHILD TO START $prg $killresult=kill 'STOP',$pid; #KILL CHILD print "\nKillresult:$killresult"; } elsif (defined $pid) { #CHILD exec $prg; exit(0); } print "\nKill self (parent, that is) to close CONSOLE window"; kill 'STOP',$$; exit(0);

I also would like to know where to find some info about all supported SIGNALS in win32. This seem to be hard to find on the web? Is there another (better) way to handle interrupts in WIN32 ?

And finally about the SIGNAL handler below:
This handler is called when a CHILD-process exits (right ?). But what does the handler do, exactly? Is the CHILD not already killed and gone when this handler is called?

sub REAPER { $waitedpid=wait; print "\nKilled $waitepid"; $SIG{'CHLD'}=\&REAPER; } $SIG{'CHLD'}=\&REAPER;

Thanks for any help!

Edited: ~Wed Jul 3 17:45:32 2002 (GMT), by footpad:
Added <code> tags

Comment on WIN32 exec mystery
Select or Download Code
Re: WIN32 exec mystery
by Aristotle (Chancellor) on Jul 02, 2002 at 19:19 UTC
    I have neither much experience with Perl on nor access to a Windows machine, so I can not give any specific advice, but I suggest you look into one of the Win32:: modules to do the job you want, specifically Win32::Process.

    Makeshifts last the longest.

Re: WIN32 exec mystery
by Courage (Parson) on Jul 02, 2002 at 20:22 UTC
    I'll try to answer your first part of a question about running programs with/without console window.

    Actually perl internally parses your command to know what to run and which arguments to pass. Hence difference when you add extra quotes.
    Here is a side remark - I dislike "exec" on Win32 platform very much - it seems not to do anything good in any my attempts to use it appropriately.

    And, as I see, you try to start a program without console on windows, and you overcomplicated a task.
    See a discussion at How to 'invoke' a PDF file on Win32 about how similar task was solved. Let me know if you need something different than I understood.

    Courage, the Cowardly Dog

      Firstly , Thanks for input!

      I'm adjusting some settings regarding to the program that I want to start both before it's started and afterwards - when its up and running. It's a GUI-program, so I would like users not to see the console window at all, but that is not highest priority.

      You say quote:
      "Actually perl internally parses your command to know what to run and which arguments to pass. Hence difference when you add extra quotes."

      What does this mean regarding to the console window that won't be killed (or the console windows (not) appearing)? It seems that when I use

      my $prg= ' "c:/winnt/notepad.exe" ';

      and

      exec $prg ;

      in the child process there is some other process starting up, which keep the perl program from ending (and thus closing the console window)?! Tom
Re: WIN32 exec mystery
by ton (Friar) on Jul 02, 2002 at 20:34 UTC
    Weirdness.

    Here's some code that works, using Win32::Process. (Note: I made a copy of notepad.exe in my "Program Files" directory to test spaces.)

    use Win32::Process; my $prgPath = 'c:/program files/notepad.exe'; my $prgArgs = 'notepad.exe'; Win32::Process::Create( $processObj, $prgPath, $prgArgs, 0, NORMAL_PRIORITY_CLASS, ".") || die Win32::FormatMessage(Win32::Get +LastError());
    $prgArgs is (obviously) the arguments that get passed into the program. By convention, the first argument is always the executable name. notepad.exe doesn't seem to care if you pass itself in as the first argument or not, but some other programs do...

    Hope this helps.

    -Ton
    -----
    Be bloody, bold, and resolute; laugh to scorn
    The power of man...

      Ok,
      this will do fine.

      Thanks!

      Also I have found the answer regarding hiding the console window in another posting.

      I love super search, this site and PERL!

      Tom

      Edited: ~Wed Jul 3 16:40:59 2002 (GMT), by footpad:
      Converted HREF hyperlink to id:// link, per Consideration

        Quick followup about hiding console window:

        I found a small utility called ConsoleTool that served me a better soloution than running the script with wperl.exe - because when my script was run this way, the console (dos box) kept comming up every time the script needed to run some dos-batch files.
        Instead I started the script in normal console window (with perl.exe) and from within the script called ConsoleTool. The script goes away (from screen/taskbar) and stays away even when dos-system-commands/batch-files are executed from within my script.

        Nice.

        tom

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://178936]
Approved by Rex(Wrecks)
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (16)
As of 2014-07-31 19:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (252 votes), past polls