Upravit

Sdílet prostřednictvím


Create method of the Win32_Process class

The Create WMI class method creates a new process.

This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.

Syntax

uint32 Create(
  [in]  string               CommandLine,
  [in]  string               CurrentDirectory,
  [in]  Win32_ProcessStartup ProcessStartupInformation,
  [out] uint32               ProcessId
);

Parameters

CommandLine [in]

Command line to execute. The system adds a null character to the command line, trimming the string if necessary, to indicate which file was actually used.

CurrentDirectory [in]

Current drive and directory for the child process. The string requires that the current directory resolves to a known path. A user can specify an absolute path or a path relative to the current working directory. If this parameter is NULL, the new process will have the same path as the calling process. This option is provided primarily for shells that must start an application and specify the application's initial drive and working directory.

ProcessStartupInformation [in]

The startup configuration of a Windows process. For more information, see Win32_ProcessStartup.

ProcessId [out]

Global process identifier that can be used to identify a process. The value is valid from the time the process is created until the time the process is terminated.

Return value

Returns a value of 0 (zero) if the process was successfully created, and any other number to indicate an error. For additional error codes, see WMI Error Constants or WbemErrorEnum. For general HRESULT values, see System Error Codes.

Successful completion (0)

Access denied (2)

Insufficient privilege (3)

Unknown failure (8)

Path not found (9)

Invalid parameter (21)

Other (22 4294967295)

Remarks

You can create an instance of the Win32_ProcessStartup class to configure the process before calling this method.

A fully qualified path must be specified in cases where the program to be launched is not in the search path of Winmgmt.exe. If the newly created process attempts to interact with objects on the target system without the appropriate access privileges, it is terminated without notification to this method.

For security reasons the Win32_Process.Create method cannot be used to start an interactive process remotely.

Processes created with the Win32_Process.Create method are limited by the job object unless the CREATE_BREAKAWAY_FROM_JOB flag is specified. For more information, see Win32_ProcessStartup and __ProviderHostQuotaConfiguration.

Examples

The following VBScript example demonstrates how to invoke a CIM method as if it were an automation method of SWbemObject.

on error resume next

set process = GetObject("winmgmts:Win32_Process")

result = process.Create ("notepad.exe",null,null,processid)

WScript.Echo "Method returned result = " & result
WScript.Echo "Id of new process is " & processid

if err <>0 then
 WScript.Echo Err.Description, "0x" & Hex(Err.Number)
end if

The following Perl example demonstrates how to invoke a CIM method as if it were an automation method of SWbemObject.

use strict;
use Win32::OLE;

my ($process, $outParam, $processid, $inParam, $objMethod);

eval { $process = 
 Win32::OLE->GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2:Win32_Process"); };

if (!$@ && defined $process)
{
 $objMethod = $process->Methods_("Create");

 #Spawn an instance of inParameters and assign the values.
 $inParam = $objMethod->inParameters->SpawnInstance_ if (defined $objMethod);
 $inParam->{CommandLine} = "notepad.exe";
 $inParam->{CurrentDirectory} = undef;
 $inParam->{ProcessStartupInformation} = undef;

 $outParam = $process->ExecMethod_("Create", $inParam) if (defined $inParam);
 if ($outParam->{ReturnValue})
 {
  print STDERR Win32::OLE->LastError, "\n";
 }
 else
 {
  print "Method returned result = $outParam->{ReturnValue}\n";
  print "Id of new process is $outParam->{ProcessId}\n"
 }
}
else
{
 print STDERR Win32::OLE->LastError, "\n";
}

The following VBScript code example creates a Notepad process on the local computer. Win32_ProcessStartup is used to configure the process settings.

Const SW_NORMAL = 1
strComputer = "."
strCommand = "Notepad.exe" 
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")

' Configure the Notepad process to show a window
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = SW_NORMAL

' Create Notepad process
Set objProcess = objWMIService.Get("Win32_Process")
intReturn = objProcess.Create _
    (strCommand, Null, objConfig, intProcessID)
If intReturn <> 0 Then
    Wscript.Echo "Process could not be created." & _
        vbNewLine & "Command line: " & strCommand & _
        vbNewLine & "Return value: " & intReturn
Else
    Wscript.Echo "Process created." & _
        vbNewLine & "Command line: " & strCommand & _
        vbNewLine & "Process ID: " & intProcessID
End If

Requirements

Requirement Value
Minimum supported client
Windows Vista
Minimum supported server
Windows Server 2008
Namespace
Root\CIMV2
MOF
CIMWin32.mof
DLL
CIMWin32.dll

See also

Operating System Classes

Win32_Process

WMI Tasks: Processes