VC WMI 2
来源:互联网 发布:怎么用vps建站 编辑:程序博客网 时间:2024/06/05 16:36
VC里实现WMI编程:获取网络信息1(VC已调试过了)
2009年01月08日 星期四 18:51
//---------------------------------------------------------------------------
// 使用WMI获取硬件消息
#define _WIN32_DCOM
//#include <objbase.h>
#include <comdef.h>
#include <Wbemidl.h>
//#include <conio.h>
# pragma comment(lib, "wbemuuid.lib")
//---------------------------------------------------------------------------
static char *wc2mb( const wchar_t *pSrc )
{
char *rval ;
int iSize ;
if ( NULL == pSrc ){
return NULL ;
}
// 计算,返回值的领域要多少
iSize = WideCharToMultiByte( CP_ACP,
0,
pSrc,
-1, // 自动计算源字符串的长短
NULL, // 现在估计出力领域的大小,所以指定 NULL
0, // 现在估计出力领域的大小,所以指定 0
NULL,
NULL
);
// 确保领域
rval = (char *)malloc( iSize );
if ( NULL != rval ){
// 执行字套种类变换
iSize = WideCharToMultiByte( CP_ACP,
0,
pSrc,
-1, // 自动计算源字符串的长短
rval,
iSize,
NULL,
NULL
);
}
return rval ;
}
BOOL ManageWMI(char *str)
{
HRESULT hres;
char szTmp[40960];
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hres)) {
sprintf(str,"Failed to initialize COM library. Error code = 0x%X/r/n",hres);
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you need to specify -
// the default authentication credentials for a user by using
// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
// parameter of CoInitializeSecurity ------------------------
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres)) {
sprintf(str,"Failed to initialize security. Error code = 0x%X/r/n",hres);
CoUninitialize();
return 1; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres)) {
sprintf(str,"Failed to create IWbemLocator object. Err code = 0x%X/r/n",hres);
CoUninitialize();
return 1; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
// Connect to the root/cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT//CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres)) {
sprintf(str,"Could not connect. Error code = 0x%X/r/n",hres);
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
sprintf(str,"Connected to ROOT//CIMV2 WMI namespace");
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres)) {
sprintf(str,"Could not set proxy blanket. Error code = 0x%X/r/n",hres);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = NULL;
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
/*hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres)) {
sprintf(str,"Query for Network Adapter Configuration failed. Error code = 0x%X/r/n", hres);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
while (pEnumerator) {
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if(0 == uReturn) {
break;
}
VARIANT vtProp;
VariantInit(&vtProp);
//hr = pclsObj->Get(L"IPSubnet", 0, &vtProp, 0, 0);
// Get the value of the Enable property
hr = pclsObj->Get(L"IPEnabled", 0, &vtProp, 0, 0);
// Get the value of the MacAddress property
if(vtProp.boolVal) {
hr = pclsObj->Get(L"MacAddress", 0, &vtProp, 0, 0);
sprintf(szTmp," MacAddress : %s/r/n" ,wc2mb(vtProp.bstrVal));
strcat(str,szTmp);
}
hr = pclsObj->Get(L"IPEnabled", 0, &vtProp, 0, 0);
if(vtProp.boolVal) {
LONG lstart, lend;
LONG idx = -1;
BSTR* pbstr;
SAFEARRAY *sa;
hr = pclsObj->Get(L"Description", 0, &vtProp, 0, 0);
if(!FAILED(hr)) {
sprintf(szTmp,"Description: %s/r/n",wc2mb(vtProp.bstrVal));
strcat(str,szTmp);
}
hr = pclsObj->Get(L"DNSHostName", 0, &vtProp, 0, 0);
if(!FAILED(hr)) {
sprintf(szTmp,"DNS:%s/r/n",wc2mb(vtProp.bstrVal));
strcat(str,szTmp);
}
hr = pclsObj->Get(L"IPAddress", 0, &vtProp, 0, 0);
if(!FAILED(hr)) {
SAFEARRAY *psa = vtProp.parray;
void **p = (void **)psa->pvData;
wchar_t *wstr = (unsigned short *)*p;
sprintf(szTmp,"IP Address::%s/r/n", wc2mb(wstr));
strcat(str,szTmp);
}
}
VariantClear(&vtProp);
}*/
// get baseboard's infomation
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_BaseBoard"),// WHERE IPEnabled = 'TRUE'"),
/*WBEM_FLAG_FORWARD_ONLY | */WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres)) {
sprintf(str,"Query for BaseBoard's infomation failed. Error code = 0x%X/r/n", hres);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
uReturn = 0;
while (pEnumerator) {
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if(0 == uReturn) {
break;
}
VARIANT vtProp;
VariantInit(&vtProp);
//hr = pclsObj->Get(L"IPSubnet", 0, &vtProp, 0, 0);
// Get the value of the Enable property
hr = pclsObj->Get(L"IPEnabled", 0, &vtProp, 0, 0);
// Get the value of the MacAddress property
if(vtProp.boolVal) {
hr = pclsObj->Get(L"Caption", 0, &vtProp, 0, 0);
sprintf(szTmp," Caption : %s/r/n" ,wc2mb(vtProp.bstrVal));
strcat(str,szTmp);
}
}
// Cleanup
// ========
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
pclsObj->Release();
CoUninitialize();
return 0; // Program successfully completed.
}
WMI 可以访问的信息类型有:
Win32_1394Controller
Win32_BaseBoard
Win32_Battery
Win32_BIOS
Win32_Bus
Win32_CacheMemory
Win32_CDROMDrive
Win32_CurrentProbe
Win32_DesktopMonitor
Win32_DeviceMemoryAddress
Win32_DiskDrive
Win32_DisplayConfiguration
Win32_DisplayControllerConfiguration
Win32_DMAChannel
Win32_Fan
Win32_FloppyController
Win32_FloppyDrive
Win32_HeatPipe
Win32_IDEController
Win32_InfraredDevice
Win32_IRQResource
Win32_Keyboard
Win32_MemoryArray
Win32_MemoryDevice
Win32_MotherboardDevice
Win32_NetworkAdapter
Win32_NetworkAdapterConfiguration
Win32_OnBoardDevice
Win32_ParallelPort
Win32_PCMCIAController
Win32_PhysicalMemory
Win32_PhysicalMemoryArray
Win32_PnPEntity
Win32_PointingDevice
Win32_PortableBattery
Win32_PortConnector
Win32_PortResource
Win32_POTSModem
Win32_PowerManagementEvent
Win32_Printer
Win32_PrinterConfiguration
Win32_PrintJob
Win32_Processor
Win32_Refrigeration
Win32_SerialPort
Win32_SerialPortConfiguration
Win32_SMBIOSMemory
Win32_SoundDevice
Win32_SystemEnclosure
Win32_SystemMemoryResource
Win32_SystemSlot
Win32_TapeDrive
Win32_TemperatureProbe
Win32_UninterruptiblePowerSupply
Win32_USBController
Win32_VideoConfiguration
Win32_VideoController
Win32_VoltageProbe
// 使用WMI获取硬件消息
#define _WIN32_DCOM
//#include <objbase.h>
#include <comdef.h>
#include <Wbemidl.h>
//#include <conio.h>
# pragma comment(lib, "wbemuuid.lib")
//---------------------------------------------------------------------------
static char *wc2mb( const wchar_t *pSrc )
{
char *rval ;
int iSize ;
if ( NULL == pSrc ){
return NULL ;
}
// 计算,返回值的领域要多少
iSize = WideCharToMultiByte( CP_ACP,
0,
pSrc,
-1, // 自动计算源字符串的长短
NULL, // 现在估计出力领域的大小,所以指定 NULL
0, // 现在估计出力领域的大小,所以指定 0
NULL,
NULL
);
// 确保领域
rval = (char *)malloc( iSize );
if ( NULL != rval ){
// 执行字套种类变换
iSize = WideCharToMultiByte( CP_ACP,
0,
pSrc,
-1, // 自动计算源字符串的长短
rval,
iSize,
NULL,
NULL
);
}
return rval ;
}
BOOL ManageWMI(char *str)
{
HRESULT hres;
char szTmp[40960];
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hres)) {
sprintf(str,"Failed to initialize COM library. Error code = 0x%X/r/n",hres);
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you need to specify -
// the default authentication credentials for a user by using
// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
// parameter of CoInitializeSecurity ------------------------
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres)) {
sprintf(str,"Failed to initialize security. Error code = 0x%X/r/n",hres);
CoUninitialize();
return 1; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres)) {
sprintf(str,"Failed to create IWbemLocator object. Err code = 0x%X/r/n",hres);
CoUninitialize();
return 1; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
// Connect to the root/cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT//CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres)) {
sprintf(str,"Could not connect. Error code = 0x%X/r/n",hres);
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
sprintf(str,"Connected to ROOT//CIMV2 WMI namespace");
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres)) {
sprintf(str,"Could not set proxy blanket. Error code = 0x%X/r/n",hres);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = NULL;
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
/*hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres)) {
sprintf(str,"Query for Network Adapter Configuration failed. Error code = 0x%X/r/n", hres);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
while (pEnumerator) {
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if(0 == uReturn) {
break;
}
VARIANT vtProp;
VariantInit(&vtProp);
//hr = pclsObj->Get(L"IPSubnet", 0, &vtProp, 0, 0);
// Get the value of the Enable property
hr = pclsObj->Get(L"IPEnabled", 0, &vtProp, 0, 0);
// Get the value of the MacAddress property
if(vtProp.boolVal) {
hr = pclsObj->Get(L"MacAddress", 0, &vtProp, 0, 0);
sprintf(szTmp," MacAddress : %s/r/n" ,wc2mb(vtProp.bstrVal));
strcat(str,szTmp);
}
hr = pclsObj->Get(L"IPEnabled", 0, &vtProp, 0, 0);
if(vtProp.boolVal) {
LONG lstart, lend;
LONG idx = -1;
BSTR* pbstr;
SAFEARRAY *sa;
hr = pclsObj->Get(L"Description", 0, &vtProp, 0, 0);
if(!FAILED(hr)) {
sprintf(szTmp,"Description: %s/r/n",wc2mb(vtProp.bstrVal));
strcat(str,szTmp);
}
hr = pclsObj->Get(L"DNSHostName", 0, &vtProp, 0, 0);
if(!FAILED(hr)) {
sprintf(szTmp,"DNS:%s/r/n",wc2mb(vtProp.bstrVal));
strcat(str,szTmp);
}
hr = pclsObj->Get(L"IPAddress", 0, &vtProp, 0, 0);
if(!FAILED(hr)) {
SAFEARRAY *psa = vtProp.parray;
void **p = (void **)psa->pvData;
wchar_t *wstr = (unsigned short *)*p;
sprintf(szTmp,"IP Address::%s/r/n", wc2mb(wstr));
strcat(str,szTmp);
}
}
VariantClear(&vtProp);
}*/
// get baseboard's infomation
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_BaseBoard"),// WHERE IPEnabled = 'TRUE'"),
/*WBEM_FLAG_FORWARD_ONLY | */WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres)) {
sprintf(str,"Query for BaseBoard's infomation failed. Error code = 0x%X/r/n", hres);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
uReturn = 0;
while (pEnumerator) {
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if(0 == uReturn) {
break;
}
VARIANT vtProp;
VariantInit(&vtProp);
//hr = pclsObj->Get(L"IPSubnet", 0, &vtProp, 0, 0);
// Get the value of the Enable property
hr = pclsObj->Get(L"IPEnabled", 0, &vtProp, 0, 0);
// Get the value of the MacAddress property
if(vtProp.boolVal) {
hr = pclsObj->Get(L"Caption", 0, &vtProp, 0, 0);
sprintf(szTmp," Caption : %s/r/n" ,wc2mb(vtProp.bstrVal));
strcat(str,szTmp);
}
}
// Cleanup
// ========
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
pclsObj->Release();
CoUninitialize();
return 0; // Program successfully completed.
}
WMI 可以访问的信息类型有:
Win32_1394Controller
Win32_BaseBoard
Win32_Battery
Win32_BIOS
Win32_Bus
Win32_CacheMemory
Win32_CDROMDrive
Win32_CurrentProbe
Win32_DesktopMonitor
Win32_DeviceMemoryAddress
Win32_DiskDrive
Win32_DisplayConfiguration
Win32_DisplayControllerConfiguration
Win32_DMAChannel
Win32_Fan
Win32_FloppyController
Win32_FloppyDrive
Win32_HeatPipe
Win32_IDEController
Win32_InfraredDevice
Win32_IRQResource
Win32_Keyboard
Win32_MemoryArray
Win32_MemoryDevice
Win32_MotherboardDevice
Win32_NetworkAdapter
Win32_NetworkAdapterConfiguration
Win32_OnBoardDevice
Win32_ParallelPort
Win32_PCMCIAController
Win32_PhysicalMemory
Win32_PhysicalMemoryArray
Win32_PnPEntity
Win32_PointingDevice
Win32_PortableBattery
Win32_PortConnector
Win32_PortResource
Win32_POTSModem
Win32_PowerManagementEvent
Win32_Printer
Win32_PrinterConfiguration
Win32_PrintJob
Win32_Processor
Win32_Refrigeration
Win32_SerialPort
Win32_SerialPortConfiguration
Win32_SMBIOSMemory
Win32_SoundDevice
Win32_SystemEnclosure
Win32_SystemMemoryResource
Win32_SystemSlot
Win32_TapeDrive
Win32_TemperatureProbe
Win32_UninterruptiblePowerSupply
Win32_USBController
Win32_VideoConfiguration
Win32_VideoController
Win32_VoltageProbe
- VC WMI 2
- VC WMI
- VC调用WMI框架
- vc使用wmi例子
- VC下WMI示例代码
- WMI编程之一:在VC中使用WMI
- vc操作wmi遍历进程信息
- 利用WMI实现ring3进程监控 - vc
- VC WMI应用获取SMBios中的信息
- WMI Series 2:windows管理规范WMI
- WMI Series 2:windows管理规范WMI
- WMI
- WMI
- WMI
- WMI
- WMI
- WMI技术介绍和应用——VC开发WMI应用的基本步骤
- WMI技术介绍和应用——VC开发WMI应用的基本步骤
- C语言转义字符集
- 运算符优先级
- proc文件系统分析
- VC WMI
- Linux FTP 服务器配置简单说明
- VC WMI 2
- [转]CentOS5.5编译arm-elf-gcc
- 使用 AppFuse 的七个理由
- 主题:推荐一个超强的swing表格
- netfilter框架
- WMI常见问题解答
- C核心技术手册(二十八)
- 主题:共享一个FCKEditor,支持多种编程语言代码高亮,Java文件上传并UUID命名
- 传智播客-Java基础加强-day9