// WMI.cpp : 定义控制台应用程序的入口点。
//

/*
    Example: Getting WMI Data From the Local Computer

http://www.dx21.com/SCRIPTING/WMI/classes.asp 中可以查看所有的类及可获得的属性

//参考
 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/example__getting_wmi_data_from_the_local_computer.asp
*/

#include "stdafx.h"

#define _WIN32_DCOM
#include <iostream>
#include <comdef.h>
#include <Wbemidl.h>

using namespace std;

# pragma comment(lib, "wbemuuid.lib")

int _tmain(int argc, _TCHAR* 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 --------------------------
    // 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))
    {
        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 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))
    {
        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 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))
    {
        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 system
    IEnumWbemClassObject* 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;
    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 property
        hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
        wcout << " OS Name : " << vtProp.bstrVal << endl;
        VariantClear(&vtProp);
    }

// Cleanup
    // ========
   
    pSvc->Release();
    pLoc->Release();
    pEnumerator->Release();
    pclsObj->Release();
    CoUninitialize();

return 0;   // Program successfully completed.
}

使用 WMI 获得计算机信息相关推荐

  1. 使用WMI对象收集计算机信息

    WMI(windows管理规范),是Windows 2K/XP管理系统的核心:对于其他的Win32操作系统,WMI是一个有用的插件.WMI以CIMOM为基础. CIMOM即公共信息模型对象管理器(Co ...

  2. Windows开启WMI时一些总结

    通过远程的方式连接WMI获取计算机信息时,可能会出现远程主机拒绝访问,这时就要通过下面的方式来开启当前计算机的WMI服务,下面以Win7和Win10为例来进行相关的说明,通过一步步排查去连接远程服务. ...

  3. android fastboot原理,Android 手机进入不了fastboot模式的解决方案

    本方案仅针对linux terminal下刷手机img文件的情况: fastboot的通常流程如下: cd out/target/product/XXX/   //进入.img文件的目录 adb re ...

  4. Visual C#中用WMI获取远程计算机信息

    如果不使用WMI,想要获取远程计算机的系统数据,最常用的方法就是在远程计算机上运行一个客户端程序,本地机通过和这个客户端程序来获取远程计算机的系 统数据.这种实现方法无论是程序设计还是后面的程序分发都 ...

  5. experiment : 使用WMI取本地计算机信息

    最近的任务中有个分支任务, 要求将客户本地计算机Windows操作系统中的软硬件信息记录下来, 给研发看, 辅助调试用. 看到WMI能做这事, codeproject上资料还没有csdn上多~ 有人反 ...

  6. C#通过WMI的wind32 的API函数实现msinfo32的本地和远程计算机的系统摘要信息查看功能...

    最近做一个项目碰到要实现查看本地和远程计算机的摘要信息,采用命令行msinfo32可以很快查看到,如下图: 需要在用C#来实现类似信息查看.尤其远程计算机的..因此通过MSDN查询到.win32的AP ...

  7. PowerShell_零基础自学课程_8_高级主题:WMI对象和COM组件

    本系列文章从最初的初识开始,基本上可以完成一些简单的系统管理了,为了更方便的管理系统,同时为了更好的发掘系统的性能,就需要用到系统提供 的一些高级特性,在Windows Server系列的OS中,如果 ...

  8. C/C++通过WMI和系统API函数获取获取系统硬件配置信息(转)

    前段时间由于项目需要,要求做一个服务器的实时性能监控(CPU.内存.网络利用率等)和读取服务器的硬件配置参数的接口供项目组使用,就是一个类似于鲁大师之类的东东吧... 当然第一想法肯定是利用Windo ...

  9. C++通过WMI获取硬件配置信息

    C++通过WMI获取硬件配置信息 WMI即Windows管理规范.通过它可以访问.配置.管理和监视几乎所有的Windows资源. WMI提供程序在WMI和托管资源之间扮演着中间方的角色.提供程序代表使 ...

最新文章

  1. 在VS中用正则表达式查找或替换
  2. itertools mode 之 combinations用法
  3. 【学习】026 Zookeeper
  4. html5 测评游戏,暗黑之王评测:HTML5游戏铸就最华丽ARPG冒险
  5. 能不能翻译PHP网站源码,有朋友可以帮忙用PHP翻译一段PYTHON代码吗?
  6. PS5运行Linux,索尼发布最新驱动!PS5手柄现在已支持Linux系统
  7. 本周论文推荐(迁移学习、图神经网络)
  8. python yiled
  9. 分析咪蒙1013篇文章,300多万字,她凭什么会火?
  10. arpspoof: libnet_check_iface() ioctl: No such device 解决方法
  11. 双曲函数奇偶性_[快乐数学]双曲函数(二)
  12. Doom3证明了“保持简单”有效。
  13. APM32 系列 MCU 获得 IAR Embedded Workbench 和 SEGGER J-Link Debug Probes 的全面支持
  14. CTF-隐写术(三)
  15. vue中公告消息横向无缝循环滚动
  16. selenium自动化图片不加载设置
  17. win11共享打印机无法连接怎么办
  18. 已解决 vmware 虚拟机安装后没有虚拟网卡问题
  19. [C语言]——矩阵的转置
  20. 文本编辑--程序员专属技能

热门文章

  1. uni-app端用户名密码加密解密。后端解密方法。
  2. PayPal买家以”信用卡被盗刷”发起未授权争议要求退款怎么办?
  3. 计算机中二进制有小数吗,计算机中的二进制小数
  4. 拒绝服务攻击的常见类型
  5. 为图书出版带来第二春的,正是AI!
  6. SQLite基本用法
  7. mysql数据库快速传输方案_MySQL数据库迁移快速导出导入大量数据
  8. 一些OJ的排序题(冒泡排序的一般形式)
  9. sql 统计7天内 每天的总数量
  10. 2021年T电梯修理考试平台及T电梯修理考试申请表