Win32 - forked process doesn't create a Window

Discussion in 'Perl' started by RL, Feb 2, 2004.

  1. RL

    RL Guest

    Hello Perl gurus,

    1. I have a web page where I can push a button (dospawn.html).
    2. This button calls a CGI script (spawnboss.cgi)
    3. spawnboss.cgi calls a forking perl script (forkme.pl)
    4. forkme.pl calls the process creation script (createme.pl)
    5. createme.pl creates my notepad.exe process, but no window shows up on my
    PC.

    The result on my web browser is:
    SPAWNBOSS: Running perl forkme.pl C:\Windows\system32 notepad.exe
    FORKME: Forking perl createme.pl C:\Windows\system32 notepad.exe
    CREATEME: Starting C:\Windows\system32\notepad.exe
    CREATEME: process notepad.exe is running

    When I kill the notepad.exe from my Task Manager,
    I then get on my web browser the final output:
    CREATEME: process notepad.exe was ended

    The only thing missing is the graphical window for NOTEPAD so I can type
    into it.

    Thanks for your assistance :)
    Rob

    Details following...

    Have the latest installed version of ActivePerl 5.8.2.
    The web page is on my personal Apache web server 2.0.47.
    http://localhost/~test/dospawn.html
    Configured for CGI with:
    =====================================
    LoadModule cgi_module modules/mod_cgi.so
    ScriptAlias /cgi-bin/ "D:/Apps/Apache2/cgi-bin/"
    UserDir "D:\Web\USERS"
    <Directory "D:/Web/USERS/test/cgi-bin/">
    AllowOverride None
    Options ExecCGI
    Order allow,deny
    Allow from all
    </Directory>
    AddHandler cgi-script .cgi .pl
    =====================================

    The files are as follows:
    =====================================
    DOSPAWN.HTML
    <html><body>
    <form action="cgi-bin/spawnboss.cgi?action=spawn" method="post"
    enctype="multipart/form-data">
    <input type=hidden name="action" value="spawn">
    <input type="submit" value="Spawn NOTEPAD">
    </form>
    </body></html>
    =====================================
    SPAWNBOSS.CGI
    #!d:/apps/perl/bin/perl.exe
    use CGI qw/:standard :html3/;
    &spawn if (param('action') eq 'spawn'); # Spawn a process
    exit;
    sub spawn {
    print "Content-type: text/html\n\n";
    print "<HTML><BODY>\n";
    $command = "perl forkme.pl C:\\Windows\\system32 notepad.exe";
    print "SPAWNBOSS: Running $command<br>\n";
    system("$command");
    print "</BODY></HTML>\n";
    return;
    }
    =====================================
    FORKME.PL
    #!d:/apps/perl/bin/perl.exe
    $command = "perl createme.pl $ARGV[0] $ARGV[1]";
    if ($pid = fork) {
    print "FORKME: Forking $command<br>\n";
    exec "$command";
    die "couldn't exec $command : $!";
    }
    else {
    die "DO_RUNME: fork failed: $!";
    }
    exit;
    =====================================
    CREATEME.PL
    #!d:/apps/perl/bin/perl.exe
    use Win32::process;
    use IO::Handle;
    STDOUT->autoflush(1);
    STDERR->autoflush(1);
    sub ErrorReport{
    print Win32::FormatMessage( Win32::GetLastError() );
    }
    print("CREATEME: Starting $ARGV[0]\\$ARGV[1]<br>\n");
    Win32::process::Create($ProcessObj,
    "$ARGV[0]\\$ARGV[1]",
    "$ARGV[1]",
    1,
    NORMAL_PRIORITY_CLASS,
    "$ARGV[0]\\")|| die ErrorReport();
    print("CREATEME: process $ARGV[1] is running<br>\n");
    $ProcessObj->Wait(INFINITE);
    print("CREATEME: process $ARGV[1] was ended<br>\n");
    exit;
    =====================================
    The END
     
    RL, Feb 2, 2004
    #1
    1. Advertising

  2. RL

    Jim Gibson Guest

    In article <rPhTb.2043$>, RL
    <> wrote:

    > Hello Perl gurus,
    >
    > 1. I have a web page where I can push a button (dospawn.html).
    > 2. This button calls a CGI script (spawnboss.cgi)
    > 3. spawnboss.cgi calls a forking perl script (forkme.pl)
    > 4. forkme.pl calls the process creation script (createme.pl)
    > 5. createme.pl creates my notepad.exe process, but no window shows up on my
    > PC.
    >
    > The result on my web browser is:
    > SPAWNBOSS: Running perl forkme.pl C:\Windows\system32 notepad.exe
    > FORKME: Forking perl createme.pl C:\Windows\system32 notepad.exe
    > CREATEME: Starting C:\Windows\system32\notepad.exe
    > CREATEME: process notepad.exe is running
    >
    > When I kill the notepad.exe from my Task Manager,
    > I then get on my web browser the final output:
    > CREATEME: process notepad.exe was ended
    >
    > The only thing missing is the graphical window for NOTEPAD so I can type
    > into it.
    >
    > Thanks for your assistance :)
    > Rob


    Since I don't work on Windows, I am afraid I can't help you too much. I
    can, however, point out some oddities in your program.

    >
    > Details following...
    >
    > Have the latest installed version of ActivePerl 5.8.2.
    > The web page is on my personal Apache web server 2.0.47.
    > http://localhost/~test/dospawn.html
    > Configured for CGI with:
    > =====================================
    > LoadModule cgi_module modules/mod_cgi.so
    > ScriptAlias /cgi-bin/ "D:/Apps/Apache2/cgi-bin/"
    > UserDir "D:\Web\USERS"
    > <Directory "D:/Web/USERS/test/cgi-bin/">
    > AllowOverride None
    > Options ExecCGI
    > Order allow,deny
    > Allow from all
    > </Directory>
    > AddHandler cgi-script .cgi .pl
    > =====================================
    >
    > The files are as follows:
    > =====================================
    > DOSPAWN.HTML
    > <html><body>
    > <form action="cgi-bin/spawnboss.cgi?action=spawn" method="post"
    > enctype="multipart/form-data">
    > <input type=hidden name="action" value="spawn">
    > <input type="submit" value="Spawn NOTEPAD">
    > </form>
    > </body></html>
    > =====================================
    > SPAWNBOSS.CGI
    > #!d:/apps/perl/bin/perl.exe
    > use CGI qw/:standard :html3/;
    > &spawn if (param('action') eq 'spawn'); # Spawn a process
    > exit;
    > sub spawn {
    > print "Content-type: text/html\n\n";
    > print "<HTML><BODY>\n";
    > $command = "perl forkme.pl C:\\Windows\\system32 notepad.exe";
    > print "SPAWNBOSS: Running $command<br>\n";
    > system("$command");
    > print "</BODY></HTML>\n";
    > return;
    > }
    > =====================================
    > FORKME.PL
    > #!d:/apps/perl/bin/perl.exe
    > $command = "perl createme.pl $ARGV[0] $ARGV[1]";
    > if ($pid = fork) {
    > print "FORKME: Forking $command<br>\n";
    > exec "$command";
    > die "couldn't exec $command : $!";
    > }
    > else {
    > die "DO_RUNME: fork failed: $!";
    > }
    > exit;


    fork() returns a non-zero pid (that of the child) to the parent and
    returns zero to the child. Thus, you are exec'ing from the parent
    process and the child just dies with an error message. You can probably
    eliminate the fork and this entire script, since you are not using the
    child process. Do you want the parent process to return the completed
    web page to the browser while notepad.exe runs? Then you should run
    notepad.exe from the child.


    > =====================================
    > CREATEME.PL
    > #!d:/apps/perl/bin/perl.exe
    > use Win32::process;
    > use IO::Handle;
    > STDOUT->autoflush(1);
    > STDERR->autoflush(1);
    > sub ErrorReport{
    > print Win32::FormatMessage( Win32::GetLastError() );
    > }
    > print("CREATEME: Starting $ARGV[0]\\$ARGV[1]<br>\n");
    > Win32::process::Create($ProcessObj,
    > "$ARGV[0]\\$ARGV[1]",
    > "$ARGV[1]",
    > 1,
    > NORMAL_PRIORITY_CLASS,
    > "$ARGV[0]\\")|| die ErrorReport();
    > print("CREATEME: process $ARGV[1] is running<br>\n");
    > $ProcessObj->Wait(INFINITE);
    > print("CREATEME: process $ARGV[1] was ended<br>\n");
    > exit;
    > =====================================
    > The END
    >


    Another tip: make sure you can run your CGI script from the command
    line before you try to execute it from the web server, if you have
    command line access to the server.

    FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future
    for better response.
     
    Jim Gibson, Feb 6, 2004
    #2
    1. Advertising

  3. RL

    RL Guest

    Hi Jim,

    You are correct, I don't need the fork.

    Also, just realized I forgot to add the createme.pl program
    =====================================
    CREATEME.PL
    #!d:/apps/perl/bin/perl.exe
    use Win32::process;
    use IO::Handle;
    STDOUT->autoflush(1);
    STDERR->autoflush(1);
    sub ErrorReport{
    print Win32::FormatMessage( Win32::GetLastError() );
    }
    print("CREATEME: Starting $ARGV[0]\\$ARGV[1]<br>\n");
    Win32::process::Create($ProcessObj,
    "$ARGV[0]\\$ARGV[1]",
    "$ARGV[1]",
    1,
    NORMAL_PRIORITY_CLASS,
    "$ARGV[0]\\")|| die ErrorReport();
    print("CREATEME: process $ARGV[1] is running<br>\n");
    $ProcessObj->Wait(INFINITE);
    print("CREATEME: process $ARGV[1] was ended<br>\n");
    exit;
    =====================================

    "Jim Gibson" <> wrote in message
    news:050220041717002619%...
    > In article <rPhTb.2043$>, RL
    > <> wrote:
    >
    > > Hello Perl gurus,
    > >
    > > 1. I have a web page where I can push a button (dospawn.html).
    > > 2. This button calls a CGI script (spawnboss.cgi)
    > > 3. spawnboss.cgi calls a forking perl script (forkme.pl)
    > > 4. forkme.pl calls the process creation script (createme.pl)
    > > 5. createme.pl creates my notepad.exe process, but no window shows up on

    my
    > > PC.
    > >
    > > The result on my web browser is:
    > > SPAWNBOSS: Running perl forkme.pl C:\Windows\system32 notepad.exe
    > > FORKME: Forking perl createme.pl C:\Windows\system32 notepad.exe
    > > CREATEME: Starting C:\Windows\system32\notepad.exe
    > > CREATEME: process notepad.exe is running
    > >
    > > When I kill the notepad.exe from my Task Manager,
    > > I then get on my web browser the final output:
    > > CREATEME: process notepad.exe was ended
    > >
    > > The only thing missing is the graphical window for NOTEPAD so I can type
    > > into it.
    > >
    > > Thanks for your assistance :)
    > > Rob

    >
    > Since I don't work on Windows, I am afraid I can't help you too much. I
    > can, however, point out some oddities in your program.
    >
    > >
    > > Details following...
    > >
    > > Have the latest installed version of ActivePerl 5.8.2.
    > > The web page is on my personal Apache web server 2.0.47.
    > > http://localhost/~test/dospawn.html
    > > Configured for CGI with:
    > > =====================================
    > > LoadModule cgi_module modules/mod_cgi.so
    > > ScriptAlias /cgi-bin/ "D:/Apps/Apache2/cgi-bin/"
    > > UserDir "D:\Web\USERS"
    > > <Directory "D:/Web/USERS/test/cgi-bin/">
    > > AllowOverride None
    > > Options ExecCGI
    > > Order allow,deny
    > > Allow from all
    > > </Directory>
    > > AddHandler cgi-script .cgi .pl
    > > =====================================
    > >
    > > The files are as follows:
    > > =====================================
    > > DOSPAWN.HTML
    > > <html><body>
    > > <form action="cgi-bin/spawnboss.cgi?action=spawn" method="post"
    > > enctype="multipart/form-data">
    > > <input type=hidden name="action" value="spawn">
    > > <input type="submit" value="Spawn NOTEPAD">
    > > </form>
    > > </body></html>
    > > =====================================
    > > SPAWNBOSS.CGI
    > > #!d:/apps/perl/bin/perl.exe
    > > use CGI qw/:standard :html3/;
    > > &spawn if (param('action') eq 'spawn'); # Spawn a process
    > > exit;
    > > sub spawn {
    > > print "Content-type: text/html\n\n";
    > > print "<HTML><BODY>\n";
    > > $command = "perl forkme.pl C:\\Windows\\system32 notepad.exe";
    > > print "SPAWNBOSS: Running $command<br>\n";
    > > system("$command");
    > > print "</BODY></HTML>\n";
    > > return;
    > > }
    > > =====================================
    > > FORKME.PL
    > > #!d:/apps/perl/bin/perl.exe
    > > $command = "perl createme.pl $ARGV[0] $ARGV[1]";
    > > if ($pid = fork) {
    > > print "FORKME: Forking $command<br>\n";
    > > exec "$command";
    > > die "couldn't exec $command : $!";
    > > }
    > > else {
    > > die "DO_RUNME: fork failed: $!";
    > > }
    > > exit;

    >
    > fork() returns a non-zero pid (that of the child) to the parent and
    > returns zero to the child. Thus, you are exec'ing from the parent
    > process and the child just dies with an error message. You can probably
    > eliminate the fork and this entire script, since you are not using the
    > child process. Do you want the parent process to return the completed
    > web page to the browser while notepad.exe runs? Then you should run
    > notepad.exe from the child.
    >
    >
    > > =====================================
    > > CREATEME.PL
    > > #!d:/apps/perl/bin/perl.exe
    > > use Win32::process;
    > > use IO::Handle;
    > > STDOUT->autoflush(1);
    > > STDERR->autoflush(1);
    > > sub ErrorReport{
    > > print Win32::FormatMessage( Win32::GetLastError() );
    > > }
    > > print("CREATEME: Starting $ARGV[0]\\$ARGV[1]<br>\n");
    > > Win32::process::Create($ProcessObj,
    > > "$ARGV[0]\\$ARGV[1]",
    > > "$ARGV[1]",
    > > 1,
    > > NORMAL_PRIORITY_CLASS,
    > > "$ARGV[0]\\")|| die ErrorReport();
    > > print("CREATEME: process $ARGV[1] is running<br>\n");
    > > $ProcessObj->Wait(INFINITE);
    > > print("CREATEME: process $ARGV[1] was ended<br>\n");
    > > exit;
    > > =====================================
    > > The END
    > >

    >
    > Another tip: make sure you can run your CGI script from the command
    > line before you try to execute it from the web server, if you have
    > command line access to the server.
    >
    > FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future
    > for better response.
     
    RL, Feb 6, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    427
    Chris Croughton
    Apr 13, 2005
  2. John Lin
    Replies:
    19
    Views:
    571
    Donn Cave
    Sep 25, 2003
  3. James Colannino

    Detecting problems in a forked process

    James Colannino, Dec 29, 2005, in forum: Python
    Replies:
    2
    Views:
    311
    Donn Cave
    Dec 30, 2005
  4. Qi Lu
    Replies:
    0
    Views:
    406
    Qi Lu
    Jul 14, 2008
  5. Mike Stead
    Replies:
    3
    Views:
    271
    Roger Pack
    Mar 25, 2010
Loading...

Share This Page