时光荏苒,六道轮回。
2004年,初中,欧锦赛,希腊神话,17岁的C罗哭成了泪人!
2016年,工作,欧洲杯,能否再让C罗哭成泪人?

想要获得计算机硬件的详细信息,我们可以使用WMI。

今天就作为开篇,谈一谈什么是WMI?
Windows Management Instrumentation (WMI) is a scalable system management infrastructure that uses a single, consistent, standards-based, extensible, object-oriented interface. WMI provides you with a standard way to interact with system management information and the underlying WMI APIs. WMI is used primarily by system management application developers and administrators to access and manipulate system management information.

The purpose of WMI is to provide a standardized means for managing your computer system, be it a local computer or all the computers in an enterprise. In its simplest terms, management is little more than collecting data about the state of a managed object on the computer system and altering the state of the managed object by changing the data stored about the object. A managed object can be a hardware entity, such as a memory array, port, or disk drive. It can also be a software entity, such as a service, user account, or page file.

WMI can manage the many components of a computer system. In managing a hard disk, you can use WMI to monitor the amount of free space remaining on the disk. You could also use WMI to remotely alter the state of the drive by deleting files, changing file security, or partitioning or formatting the drive.

WMI is not only a powerful tool to collect system information, it is also very easy to use. Existing scripting WMI interface makes it possible to be used for system administrators and web-designers as well as for skilled programmers.

WMI的全称是Windows Management Instrumentation,即Windows管理工具。它是Windows操作系统中管理数据和操作的基础模块。我们可以通过WMI脚本或者应用程序去管理本地或者远程计算机上的资源。对于VC和汇编程序员,想获取诸如CPU序列号和硬盘序列号等信息是非常容易的。但是对于VB以及其他一些脚本语言,想尝试获取系统中一些硬件信息可能就没那么容易了。微软为了能达到一种通用性目的(遵守某些行业标准),设计了WMI。它提供了一个通过操作系统、网络和企业环境去管理本地或远程计算机的统一接口集。应用程序和脚本语言使用这套接口集去完成任务,而不是直接通过Windows API。可能有人要问,为什么不让设计的脚本直接在底层使用Windows API,而非要弄个新的技术呢?原因是在目前Windows API中,有些是不支持远程调用或者脚本调用的。这样通过统一模型的WMI,像VB和脚本语言就可以去访问部分系统信息了。但是并不是所有脚本语言都可以使用WMI技术:它要支持ActiveX技术。

先上一个官方的例子:

