//Get ManagementObject for process
queryCollection = getProcessCollection("Select * from Win32_Process Where ProcessID = ''" + ProcessID + "''");
//Status
updateStatus("Invoking terminate process");
foreach ( ManagementObject mo in queryCollection)
{
//start or stop service
mo.InvokeMethod(observer, "Terminate", null);
}
//wait until invoke method is complete or 5 sec timeout
int intCount = 0;
while (!completionHandlerObj.IsComplete)
{
if (intCount == 10)
{
MessageBox.Show("Terminate process timed out.", "Terminate Process Status");
break;
}
//wait 1/2 sec.
System.Threading.Thread.Sleep(500);
//increment counter
intCount++;
}
if (intCount != 10)
{
//InvokeMethod did not time out
if (completionHandlerObj.ReturnObject.Properties["returnValue"].Value.ToString() == "0")
{
lvItem = ProcessItem;
lvItem.Remove();
}
else
{
MessageBox.Show("Error terminating process.", "Terminate Process");
}
}
创建进程:
创建一个新的进程我们需要调用ManagementClass类的InvokeMethod方法来完成
我们可以通过ManagementClass processClass = New ManagementClass(ms,path,null);
这条语句来实现一个ManagementClass对象.ms是一个ManagementScope类的实例;
,path是一个ManagementPath的实例.ManagementScope来设置Management的范围.
ManagementPath用来提供一个包装,用于分析和生成 WMI 对象的路径.("Win32_Process")
在这之前我们还需要调用ManagementClass.InvokeMethod(observer, methodName, inParameters).
我们可以通过一个对象数组来一次传递四个参数.inParameters实际上就是一个
Create Method in Class Win32_Process(参考Platform SDK: Windows Management Instrumentation)
uint32 Create(string CommandLine,
string CurrentDirectory,
Win32_ProcessStartup ProcessStartupInformation,
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 creat