AI Agent技术社区
C++通过WMI启动/停止服务,修改服务类型 ( C++通过WMI启动/停止服务,修改服务类型 (
stdafx.h#pragma once#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料#define _CRT_SECURE_NO_WARNINGS#define _CRT_SECURE_NO_DEPRECATE#include#include#includeusing namesp
joliny · 2012-03-14 16:18:34 发布
| stdafx.h |
|
#pragma once
#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <tchar.h>
#include <string>
using namespace std; |
| WMI.h |
std::string GetServerProperty(LPCWSTR proName, IWbemClassObject *pclsObj);
int GetWin32Info(string wql, IWbemLocator *&pLoc, IWbemServices *&pSvc, IEnumWbemClassObject *&pEnumerator);
//调用没有参数的方法
int ExecMethod(OLECHAR* sClassName, OLECHAR* sMethodName, IWbemLocator *&pLoc, IWbemServices *&pSvc, IEnumWbemClassObject *&pEnumerator, IWbemClassObject *&pclsObj);
//调用有参数的方法
int ExecMethod(OLECHAR* sClassName, OLECHAR* sMethodName, LPCWSTR sParaName, OLECHAR* sParaValue, IWbemLocator *&pLoc, IWbemServices *&pSvc, IEnumWbemClassObject *&pEnumerator, IWbemClassObject *&pclsObj); |
| WMI.cpp |
#include "stdafx.h"
#include "get_hostname_IP_MAC.h"
#include "get_service_info.h"
#include "replace_special_str.h"
#include
string GetServerProperty(LPCWSTR proName, IWbemClassObject *pclsObj)
{
char buf[2000];
VARIANT vtProp;
HRESULT hr = pclsObj->Get(proName, 0, &vtProp, 0, 0);
if( hr == WBEM_S_NO_ERROR && vtProp.vt != VT_NULL )
{
_bstr_t bstr = &vtProp; //Just to convert BSTR to ANSI
char* str=(char*)bstr;
sprintf(buf,"%s", str);
VariantClear(&vtProp);
return replace_c(buf);
}
return "";
}
int GetWin32Info(string wql, IWbemLocator *&pLoc, IWbemServices *&pSvc, IEnumWbemClassObject *&pEnumerator)
{
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl;
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you need to specify -
|