#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>#pragma comment(lib, "wbemuuid.lib")int main(int argc, char **argv)
{HRESULT hres;// Step 1: --------------------------------------------------// Initialize COM. ------------------------------------------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 --------------------------hres =  CoInitializeSecurity(NULL, -1,                          // COM authenticationNULL,                        // Authentication servicesNULL,                        // ReservedRPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation  NULL,                        // Authentication infoEOAC_NONE,                   // Additional capabilities NULL                         // Reserved);if (FAILED(hres)){cout << "Failed to initialize security. Error code = 0x" << hex << hres << endl;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)){cout << "Failed to create IWbemLocator object."<< " Err code = 0x"<< hex << hres << endl;CoUninitialize();return 1;                 // Program has failed.}// Step 4: -----------------------------------------------------// Connect to WMI through the IWbemLocator::ConnectServer methodIWbemServices *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 namespaceNULL,                    // User name. NULL = current userNULL,                    // User password. NULL = current0,                       // Locale. NULL indicates currentNULL,                    // Security flags.0,                       // Authority (for example, Kerberos)0,                       // Context object &pSvc                    // pointer to IWbemServices proxy);if (FAILED(hres)){cout << "Could not connect. Error code = 0x" << hex << hres << endl;pLoc->Release();     CoUninitialize();return 1;                // Program has failed.}cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;// Step 5: --------------------------------------------------// Set security levels on the proxy -------------------------hres = CoSetProxyBlanket(pSvc,                        // Indicates the proxy to setRPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxxRPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxxNULL,                        // Server principal name RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxxNULL,                        // client identityEOAC_NONE                    // proxy capabilities );if (FAILED(hres)){cout << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl;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 systemIEnumWbemClassObject* pEnumerator = NULL;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)){cout << "Query for operating system name failed."<< " Error code = 0x" << hex << hres << endl;pSvc->Release();pLoc->Release();CoUninitialize();return 1;               // Program has failed.}// Step 7: -------------------------------------------------// Get the data from the query in step 6 -------------------IWbemClassObject *pclsObj = NULL;ULONG uReturn = 0;while (pEnumerator){HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);if(0 == uReturn){break;}VARIANT vtProp;// Get the value of the Name propertyhr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);wcout << " OS Name : " << vtProp.bstrVal << endl;VariantClear(&vtProp);pclsObj->Release();}// Cleanup// ========pSvc->Release();pLoc->Release();pEnumerator->Release();CoUninitialize();return 0;   // Program successfully completed.}

步骤:
初始化COM库
设置进程COM安全信息
创建进程内COM服务器
连接WMI命名空间
设置WMI连接的安全等级
发起WMI请求
清理

接下来的操作就是换汤不换药。

这里有个忠告,免费的:
把上面的例子代码粘贴到自己的工程中时,一定要养成review的习惯,去掉不必要的注释,不必要的空行。

上面的代码中用到了很多api,这里不再一一赘述,请参阅msdn官方文档。

这里仅仅是个开始,接下来要发生的事儿:

1 win32控制台程序中查看计算机显卡的详细信息

2 为何在GUI程序中,使用win32控制台程序同样的方法,我们无法获得正确的信息呢?

3 qt中使用WMI中遇到了哪些坑。

coming soon~~~~~

Windows客户端开发--WMI技术介绍相关推荐

  1. Windows客户端开发--获取系统mac地址(使用WMI)

    之前写过两篇博客,介绍了windows的WMI技术,以及如果通过WMI获取显卡详细信息: Windows客户端开发–WMI技术介绍 Windows客户端开发–使用WMI获取显卡详细信息(win32控制 ...

  2. WMI技术介绍和应用——VC开发WMI应用的基本步骤

    在<WMI技术介绍和应用--WMI概述>中介绍了我们可以使用C++..net或者支持ActiveX技术的脚本语言来使用WMI.但是各种语言对WMI的控制能力是不同的,比如脚本语言只能用来从 ...

  3. WMI技术介绍和应用——总结(完)

    断断续续的,历经三年将WMI这个主题给写完了.记得最开始时接触该技术,是因为传统获取CPU序列号的方法总是出错.于是接触了这种已经很老的技术.本着打破砂锅问到底的想法,我决定稍微研究一下,结果越来越深 ...

  4. WMI技术介绍和应用——事件通知

    在<WMI技术介绍和应用--WMI概述>中,我们使用了下图介绍WMI构架(转载请指明出于breaksoftware的csdn博客) 我们之前介绍的使用WMI查询系统.硬件等信息的功能,是通 ...

  5. Windows客户端开发简介(二)

    Windows客户端开发简介(二) 一个典型的Windows客户端程序要有哪几部分构成呢?下面我会以一个国内比较流行的互联网客户端程序的基本架构来跟大家逐步展开分析,由于涉及到知识产权的问题,请大家不 ...

  6. Windows客户端开发简介(一)

    在这样一个移动当道的年代,我跟大家讨论Windows客户端开发,似乎有些倚老卖老的意思了.然而我却觉得无论什么时候,Windows客户端开发其实还是有着不少实用经典的技术的.对了,确切说我是要说说Wi ...

  7. WMI技术介绍和应用——Event Provider

    在<WMI技术介绍和应用--Instance/Method Provider>一文中,我们介绍了Instance和Method Provider的编写方法.本文我们将介绍更有意思的&quo ...

  8. WMI技术介绍和应用——Instance/Method Provider

    在<WMI技术介绍和应用--事件通知>一文中,我们提到了提供者(Provider)这个概念.我们还是要引入WMI的结构图(转载请指明出于breaksoftware的csdn博客) 我们在1 ...

  9. WMI技术介绍和应用——执行方法

    在之前的博文中,我们主要介绍了如何使用WMI查询信息和接收事件.本文将介绍WMI的另一种用法--执行方法.(转载请指明出于breaksoftware的csdn博客) 这块的内容在msdn中有详细的介绍 ...

最新文章

  1. 保研软件学院和计算机学院,西南大学计算机与信息科学学院·软件学院(专业学位)计算机技术保研细则...
  2. 从字符串中提取BCD码,转换为UINT数据并返回
  3. servlet生命周期
  4. HTC Link是仅适用于日本的6DOF VR头显
  5. 支付宝扫一下就能体验的深度学习模型
  6. python邮件图片加密_Python爬虫如何应对Cloudflare邮箱加密
  7. camunda 流程执行追踪_九思OA项目管理解决方案:规范企业项目流程,掌握项目进展...
  8. CImage与OpenCV兼容问题
  9. (BFS)Meteor Shower (poj3669)
  10. 一文看懂高可用:异地多活
  11. C和C指针小记(五)-指针类型
  12. 如何调整一个 IFrame 到其内容的大小不显示滚动条[微软帮助]
  13. 三、运算符、表达式和语句
  14. Multisim破解教程
  15. POJ 2387 Dijkstra
  16. Linux - 增加用户、添加用户组
  17. 推荐算法可以做到千人千面,但是千人千面的流量利用效率一定是优于人工分发吗?
  18. 【渝粤题库】陕西师范大学292951 公司金融学Ⅱ 作业(专升本)
  19. 职场生存--向上管理
  20. finalcut剪切快捷键_final cut pro怎么用快捷键把时间线上的素材移动到入点或剪辑点...

热门文章

  1. 趣文分享:C 语言和 C++、C# 的区别在什么地方?
  2. 半导体制造中使用的蚀刻技术
  3. 推荐系统2-召回1usercf
  4. 曼孚科技丨如何通过数据服务助力数字经济发展?
  5. Python表白比心
  6. UI一揽子计划 8 (UINavigationController 、界面通信 、NSTimer  、NSUserDefaults)
  7. UI一揽子计划 16 (网络编程、HTTP协议、iOS 实现网络编程、服务器接口)
  8. 读点书:程序员数学123简谈
  9. suse linux 远程桌面,Xmanager 远程连接CentOS linux和SUSE linux
  10. 中型研发组织管理之--组织架构设置