Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For kill a process in a remote machine you can use the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa394372%28v=vs.85%29.aspx" rel="nofollow"><code>Win32_Process</code></a> WMI class and the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa393907%28v=vs.85%29.aspx" rel="nofollow"><code>Terminate</code></a> method and for stop a remote service you can use the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa394418%28v=vs.85%29.aspx" rel="nofollow"><code>Win32_Service</code></a> and the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa393673%28v=vs.85%29.aspx" rel="nofollow"><code>StopService</code></a> method.</p> <p>Here is a C++ sample:</p> <pre><code>#include "stdafx.h" #define _WIN32_DCOM #include &lt;iostream&gt; using namespace std; #include &lt;comdef.h&gt; #include &lt;Wbemidl.h&gt; # pragma comment(lib, "wbemuuid.lib") //CREDENTIAL structure //http://msdn.microsoft.com/en-us/library/windows/desktop/aa374788%28v=vs.85%29.aspx #define CRED_MAX_USERNAME_LENGTH 513 #define CRED_MAX_CREDENTIAL_BLOB_SIZE 512 #define CREDUI_MAX_USERNAME_LENGTH CRED_MAX_USERNAME_LENGTH #define CREDUI_MAX_PASSWORD_LENGTH (CRED_MAX_CREDENTIAL_BLOB_SIZE / 2) // The Terminate method terminates a process and all of its threads. The method returns an integer value that can be interpretted as follows: // 0 - Successful completion. // 2 - The user does not have access to the requested information. // 3 - The user does not have sufficient privilge. // 8 - Unknown failure. // 9 - The path specified does not exist. // 21 - The specified parameter is invalid. // Other - For integer values other than those listed above, refer to Win32 error code documentation. // // Note: The SE_DEBUG_PRIVILEGE privilege is required to invoke this method #pragma argsused int main(int argc, char* argv[]) { wchar_t pszName[CREDUI_MAX_USERNAME_LENGTH+1] = L"user"; wchar_t pszPwd[CREDUI_MAX_PASSWORD_LENGTH+1] = L"password"; BSTR strNetworkResource; //To use a WMI remote connection set localconn to false and configure the values of the pszName, pszPwd and the name of the remote machine in strNetworkResource bool localconn = true; strNetworkResource = localconn ? L"\\\\.\\root\\CIMV2" : L"\\\\remote--machine\\root\\CIMV2"; COAUTHIDENTITY *userAcct = NULL ; COAUTHIDENTITY authIdent; HRESULT hres; // Initialize COM. ------------------------------------------ hres = CoInitializeEx(0, COINIT_MULTITHREADED); if (FAILED(hres)) { cout &lt;&lt; "Failed to initialize COM library. Error code = 0x" &lt;&lt; hex &lt;&lt; hres &lt;&lt; endl; cout &lt;&lt; _com_error(hres).ErrorMessage() &lt;&lt; endl; cout &lt;&lt; "press enter to exit" &lt;&lt; endl; cin.get(); return 1; // Program has failed. } // Set general COM security levels -------------------------- if (localconn) hres = CoInitializeSecurity( NULL, -1, // COM authentication NULL, // Authentication services NULL, // Reserved RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation NULL, // Authentication info EOAC_NONE, // Additional capabilities NULL // Reserved ); else hres = CoInitializeSecurity( NULL, -1, // COM authentication NULL, // Authentication services NULL, // Reserved RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication RPC_C_IMP_LEVEL_IDENTIFY, // Default Impersonation NULL, // Authentication info EOAC_NONE, // Additional capabilities NULL // Reserved ); if (FAILED(hres)) { cout &lt;&lt; "Failed to initialize security. Error code = 0x" &lt;&lt; hex &lt;&lt; hres &lt;&lt; endl; cout &lt;&lt; _com_error(hres).ErrorMessage() &lt;&lt; endl; CoUninitialize(); cout &lt;&lt; "press enter to exit" &lt;&lt; endl; cin.get(); return 1; // Program has failed. } // Obtain the initial locator to WMI ------------------------- IWbemLocator *pLoc = NULL; hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &amp;pLoc); if (FAILED(hres)) { cout &lt;&lt; "Failed to create IWbemLocator object. " &lt;&lt; "Err code = 0x" &lt;&lt; hex &lt;&lt; hres &lt;&lt; endl; cout &lt;&lt; _com_error(hres).ErrorMessage() &lt;&lt; endl; CoUninitialize(); cout &lt;&lt; "press enter to exit" &lt;&lt; endl; cin.get(); return 1; // Program has failed. } // Connect to WMI through the IWbemLocator::ConnectServer method IWbemServices *pSvc = NULL; // Connect to the root\\CIMV2 namespace // and obtain pointer pSvc to make IWbemServices calls. if (localconn) hres = pLoc-&gt;ConnectServer( _bstr_t(strNetworkResource), // Object path of WMI namespace NULL, // User name. NULL = current user NULL, // User password. NULL = current 0, // Locale. NULL indicates current NULL, // Security flags. 0, // Authority (e.g. Kerberos) 0, // Context object &amp;pSvc // pointer to IWbemServices proxy ); else hres = pLoc-&gt;ConnectServer( _bstr_t(strNetworkResource), // Object path of WMI namespace _bstr_t(pszName), // User name _bstr_t(pszPwd), // User password NULL, // Locale NULL, // Security flags NULL, // Authority NULL, // Context object &amp;pSvc // IWbemServices proxy ); if (FAILED(hres)) { cout &lt;&lt; "Could not connect. Error code = 0x" &lt;&lt; hex &lt;&lt; hres &lt;&lt; endl; cout &lt;&lt; _com_error(hres).ErrorMessage() &lt;&lt; endl; pLoc-&gt;Release(); CoUninitialize(); cout &lt;&lt; "press enter to exit" &lt;&lt; endl; cin.get(); return 1; // Program has failed. } cout &lt;&lt; "Connected to root\\CIMV2 WMI namespace" &lt;&lt; endl; // Set security levels on the proxy ------------------------- if (localconn) hres = CoSetProxyBlanket( pSvc, // Indicates the proxy to set RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx NULL, // Server principal name RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx NULL, // client identity EOAC_NONE // proxy capabilities ); else { // Create COAUTHIDENTITY that can be used for setting security on proxy memset(&amp;authIdent, 0, sizeof(COAUTHIDENTITY)); authIdent.PasswordLength = wcslen (pszPwd); authIdent.Password = (USHORT*)pszPwd; authIdent.User = (USHORT*)pszName; authIdent.UserLength = wcslen(pszName); authIdent.Domain = 0; authIdent.DomainLength = 0; authIdent.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; userAcct = &amp;authIdent; hres = CoSetProxyBlanket( pSvc, // Indicates the proxy to set RPC_C_AUTHN_DEFAULT, // RPC_C_AUTHN_xxx RPC_C_AUTHZ_DEFAULT, // RPC_C_AUTHZ_xxx COLE_DEFAULT_PRINCIPAL, // Server principal name RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // RPC_C_AUTHN_LEVEL_xxx RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx userAcct, // client identity EOAC_NONE // proxy capabilities ); } if (FAILED(hres)) { cout &lt;&lt; "Could not set proxy blanket. Error code = 0x" &lt;&lt; hex &lt;&lt; hres &lt;&lt; endl; cout &lt;&lt; _com_error(hres).ErrorMessage() &lt;&lt; endl; pSvc-&gt;Release(); pLoc-&gt;Release(); CoUninitialize(); cout &lt;&lt; "press enter to exit" &lt;&lt; endl; cin.get(); return 1; // Program has failed. } // Use the IWbemServices pointer to make requests of WMI ---- BSTR MethodName = SysAllocString(L"Terminate"); BSTR ClassName = SysAllocString(L"Win32_Process"); IWbemClassObject* pClass = NULL; hres = pSvc-&gt;GetObject(ClassName, 0, NULL, &amp;pClass, NULL); IWbemClassObject* pInParamsDefinition = NULL; hres = pClass-&gt;GetMethod(MethodName, 0, &amp;pInParamsDefinition, NULL); IWbemClassObject* pClassInstance = NULL; hres = pInParamsDefinition-&gt;SpawnInstance(0, &amp;pClassInstance); VARIANT varCommand; // Execute Method IWbemClassObject* pOutParams = NULL; // Pass the Pid of the process to terminate hres = pSvc-&gt;ExecMethod(L"Win32_Process.Handle=\"4532\"", MethodName, 0, NULL, pClassInstance, &amp;pOutParams, NULL); if (FAILED(hres)) { cout &lt;&lt; "Could not execute method. Error code = 0x" &lt;&lt; hex &lt;&lt; hres &lt;&lt; endl; cout &lt;&lt; _com_error(hres).ErrorMessage() &lt;&lt; endl; SysFreeString(ClassName); SysFreeString(MethodName); if (pClass) pClass-&gt;Release(); if (pInParamsDefinition) pInParamsDefinition-&gt;Release(); if (pOutParams) pOutParams-&gt;Release(); if (pSvc) pSvc-&gt;Release(); if (pLoc) pLoc-&gt;Release(); CoUninitialize(); cout &lt;&lt; "press enter to exit" &lt;&lt; endl; cin.get(); return 1; // Program has failed. } VARIANT varReturnValue; hres = pOutParams-&gt;Get(L"ReturnValue", 0, &amp;varReturnValue, NULL, 0); if (!FAILED(hres)) wcout &lt;&lt; "ReturnValue " &lt;&lt; varReturnValue.intVal &lt;&lt; endl; VariantClear(&amp;varReturnValue); // Clean up SysFreeString(ClassName); SysFreeString(MethodName); if (pClass) pClass-&gt;Release(); if (pInParamsDefinition) pInParamsDefinition-&gt;Release(); if (pOutParams) pOutParams-&gt;Release(); if (pLoc) pLoc-&gt;Release(); if (pSvc) pSvc-&gt;Release(); CoUninitialize(); cout &lt;&lt; "press enter to exit" &lt;&lt; endl; cin.get(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload