Question
November 15, 2006 at 06:37 PM
abhiramhi

Terminating process using WMI

by abhiramhi . Updated 19 years, 11 months ago

I am trying to terminate a process using WMI iam using the folowing code for termination

BSTR ClassName = SysAllocString(L”Win32_Process”);

/* YOU NEED TO CHANGE THE NUMBER VALUE OF THE HANDLE
(PROCESS ID) TO THE CORRECT VALUE OF THE PROCESS YOU
ARE TRYING TO TERMINATE (this provides a path to
the class instance you are tying to terminate). */
BSTR ClassNameInstance = SysAllocString(
L”Win32_Process.Handle=\”3336\””);

_bstr_t MethodName = (L”Terminate”);
BSTR ParameterName = SysAllocString(L”Reason”);

IWbemClassObject* pClass = NULL;
hres = m_pIWbemService->GetObject(ClassName, 0, NULL, &pClass, NULL);

IWbemClassObject* pInParamsDefinition = NULL;
IWbemClassObject* pOutMethod = NULL;
hres = pClass->GetMethod(MethodName, 0,
&pInParamsDefinition, &pOutMethod);

if (FAILED(hres))
{
cout << "Could not get the method. Error code = 0x" << hex << hres << endl; } IWbemClassObject* pClassInstance = NULL; hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance);

// Create the values for the in parameters
VARIANT pcVal;
VariantInit(&pcVal);
V_VT(&pcVal) = VT_I4;

// Store the value for the in parameters
hres = pClassInstance->Put(L”Reason”, 0,
&pcVal, 0);

// Execute Method
hres = m_pIWbemService->ExecMethod(ClassNameInstance, MethodName, 0,
NULL, pClassInstance, NULL, NULL);

if (FAILED(hres))
{
cout << "Could not execute method. Error code = 0x" << hex << hres << endl; VariantClear(&pcVal); SysFreeString(ClassName); SysFreeString(MethodName); pClass->Release();
pInParamsDefinition->Release();
m_pIWbemService->Release();
//pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}

but here iam giving the process ID as 3334 and it is working fine .But instead of process ID i want to give proces name but when iam changing the processid to process name

like
BSTR ClassNameInstance = SysAllocString( L”Win32_Process.Handle=\”notepad.exe\””);

iam not able to terminate the process notepad

can you please help me and modify the code

Thanks in advance

This discussion is locked

All Comments