#  Process.pl
#  Example 7.11:
#  ----------------------------------------
#  From "Win32 Perl Scripting: Administrators Handbook" by Dave Roth
#  Published by New Riders Publishing.
#  ISBN # 1-57870-215-1
#
#  This script illustrates using the Win32::Process extension to create and
#  manage a process.
#
print "From the book 'Win32 Perl Scripting: The Administrator's Handbook' by Dave Roth\n\n";


use Win32::Process;
$Timeout = 5;
$File = $0;
$App = "$ENV{SystemRoot}\\notepad.exe";
$Cmd = "notepad $File";
$bInherit = 0;
$Dir = ".";
$Flag = CREATE_SUSPENDED | CREATE_NEW_CONSOLE;
if( Win32::Process::Create( $Process,
                           $App,
                           $Cmd,
                           $bInherit,
                           $Flag,
                           $Dir ) )
{
    $Pid = $Process->GetProcessID();
    print "The process was created in a suspended state with an ID of $Pid.\n";
    print "Now resuming the process...\n";
    while( 1 < $Process->Resume() ){};
    print "Now we will wait for $Timeout seconds for the process to\n";
    print "terminate...\n";
    $Result = $Process->Wait( $Timeout * 1000 );
    if( ! $Result )
    {
        print "The process did not terminate so we will now kill it...\n";
        $Process->Kill( 0 );
    }
    else
    {
        print "The process was terminated by the user.\n";
    }
}
else
{
    print "Unable to create new process.\n";
    print "Error: " . Win32::FormatMessage( Win32::GetLastError() );
}

