1
A
回答
5
對於遠程機器殺死一個過程中,你可以使用Win32_Process
WMI類和Terminate
方法和停止遠程服務,您可以使用Win32_Service
和StopService
方法。
這裏是一個C++樣品:
#include "stdafx.h"
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
# 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 << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl;
cout << _com_error(hres).ErrorMessage() << endl;
cout << "press enter to exit" << 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 << "Failed to initialize security. Error code = 0x" << hex << hres << endl;
cout << _com_error(hres).ErrorMessage() << endl;
CoUninitialize();
cout << "press enter to exit" << 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 *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object. " << "Err code = 0x" << hex << hres << endl;
cout << _com_error(hres).ErrorMessage() << endl;
CoUninitialize();
cout << "press enter to exit" << 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->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
&pSvc // pointer to IWbemServices proxy
);
else
hres = pLoc->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
&pSvc // IWbemServices proxy
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x" << hex << hres << endl;
cout << _com_error(hres).ErrorMessage() << endl;
pLoc->Release();
CoUninitialize();
cout << "press enter to exit" << endl;
cin.get();
return 1; // Program has failed.
}
cout << "Connected to root\\CIMV2 WMI namespace" << 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(&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 = &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 << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl;
cout << _com_error(hres).ErrorMessage() << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
cout << "press enter to exit" << 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->GetObject(ClassName, 0, NULL, &pClass, NULL);
IWbemClassObject* pInParamsDefinition = NULL;
hres = pClass->GetMethod(MethodName, 0, &pInParamsDefinition, NULL);
IWbemClassObject* pClassInstance = NULL;
hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance);
VARIANT varCommand;
// Execute Method
IWbemClassObject* pOutParams = NULL;
// Pass the Pid of the process to terminate
hres = pSvc->ExecMethod(L"Win32_Process.Handle=\"4532\"", MethodName, 0,
NULL, pClassInstance, &pOutParams, NULL);
if (FAILED(hres))
{
cout << "Could not execute method. Error code = 0x" << hex << hres << endl;
cout << _com_error(hres).ErrorMessage() << endl;
SysFreeString(ClassName);
SysFreeString(MethodName);
if (pClass) pClass->Release();
if (pInParamsDefinition) pInParamsDefinition->Release();
if (pOutParams) pOutParams->Release();
if (pSvc) pSvc->Release();
if (pLoc) pLoc->Release();
CoUninitialize();
cout << "press enter to exit" << endl;
cin.get();
return 1; // Program has failed.
}
VARIANT varReturnValue;
hres = pOutParams->Get(L"ReturnValue", 0, &varReturnValue, NULL, 0);
if (!FAILED(hres))
wcout << "ReturnValue " << varReturnValue.intVal << endl;
VariantClear(&varReturnValue);
// Clean up
SysFreeString(ClassName);
SysFreeString(MethodName);
if (pClass) pClass->Release();
if (pInParamsDefinition) pInParamsDefinition->Release();
if (pOutParams) pOutParams->Release();
if (pLoc) pLoc->Release();
if (pSvc) pSvc->Release();
CoUninitialize();
cout << "press enter to exit" << endl;
cin.get();
return 0;
}
相關問題
- 1. 如何通過在遠程計算機上使用java來終止進程?
- 2. 在遠程計算機上SSH註銷後,Firefox進程終止
- 3. 在遠程計算機上使用python啓動後臺進程
- 4. 使用Sysinternals在遠程計算機上執行進程PsExec
- 5. 遠程計算機上使用腳本
- 6. 在遠程計算機上使用WMI
- 7. 如何在Windows下的C++中的遠程計算機上啓動進程
- 8. 如何啓動/停止使用C#的遠程機器上的進程?
- 9. WaitForExit用於遠程計算機上的進程
- 10. 無法遠程終止使用WMI和C的進程#
- 11. 如何在遠程計算機上使用python調用終端/ powershell
- 12. 如何與遠程計算機上的對象進行通信?
- 13. 如何終止遠程桌面連接上的進程?
- 14. 遠程計算機
- 15. 遠程計算機
- 16. 父進程在C#中終止時如何終止子進程
- 17. 如何使用pysphere執行遠程計算機上的命令
- 18. 遠程計算機上的進程信息
- 19. 啓動前檢查遠程計算機上的進程
- 20. 殺死多個遠程計算機上的進程
- 21. 使用C#在遠程計算機上驗證Active Dir
- 22. 如何使用perl在遠程計算機上運行命令?
- 23. 如何使用Perl在遠程計算機上運行命令?
- 24. 如何使用PowerShell在遠程計算機上卸載服務?
- 25. 如何在遠程計算機上創建文件夾c#
- 26. 在遠程計算機上使用Powershell啓動和停止應用程序池
- 27. 使用powershell在遠程計算機上運行的進程的啓動時間
- 28. 遠程控制計算機並在遠程計算機上顯示
- 29. 如何在特定的遠程計算機上運行程序
- 30. 如何獲取遠程計算機上正在運行的進程的描述?
任何C或C++,關於如何使用它們的例子嗎? – shawn 2012-03-22 05:26:08
我剛剛添加了一個C++示例。 – RRUZ 2012-03-22 05:42:36
@shawn通過用'printf()'和'_com_error(hres).ErrorMessage()'用Kernel32的'GetLastError()'替換'cout',可以將上面的C++代碼轉換爲C。 – 2012-03-22 05:53:41