Question
November 16, 2006 at 09:13 AM
abhiramhi

Killing Process SUsing WMI

by abhiramhi . Updated 19 years, 11 months ago

hi

I am working killing a process using WMI iam using the following code .

HRESULT hres;

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=\”\1418\””);

_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.
}

it is working fine ,but here iam gicing the Process ID which has to be terminated like 1418.

Instead of ProcessID i want to use process Name like CAlc.exe or notepad.exe

but when IAm tring to change the code with the process name like

SysAllocString(
L”Win32_Process.Name=\”\notepad.exe\””);

notepad is not terminating . can you please help me to solve it.

Thanks in Advance

This discussion is locked

All Comments