代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
获取硬件信息
作者:
defddr
/ 发布于
2014/11/13
/
752
/******************************************************************************* * @file * @author def< qq group: 324164944 > * @blog http://www.cnblogs.com/itdef/ * @brief /*******************************************************************************/ #include "stdafx.h" #include <iostream> #include <iomanip> #include <atlstr.h> #include <comutil.h> #include <comdef.h> #include <Wbemidl.h> using namespace std; #pragma comment(lib, "wbemuuid.lib") #pragma comment(lib, "comsuppw.lib") //=================================================== class CMyWMI{ IWbemLocator *pLoc_; IWbemServices *pSvc_; void GetInfo(WCHAR* wszQueryInfo,IWbemClassObject *pclsObj); public: CMyWMI():pLoc_(NULL),pSvc_(NULL){} ~CMyWMI(){ ClearWMI(); } bool InitWMI(); bool ClearWMI(); bool QuerySystemInfo(); bool QueryCPUInfo(); bool QueryProcessInfo(); bool QueryDiskInfo(); }; bool CMyWMI::QueryDiskInfo() { HRESULT hres; //定义COM调用的返回 IEnumWbemClassObject* pEnumerator = NULL; bool bRet = false; try{ hres = pSvc_->ExecQuery( bstr_t("WQL"), bstr_t("SELECT * FROM Win32_LogicalDisk"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if (FAILED(hres)) { throw exception("ExecQuery() error."); } while (pEnumerator) { IWbemClassObject *pclsObj; ULONG uReturn = 0; HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if(0 == uReturn) { break; } GetInfo(L"Name",pclsObj); GetInfo(L"VolumeName",pclsObj); GetInfo(L"FileSystem",pclsObj); GetInfo(L"VolumeSerialNumber",pclsObj); GetInfo(L"FreeSpace",pclsObj); ; cout << endl; pclsObj->Release(); } }catch(exception& e) { cout << e.what() << endl; if(pEnumerator != NULL) { pEnumerator->Release(); pEnumerator = NULL; } return bRet; } if(pEnumerator != NULL) { pEnumerator->Release(); pEnumerator = NULL; } bRet = true; return bRet; } bool CMyWMI::QueryProcessInfo() { HRESULT hres; //定义COM调用的返回 IEnumWbemClassObject* pEnumerator = NULL; bool bRet = false; try{ hres = pSvc_->ExecQuery( bstr_t("WQL"), bstr_t("SELECT * FROM Win32_Process"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if (FAILED(hres)) { throw exception("ExecQuery() error."); } while (pEnumerator) { IWbemClassObject *pclsObj; ULONG uReturn = 0; HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if(0 == uReturn) { break; } GetInfo(L"Caption",pclsObj); GetInfo(L"ProcessId",pclsObj); GetInfo(L"ExecutablePath",pclsObj); cout << endl; pclsObj->Release(); } }catch(exception& e) { cout << e.what() << endl; if(pEnumerator != NULL) { pEnumerator->Release(); pEnumerator = NULL; } return bRet; } if(pEnumerator != NULL) { pEnumerator->Release(); pEnumerator = NULL; } bRet = true; return bRet; } void CMyWMI::GetInfo(WCHAR* wszQueryInfo,IWbemClassObject *pclsObj) { if(wszQueryInfo == NULL || NULL == pclsObj) return; VARIANT vtProp; char* lpszText = NULL; HRESULT hr = pclsObj->Get(wszQueryInfo, 0, &vtProp, 0, 0); if (SUCCEEDED(hr) && (V_VT(&vtProp) == VT_BSTR)) { lpszText = _com_util::ConvertBSTRToString(V_BSTR(&vtProp)); printf_s("%s\n", lpszText); }else if( SUCCEEDED(hr) && (V_VT(&vtProp) == VT_I1 || V_VT(&vtProp) == VT_I2 || V_VT(&vtProp) == VT_I4 || V_VT(&vtProp) == VT_I8 || V_VT(&vtProp) == VT_INT) ) { printf_s("%d\n", vtProp.intVal); } delete[] lpszText; VariantClear(&vtProp); } bool CMyWMI::QueryCPUInfo() { HRESULT hres; //定义COM调用的返回 IEnumWbemClassObject* pEnumerator = NULL; bool bRet = false; try{ hres = pSvc_->ExecQuery( bstr_t("WQL"), bstr_t("SELECT * FROM Win32_Processor"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if (FAILED(hres)) { throw exception("ExecQuery() error."); } while (pEnumerator) { IWbemClassObject *pclsObj; ULONG uReturn = 0; HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if(0 == uReturn) { break; } GetInfo(L"Name",pclsObj); GetInfo(L"ProcessorId",pclsObj); GetInfo(L"Manufacturer",pclsObj); pclsObj->Release(); } }catch(exception& e) { cout << e.what() << endl; if(pEnumerator != NULL) { pEnumerator->Release(); pEnumerator = NULL; } return bRet; } if(pEnumerator != NULL) { pEnumerator->Release(); pEnumerator = NULL; } bRet = true; return bRet; } bool CMyWMI::QuerySystemInfo() { HRESULT hres; //定义COM调用的返回 IEnumWbemClassObject* pEnumerator = NULL; bool bRet = false; try{ hres = pSvc_->ExecQuery( bstr_t("WQL"), bstr_t("SELECT * FROM Win32_OperatingSystem"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if (FAILED(hres)) { throw exception("ExecQuery() error."); } while (pEnumerator) { IWbemClassObject *pclsObj; ULONG uReturn = 0; HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if(0 == uReturn) { break; } GetInfo(L"BootDevice",pclsObj); GetInfo(L"Caption",pclsObj); GetInfo(L"Manufacturer",pclsObj); GetInfo(L"CSName",pclsObj); GetInfo(L"WindowsDirectory",pclsObj); GetInfo(L"SystemDirectory",pclsObj); GetInfo(L"TotalVisibleMemorySize",pclsObj); GetInfo(L"FreePhysicalMemory",pclsObj); pclsObj->Release(); } }catch(exception& e) { cout << e.what() << endl; if(pEnumerator != NULL) { pEnumerator->Release(); pEnumerator = NULL; } return bRet; } if(pEnumerator != NULL) { pEnumerator->Release(); pEnumerator = NULL; } bRet = true; return bRet; } bool CMyWMI::ClearWMI() { bool bRet = false; if( NULL != pSvc_) pSvc_->Release(); if(pLoc_ != NULL ) pLoc_->Release(); CoUninitialize(); bRet = true; return bRet; } bool CMyWMI::InitWMI() { HRESULT hres; //定义COM调用的返回 bool bRet = false; try{ hres = CoInitializeEx(0, COINIT_MULTITHREADED); if (FAILED(hres)) { throw exception("CoInitializeEx() error."); } 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)) { throw exception("CoInitializeEx() error."); } hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc_); if (FAILED(hres)) { throw exception("CoCreateInstance() error."); } // 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)) { throw exception("ConnectServer() error."); } 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)) { throw exception("CoSetProxyBlanket() error."); } }catch(exception& e) { cout << e.what() << endl; return bRet; } bRet = true; return bRet; } //========================================================= int _tmain(int argc, _TCHAR* argv[]) { CMyWMI myWMI; myWMI.InitWMI(); myWMI.QueryDiskInfo(); return 0; }
试试其它关键字
硬件信息
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
defddr
贡献的其它代码
(
6
)
.
双击获取公网IP到剪贴板
.
实现无极限增删改查连接数据同步数据库
.
获取硬件信息
.
消息队列示例
.
统计一个目录下的子目录的Size
.
命令行 UDP 获取局域网在线用户
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3