代码语言
.
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
】
获取系统信息及性能信息
作者:
汶纡
/ 发布于
2014/11/18
/
608
#ifndef MY_WMI_CLASS_H__ #define MY_WMI_CLASS_H__ /******************************************************************************* * @file * @author def< qq group: 324164944 > * @blog http://www.cnblogs.com/itdef/ * @brief /*******************************************************************************/ #include <Wbemidl.h> namespace DEF { class CWMI { IWbemLocator *pLoc_; IWbemServices *pSvc_; public: CWMI():pLoc_(NULL),pSvc_(NULL){} ~CWMI(); BOOL QueryInfo(const char* strSQL, const char* strAttr ); BOOL Initialize(); }; } //namespace DEF #endif //MY_WMI_CLASS_H__ /******************************************************************************* * @file * @author def< qq group: 324164944 > * @blog http://www.cnblogs.com/itdef/ * @brief /*******************************************************************************/ #include "CMyWMIClass.h" #include <iostream> #include <comutil.h> using namespace DEF; using namespace std; #pragma comment(lib,"Wbemuuid") #pragma comment(lib,"comsuppw") BOOL CWMI::Initialize() { BOOL bRet = FALSE; HRESULT hres; //定义COM调用的返回 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."); } bRet = TRUE; }catch(exception& e) { cout << e.what() << endl; } return bRet; } CWMI::~CWMI() { if( NULL != pSvc_) { pSvc_->Release(); pSvc_ = NULL; } if(pLoc_ != NULL ) { pLoc_->Release(); pLoc_ = NULL; } CoUninitialize(); } BOOL CWMI::QueryInfo(const char* strSQL, const char* strAttr ) { HRESULT hres; //定义COM调用的返回 IWbemClassObject* pclsObj = NULL; IEnumWbemClassObject* pEnum = NULL; BOOL bRet = FALSE; if( NULL == strSQL || NULL == strAttr) { return FALSE; } try{ hres = pSvc_->ExecQuery( bstr_t("WQL"), bstr_t(strSQL), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnum); if (FAILED(hres)) { throw exception("ExecQuery() error."); } ULONG uReturn = 0; while( pEnum ) { HRESULT hr = pEnum->Next( WBEM_INFINITE, 1, &pclsObj, &uReturn ); if( 0 == uReturn ) { break; } VARIANT vtProp; VariantInit( &vtProp ); hr = pclsObj->Get( bstr_t( strAttr ), 0, &vtProp, 0, 0 ); if (SUCCEEDED(hr) && (V_VT(&vtProp) == VT_BSTR)) { char* str = _com_util::ConvertBSTRToString( vtProp.bstrVal ); //printf( "%s, length = %d \n", str, strlen( str ) ); printf("%s \n",str); delete [] str; } VariantClear( &vtProp ); } bRet = TRUE; }catch(exception& e) { cout << e.what() << endl; } if(pclsObj != NULL) { pclsObj->Release(); pclsObj = NULL; } if(pEnum != NULL) { pEnum->Release(); pEnum = NULL; } return bRet; } /******************************************************************************* * @file * @author def< qq group: 324164944 > * @blog http://www.cnblogs.com/itdef/ * @brief /*******************************************************************************/ #include <iostream> #include "CMyWMIClass.h" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { while(1) { Sleep(3000); DEF::CWMI a; a.Initialize(); cout << endl << "Caption "; a.QueryInfo("SELECT * FROM Win32_OperatingSystem","Caption"); cout << "Manufacturer "; a.QueryInfo("SELECT * FROM Win32_OperatingSystem","Manufacturer"); cout << "CSName "; a.QueryInfo("SELECT * FROM Win32_OperatingSystem","CSName"); cout << "PNPDeviceID "; a.QueryInfo("SELECT * FROM Win32_DiskDrive", "PNPDeviceID"); cout << "Disk SerialNumber "; a.QueryInfo("SELECT * FROM Win32_PhysicalMedia", "SerialNumber"); cout << "TotalVisibleMemorySize "; a.QueryInfo("SELECT * FROM Win32_OperatingSystem","TotalVisibleMemorySize"); cout << "FreePhysicalMemory "; a.QueryInfo("SELECT * FROM Win32_OperatingSystem","FreePhysicalMemory"); cout << "SerialNumber "; a.QueryInfo("SELECT * FROM Win32_DiskDrive","SerialNumber"); // cout << "Process Caption "; // a.QueryInfo("SELECT * FROM Win32_Process","Caption"); // // cout << "Process Handle "; // a.QueryInfo("SELECT * FROM Win32_Process","Handle"); // // // cout << "Process ExecutablePath "; // a.QueryInfo("SELECT * FROM Win32_Process","ExecutablePath"); } return 0; }
试试其它关键字
系统信息
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
汶纡
贡献的其它代码
(
55
)
.
手机类型判断
.
sql 节假日判断(春节、中秋、国庆、周末等)
.
使用LEFT OUTER JOIN 实现not in 子句
.
获取图片宽度高度、大小尺寸、图片类型、用于布局的im
.
简单的大视频截取播放功能
.
如何通過ID號來刪除DataTable中的行
.
检测远程URL是否存在
.
阻止html页面加载
.
sql生成九九乘法表
.
oracle存储遍历表中数据
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3