can't terminate process using WMI but taskkill works

丶灬走出姿态 提交于 2019-12-21 21:43:36

问题


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"

It's failing to kill the process and returning a 2 (Access Denied). However if I use:

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

the process gets killed. (but if I leave out /f it fails).

Is there any way to terminate the process using WMI?

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

To terminate a process that you do not own, enable the SeDebugPrivilege privilege.

The page provides VBScript code but how would I do this in C#?


回答1:


That is only possible with some API calls which are only available via pinvoke - for complete C# source code see here.




回答2:


Terminate process in command line need Running as administrator to avoid access denied error message .



来源:https://stackoverflow.com/questions/16284511/cant-terminate-process-using-wmi-but-taskkill-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!