Cannot complete the process using WMI, but taskkill works

My user is in the Administrators group. I am using the .NET WMI API (System.Management) to kill a process using this code:

var scope = new ManagementScope("\root\cimv2"); scope.Connect(); var query = new ObjectQuery( string.Format("Select * from Win32_Process Where ProcessID = {0}", processId)); var searcher = new ManagementObjectSearcher(scope, query); var coll = searcher.Get().GetEnumerator(); coll.MoveNext(); var mgmtObj = (ManagementObject)coll.Current; var ret = (uint) mgmtObj.InvokeMethod("Terminate"); // ret == 2 here, meaning "Access Denied" 

Cannot kill process and return 2 ( Access Denied ). However, if I use:

 Process.Start("cmd", string.Format("/c \"taskkill /f /pid {0}\"", processId)); 

the process is being killed. (but if I go down on /f , it fails).

Is there a way to complete the process using WMI?

EDIT: I found the following: http://msdn.microsoft.com/en-us/library/windows/desktop/aa393907(v=vs.85).aspx :

To complete the process that you do not have, enable the SeDebugPrivilege Privilege.

The page contains VBScript code, but how to do it in C #?

+1
source share
1 answer

This is only possible with some API calls that are only available through pinvoke - for full C # source code see here .

+3
source

All Articles