Modifying Process Startup Options

Microsoft® Windows® 2000 Scripting Guide

The Win32_Process Create method enables you to configure startup options for any new process running on a computer. For example, you can configure a process so that it starts in a "hidden" window, which prevents a user from seeing, and possibly interrupting, it. If the process runs in a command window, you can configure the size, title, and foreground and background colors of the window.

Startup options are configured using the Win32_ProcessStartup class. Win32_ProcessStartup is a Method Type class; the Method Type class exists solely to pass information to a method. In this case, all the properties of an instance of Win32_ProcessStartup are passed to an instance of Win32_Process.

To configure a startup option by using Win32_ProcessStartup:

  1. Create an instance of Win32_ProcessStartup.

  2. Configure the properties of the new instance.

  3. Include the instance as part of the Win32_Process Create method.

For example, if you have created a Win32_ProcessStartup instance named objConfig, you would pass the object name in the Create method as follows:

errReturn = objProcess.Create("Database.exe", null, objConfig, intProcessID)

A partial list of startup options that can be configured using the Win32_ProcessStartup class is shown in Table 14.3.

Table 14.3 Process Startup Properties Available Through the Win32_ProcessStartup Class

Startup Option

Description

FillAtrribute

Initial text and background colors if the process runs in a command window. These values are ignored in graphical user interface (GUI) applications.

Valid values are:

1 - Foreground blue

2 - Foreground green

4 - Foreground red

8 - Foreground intensity

16 - Background blue

32 - Background green

64 - Background red

128 Background intensity

To specify both foreground and background colors, add the values together. For example, to have red type (4) on a blue background (16), set the FillAttribute to 20.

PriorityClass

Priority class of the new process (used to determine the scheduling priorities of the threads in the process). Values are:

32 - Normal

64 - Low

128 - High

16384 - Below Normal

32768 - Above Normal

ShowWindow

State of the window as it appears to the user. Valid values are:

1 - Window is shown minimized

3 - Window is shown maximized

5 - Window is shown in normal view

12 - Window is hidden and not displayed to the user

Title

Text to be displayed in the title bar of a command window where the process runs. If not specified, the name of the executable file is used as the window title instead.

This property is ignored by processes that do not create a new command window.

XCountChars

Screen buffer width in character columns. This property is used for processes that create a command window but is ignored in GUI processes.

XSize

Width, in pixels, of the window, if a new window is created.

YCountChars

Screen buffer height in character rows. This property is used for processes that create a command window but is ignored in GUI processes.

YSize

Height, in pixels, of the window if a new window is created.

Scripting Steps

You can use the Win32_ProcessStartup class to configure various startup options for a process. These options include, but are not limited to, such things as creating a process in a hidden window and creating a higher-priority process.

Creating a process in a hidden window

Listing 14.11 contains a script that creates a process in a hidden window. To carry out this task, the script must perform the following steps:

  1. Create a constant named HIDDEN_WINDOW, and set the value to 12. This is the value required to start a process in a hidden window.

  2. Create a variable to specify the computer name.

  3. Use a GetObject call to connect to the WMI namespace root\cimv2 on the computer, and set the impersonation level to "impersonate."

  4. Get an instance of the Win32_ProcessStartup class.

  5. Use the SpawnInstance_ method to create a new, "blank" instance of Win32_ProcessStartup. The new instance, named objConfig, stores the startup properties of the process to be created.

  6. Set the ShowWindow property of the new instance to HIDDEN_WINDOW.

  7. Use a second GetObject call to get an instance of Win32_Process.

  8. Create the new process using the CreateProcess method.

    The first three parameters in the method specify the name of the executable file (such as Database.exe), the name of the startup folder (specifying Null gives the new process the same startup folder as the script that called the process), and the startup properties previously stored in objConfig.

    The fourth parameter (intProcessID) is a variable that represents the Process ID assigned to the new process. You can use this ID to monitor and, if necessary, terminate the process programmatically. This is especially useful for a process that runs in a hidden window and thus provides no visual cue to indicate that it is still running.

Listing 14.11 Creating a Process in a Hidden Window

  
1
2
3
4
5
6
7
8
9
10
Const HIDDEN_WINDOW = 12
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create _
 ("Notepad.exe", null, objConfig, intProcessID)

Creating a higher-priority process

Listing 14.12 contains a script that creates a higher-priority process. To carry out this task, the script must perform the following steps:

  1. Create a constant named ABOVE_NORMAL, and set the value to 32768. This is the value required to start a process with a priority of Above Normal.

  2. Create a variable to specify the computer name.

  3. Use a GetObject call to connect to the WMI namespace root\cimv2 on the computer, and set the impersonation level to "impersonate."

  4. Get an instance of the Win32_ProcessStartup class.

  5. Use the SpawnInstance_ method to create a new, "blank" instance of Win32_ProcessStartup. The new instance, which is used to store the startup properties of the process to be created, is named objConfig.

  6. Set the PriorityClass property of the new instance to ABOVE_NORMAL.

  7. Use a second GetObject call to get an instance of Win32_Process.

  8. Create the new process using the CreateProcess method.

    The first three parameters in the method specify the name of the executable file (Database.exe), the name of the startup folder (specifying Null gives the new process the same startup folder as the script that called the process), and the startup properties previously stored in objConfig.

    The fourth parameter (intProcessID) is a variable that represents the Process ID assigned to the new process. You can use this ID to monitor and, if necessary, terminate the process programmatically.

Listing 14.12 Creating a Higher-Priority Process

  
1
2
3
4
5
6
7
8
9
Const ABOVE_NORMAL = 32768
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.PriorityClass = ABOVE_NORMAL
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
objProcess.Create "Database.exe", Null, objConfig, intProcessID