Starting/Stoping a Service Using WMI and C/C++

Joined
Sep 5, 2005
Messages
5
Reaction score
0
Hi, Sunit here i am working on WMI technology....

Actually i am trying to start/stop services through WMI in C but it's not happening...

Actually i am able to catch all the processes in the local as well as remote system... That is connection and query are succesfull. But i am unable to execute the method...

Here is the piece of code i am writing to start/stop a service...

if(temp.CompareNoCase("Alerter") == 0) // this is how i am catching the service to stop/start after querying
{
printf("\nAlerter Killed\n");
BSTR MethodName = SysAllocString(L"StartService"); //method name
BSTR ClassName = SysAllocString(L"Win32_Service");//class name
char name[50] = "";
sprintf(name,"Win32_Service.DisplayName=\"%s\"",temp);
AfxMessageBox(name);
CString ClassPath = name;
IWbemClassObject* pClass = NULL;
hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);

// Execute Method

IWbemClassObject* pOutParams = NULL;
hres = pSvc->ExecMethod(_bstr_t(ClassPath), MethodName, 0,
NULL, NULL, &pOutParams, NULL);

// if you want to see what the method returned,
// use the following code. The return value will
// be in &varReturnValue

// Inspect out-parameters for return value
CComVariant varReturnValue;
hres = pOutParams->Get(L"ReturnValue", 0, &varReturnValue, 0, 0);

_tprintf(_T("\n %ls::%ls returned %ls\n"), ClassName, MethodName,
V_BSTR(&varReturnValue));

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

// Clean up
//--------------------------
VariantClear(&varReturnValue);
SysFreeString(ClassName);
SysFreeString(MethodName);
pClass->Release();
pOutParams->Release();

}


but here in this piece of the ExecMethod( ) function always fails... Could you tell me how to solve this problem in C....that is i need to start and stop services as per the user... Apart from this is there any other way to start/stop a service using WMI,C/C++.. Need Help

Thanks in advance..

Regards,
Sunit Nair
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top