wmi start stop service
#define _WIN32_DCOM
#include <Wbemidl.h>
#pragma comment(lib,"Wbemuuid.lib")
#include <atlbase.h>
#include "objbase.h"
#include <windows.h>
#include <comutil.h>
#pragma comment(lib, "comsupp.lib")
#include <iostream>
int main()
{ //start/stop service on windows 2000 Professional SP4, VC6 SP6 by
Onega
CComBSTR path = ("root\\cimv2");
CComBSTR InstancePath =("Win32_Service.Name='Spooler'");
HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if(SUCCEEDED(hr))
hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_CONNECT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_NONE, 0
);
{
CComPtr<IWbemLocator> spLoc;
// hr = spLoc.CoCreateInstance(CLSID_WbemLocator);
if(SUCCEEDED(hr))
hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &spLoc);
CComBSTR bstrNamespace("\\\\.\\root\\CIMV2");
CComPtr<IWbemServices> spServices;
if(SUCCEEDED(hr))
hr = spLoc->ConnectServer(bstrNamespace, NULL, NULL, 0,
NULL, 0, 0, &spServices);
IWbemServices *pNamespace = spServices;
if(SUCCEEDED(hr))
hr = CoSetProxyBlanket(spServices, RPC_C_AUTHN_DEFAULT,
RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
{
CComBSTR ClassPath = ("Win32_Service");
CComBSTR MethodName = "StopService";
_variant_t varProperty;
CComBSTR bstrPropName = "state";
CComPtr<IWbemClassObject> spInstance;
hr = pNamespace->GetObject(InstancePath,0,NULL,&spInstance,NULL);
hr = spInstance->Get(bstrPropName,0,&varProperty,NULL,0);
if( VT_BSTR == varProperty.vt &&
_wcsicmp(varProperty.bstrVal, L"Stopped") == 0)
{
MethodName = "StartService";
}
CComPtr<IWbemClassObject> pClass ;
CComPtr<IWbemClassObject> pOutInst;
CComPtr<IWbemClassObject> pInClass;
CComPtr<IWbemClassObject> pInInst;
CComPtr<IWbemClassObject> pOutClass;
if(SUCCEEDED(hr))
hr = pNamespace->GetObject(ClassPath, 0, NULL,
&pClass, NULL);
if(SUCCEEDED(hr))
hr = pClass->GetMethod(MethodName, 0, &pInClass, &pOutClass);
if(SUCCEEDED(hr) && pInClass)
hr = pInClass->SpawnInstance(0, &pInInst);
CComBSTR return_value_name = "ReturnValue";
if(SUCCEEDED(hr))
hr = pNamespace->ExecMethod(InstancePath, MethodName,
0, NULL, pInInst,&pOutInst, NULL);
_variant_t ret_value;
hr = pOutInst->Get(return_value_name, 0, &ret_value, 0, 0);
std::wcout<<L"Execute "<<(LPCWSTR)MethodName<<L" return "
<<(long)ret_value<<std::endl;
}
}
std::cout<< "finished "<<std::endl;
CoUninitialize();
return ERROR_SUCCESS;
}
#include <Wbemidl.h>
#pragma comment(lib,"Wbemuuid.lib")
#include <atlbase.h>
#include "objbase.h"
#include <windows.h>
#include <comutil.h>
#pragma comment(lib, "comsupp.lib")
#include <iostream>
int main()
{ //start/stop service on windows 2000 Professional SP4, VC6 SP6 by
Onega
CComBSTR path = ("root\\cimv2");
CComBSTR InstancePath =("Win32_Service.Name='Spooler'");
HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if(SUCCEEDED(hr))
hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_CONNECT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_NONE, 0
);
{
CComPtr<IWbemLocator> spLoc;
// hr = spLoc.CoCreateInstance(CLSID_WbemLocator);
if(SUCCEEDED(hr))
hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &spLoc);
CComBSTR bstrNamespace("\\\\.\\root\\CIMV2");
CComPtr<IWbemServices> spServices;
if(SUCCEEDED(hr))
hr = spLoc->ConnectServer(bstrNamespace, NULL, NULL, 0,
NULL, 0, 0, &spServices);
IWbemServices *pNamespace = spServices;
if(SUCCEEDED(hr))
hr = CoSetProxyBlanket(spServices, RPC_C_AUTHN_DEFAULT,
RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
{
CComBSTR ClassPath = ("Win32_Service");
CComBSTR MethodName = "StopService";
_variant_t varProperty;
CComBSTR bstrPropName = "state";
CComPtr<IWbemClassObject> spInstance;
hr = pNamespace->GetObject(InstancePath,0,NULL,&spInstance,NULL);
hr = spInstance->Get(bstrPropName,0,&varProperty,NULL,0);
if( VT_BSTR == varProperty.vt &&
_wcsicmp(varProperty.bstrVal, L"Stopped") == 0)
{
MethodName = "StartService";
}
CComPtr<IWbemClassObject> pClass ;
CComPtr<IWbemClassObject> pOutInst;
CComPtr<IWbemClassObject> pInClass;
CComPtr<IWbemClassObject> pInInst;
CComPtr<IWbemClassObject> pOutClass;
if(SUCCEEDED(hr))
hr = pNamespace->GetObject(ClassPath, 0, NULL,
&pClass, NULL);
if(SUCCEEDED(hr))
hr = pClass->GetMethod(MethodName, 0, &pInClass, &pOutClass);
if(SUCCEEDED(hr) && pInClass)
hr = pInClass->SpawnInstance(0, &pInInst);
CComBSTR return_value_name = "ReturnValue";
if(SUCCEEDED(hr))
hr = pNamespace->ExecMethod(InstancePath, MethodName,
0, NULL, pInInst,&pOutInst, NULL);
_variant_t ret_value;
hr = pOutInst->Get(return_value_name, 0, &ret_value, 0, 0);
std::wcout<<L"Execute "<<(LPCWSTR)MethodName<<L" return "
<<(long)ret_value<<std::endl;
}
}
std::cout<< "finished "<<std::endl;
CoUninitialize();
return ERROR_SUCCESS;
}
0 Comments:
Post a Comment
<< Home