home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


14.5 Win32::Process

The most flexible way of starting a process on Windows NT is to use the Win32::Process module.[ 3 ] Using this module, you can select whether or not you want to wait for the child process to run to completion, configure priorities, and suspend or resume processes.

[3] Of course, this method won't work at all on non-Windows platforms.

Even though we haven't covered all of the relevant concepts, we are going to run through a Win32::Process example.

As shown, the Win32::Process module contains a method (subroutine) called Create that does all the work of creating a process:

use Win32::Process;
Win32::Process::Create($Process, 
        "c:\\nt\\system32\\notepad.exe",
        "notepad",
        0,
        DETACHED_PROCESS,
        ".") || die "Create: $!";

This code creates an asychronous instance of Notepad . Let's take a look at the parameters. The first parameter $Process is a scalar reference that receives the process object if the call succeeds. We'll discuss references in Chapter 18, CGI Programming , but for now, you can just think of it as a parameter that receives output.

The second argument is a fully qualified (system-dependent) path to the executable. The third argument is the command line passed to the program. In this case, we're just invoking Notepad without any documents or options. The next argument specifies whether or not the new process inherits handles from the parent process (the Perl program). A value of one indicates that the process inherits any inheritable open handle in the parent process. Inheritable handles include I/O handles, socket handles, synchronization handles, and so on. Unless you really know what you're doing here, you're better off specifying this value as zero.

The next argument specifies various create options for the new process. The flag that we've passed is DETACHED_PROCESS , which indicates that the new process does not have access to the console of the calling process (our Perl program). Other flags that you may be interested in include CREATE_SUSPENDED , which creates a process that is initially suspended, and CREATE_SEPARATE_WOW_VDM , which runs a 16-bit process in its own Virtual DOS Machine (VDM). For more information on the various options, see the win32mod documentation for Win32::Process .