Access Denied when I Kill a Process through WMI in C# -
i have console application. have used wmi terminate particular process. when run application in visual studio (ide), process getting terminated successfully.
i have build application , when run exe command prompt access denied.
i running both visual studio , command prompt(cmd.exe) administrator.
public static void wmiprocesshelper(string servername, string processaction) { list<string> resultcode = null; try { connectionoptions connectionoptions = new connectionoptions() { impersonation = impersonationlevel.impersonate, }; managementscope scope = getmanagementscope(root + servername + wmirootnamespace, connectionoptions); scope.connect(); string wmiquery = "select * win32_process name = 'dllhost.exe' , commandline '%/processid:{69f26581-22fb-4a52-9a7a-806760e3cb7d}%'"; objectquery objectquery = new objectquery(wmiquery); managementobjectsearcher searcher = new managementobjectsearcher(scope, objectquery); managementobjectcollection objectcollection = searcher.get(); foreach (managementbaseobject managementbaseobject in objectcollection) { if (resultcode == null) { resultcode = new list<string>(); } managementobject process = (managementobject)managementbaseobject; console.writeline(string.format("{0} process.", processaction)); object returnobject = process.invokemethod(processaction, null); if (returnobject != null) { int returncode; if (int.tryparse(returnobject.tostring(), out returncode)) { console.writeline("return code = " + returncode); //resultcode.add(getprocesserrormessage(returncode)); } } } if (resultcode == null) { console.writeline("no process given properties exists. "); } //return getreturnmessage(resultcode, processaction); } catch (managementexception e) { console.writeline("exception occured: " + e.message); throw; } }`
what have done before this, copied working code. see connectionoptions varies depending on if local machine or not. need @ least set impersionation if used locally only. code connect on wrapper class around wmi.
public void connect(ipaddress address, string username, string password, string subroot) { try { if ((connection != null) || ( scope != null)) throw new alreadyconnectedexception("a wmi connection exists"); connection = new connectionoptions(); if (networkutility.islocalipaddress(address)) { connection.impersonation = system.management.impersonationlevel.impersonate; } else { connection.username = username; connection.password = password; connection.authority = "ntlmdomain:"; } scope = new managementscope("\\\\" + address.tostring() + "\\root\\" + subroot, connection); //connecting remote machine if (!scope.isconnected) scope.connect(); } catch (exception ex) { ex.data.add("address", address.tostring()); ex.data.add("username", username); ex.data.add("password", password); ex.data.add("wmi namespace", subroot); throw; } }
this terminate code. dont see conceptual difference doing.
public void terminateexecutingexe(string filename) { try { if ((scope == null)) throw new notconnectedexception("no wmi connection exists"); objectquery objobjectquery = new objectquery("select * win32_process name = '"+filename+"'"); managementobjectsearcher objsearcher = new managementobjectsearcher(scope, objobjectquery); foreach (managementobject queryobj in objsearcher.get()) { if (queryobj["name"].tostring().tolower() == filename.tolower()) { object[] obj = new object[] { 0 }; queryobj.invokemethod("terminate", obj); } } objsearcher = null; objobjectquery = null; } catch (exception ex) { ex.data.add("filename", filename); throw; } }
Comments
Post a Comment