WQL获取PC硬件信息

一、首先是查询的表。

硬件代号为深色的,属性代号是浅色的例如(0,0)是CPU的名字
//【0】处理器ID 
0 L"Name",  
1 L"ProcessorId",   
2 L"MaxClockSpeed", //最快速度
3 L"CurrentClockSpeed", //当前速度
4 L"L2CacheSize", //二级缓存(Byte)
5 L"L3CacheSize", //三级缓存(Byte)
6 L"Manufacturer", //制造厂家
7 L"NumberOfCores", //核数
8 L"NumberOfLogicalProcessors", //线程数,逻辑处理器数量
9 L"SystemName", //系统名字
//【1】 网卡原生MAC地址 
10 L"Name",  
11 L"AdapterType", //适配器类型
12 L"GUID",  
13 L"Manufacturer",  
14 L"PNPDeviceID",   
15 L"MACAddress", // 网卡当前MAC地址
16 L"NetConnectionID",  
17 L"Speed",  
//【2】 硬盘序列号
18 L"Name",  
19 L"SerialNumber",   
20 L"BytesPerSector",  
21 L"FirmwareRevision",  
22 L"InterfaceType",  
23 L"Manufacturer",  
24 L"Model",  
25 L"Partitions", //分区个数
26 L"PNPDeviceID",   
27 L"SerialNumber",  
28 L"Signature",  
29 L"Size",  
30 L"TotalCylinders",  
31 L"TotalHeads",  
32 L"TotalSectors",  
33 L"TotalTracks",  
34 L"TracksPerCylinder",  
//【3】 主板序列号 
35 L"Manufacturer",  
36 L"Product",  // 主板型号  
37 L"SerialNumber",  
//【4】BIOS
38 L"CurrentLanguage",  
39 L"ReleaseDate",  
40 L"SerialNumber",    
41 L"SMBIOSBIOSVersion",  
42 L"Version",  
//【5】内存
43 L"Capacity" ,//容量
44 L"DeviceLocator",  
45 L"FormFactor",  
46 L"Manufacturer",  
47 L"PartNumber",  
48 L"SerialNumber",  
49 L"Speed",  
//【6】 键盘
50 L"Description",  
51 L"DeviceID",  
52 L"PNPDeviceID",  
//【7】 点输入设备,包括鼠标。 
53 L"Description",  
54 L"DeviceID",  
55 L"Manufacturer",  
56 L"PNPDeviceID",  
//【8】 光盘驱动器(57-62)
57 L"Description",  
58 L"DeviceID",  
59 L"Drive",  
60 L"MediaType",  
61 L"Manufacturer",  
62 L"PNPDeviceID",  
//【9】 串口(63-66)
63 L"Description",  
64 L"DeviceID",  
65 L"MaxBaudRate",  
66 L"PNPDeviceID",  
// 【10】串口配置 (67-67)
67 L"Description",  
// 【11】多媒体设置,一般指声卡(68-70)
68 L"Description",  
69 L"Manufacturer",  
70 L"PNPDeviceID",  
//【12】 主板插槽 (ISA & PCI & AGP)(71-71)
71 L"SlotDesignation",  
// 【13】USB 控制器 (72-74)
72 L"Description",  
73 L"Manufacturer",  
74 L"PNPDeviceID",  
// 【14】显示器 (75-80)
75 L"Description",  
76 L"MonitorManufacturer",  
77 L"MonitorType",  
78 L"PNPDeviceID",  
79 L"ScreenHeight",  
80 L"ScreenWidth",  
// 【15】显卡细节。 (81-94)
81 L"AdapterDACType",  
82 L"AdapterRAM",  
83 L"Availability",  
84 L"CurrentRefreshRate",  
85 L"CurrentScanMode",  
86 L"CurrentVerticalResolution",  
87 L"Description",  
88 L"DriverDate",  
89 L"DriverVersion",  
90 L"MaxRefreshRate",  
91 L"PNPDeviceID",  
92 L"VideoMemoryType",  
93 L"VideoModeDescription",  
94 L"VideoProcessor",  
// 【16】显卡设置(95-102)
95 L"BitsPerPixel",  
96 L"ColorPlanes",  
97 L"Description",  
98 L"DeviceSpecificPens",  
99 L"HorizontalResolution",  
100 L"RefreshRate",  
101 L"VerticalResolution",  
102 L"VideoMode",  
二、查询的代码

#define _WIN32_DCOM

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>

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

int main(int argc, char **argv)
{
    HRESULT hres;

    // 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.
    }

    // Initialize 
    hres =  CoInitializeSecurity(
        NULL,     
        -1,      // COM negotiates service                  
        NULL,    // Authentication services
        NULL,    // Reserved
        RPC_C_AUTHN_LEVEL_DEFAULT,    // authentication
        RPC_C_IMP_LEVEL_IMPERSONATE,  // 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.
    }

    // Obtain the initial locator to Windows Management
    // on a particular host computer.
    IWbemLocator *pLoc = 0;

    hres = CoCreateInstance(
        CLSID_WbemLocator,             
        0, 
        CLSCTX_INPROC_SERVER, 
        IID_IWbemLocator, (LPVOID *) &pLoc);
 
    if (FAILED(hres))
    {
        cout << "Failed to create IWbemLocator object. "
            << "Error code = 0x"
            << hex << hres << endl;
        CoUninitialize();
        return 1;       // Program has failed.
    }

    IWbemServices *pSvc = 0;

    // 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"), // WMI namespace
        NULL,                    // User name
        NULL,                    // User password
        0,                       // Locale
        NULL,                    // Security flags                 
        0,                       // Authority       
        0,                       // Context object
        &pSvc                    // 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;

    // Set the IWbemServices proxy so that impersonation
    // of the user (client) occurs.
    hres = CoSetProxyBlanket(
       
       pSvc,                         // the proxy to set
       RPC_C_AUTHN_WINNT,            // authentication service
       RPC_C_AUTHZ_NONE,             // authorization service
       NULL,                         // Server principal name
       RPC_C_AUTHN_LEVEL_CALL,       // authentication level
       RPC_C_IMP_LEVEL_IMPERSONATE,  // impersonation level
       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.
    }


    // Use the IWbemServices pointer to make requests of WMI. 
    // Make requests here:

    // For example, query for all the running processes
    IEnumWbemClassObject* pEnumerator = NULL;
    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))
    {
        cout << "Query for processes failed. "
             << "Error code = 0x" 
             << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();     
        CoUninitialize();
        return 1;               // Program has failed.
    }
    else
    { 
        IWbemClassObject *pclsObj;
        ULONG uReturn = 0;
   
        while (pEnumerator)
        {
            hres = pEnumerator->Next(WBEM_INFINITE, 1, 
                &pclsObj, &uReturn);

            if(0 == uReturn)
            {
                break;
            }

            VARIANT vtProp;

            // Get the value of the Name property
            hres = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
            wcout << "Process Name : " << vtProp.bstrVal << endl;
            VariantClear(&vtProp);
        }
         
    }
 
    // Cleanup
    // ========

    pSvc->Release();
    pLoc->Release();     
    CoUninitialize();

    return 0;   // Program successfully completed.
}

整理之后:

WMIQuery test0;

test0.Initial();
if(!test0.WQLquery(0,0,teststr))teststr="查询失败";
else cout<<"teststr";

这句得到的是CPU名字   根据上表查询。

下面给出下载地址:

整理好的类:http://download.csdn.net/detail/ssutlyh/6545023

MFC demon:http://pan.baidu.com/s/1y2j3s

附上非WQL获取CPU序列号硬盘序列号:http://pan.baidu.com/s/1rGn8C

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NetCore是一个开源的跨平台开发框架,它具有很强的灵活性和可扩展性。要获取硬件信息,可以通过使用.NetCore提供的相关类和方法来实现。 首先,需要引入System.Management命名空间,这个命名空间提供了访问WMI(Windows Management Instrumentation,Windows管理工具)的功能,可以使用WMI获取硬件信息。 接下来,可以通过创建一个ManagementObjectSearcher对象来执行WMI查询。使用ManagementObjectSearcher的构造函数指定查询的WQL语句,例如查询所有的CPU信息可以使用"SELECT * FROM Win32_Processor"。 然后,通过调用ManagementObjectSearcher的Get()方法来执行查询,并返回一个ManagementObjectCollection对象。可以遍历这个对象,获取每一个硬件信息。 例如,要获取CPU信息,可以通过遍历ManagementObjectCollection来获取每一个Win32_Processor对象,并使用这个对象的属性来获取CPU的相关信息,如Manufacturer(制造商)、Name(名称)、NumberOfCores(核心数)等。 同样的方式,还可以获取其他硬件信息,包括内存、磁盘、网卡等等。可以通过查询不同的WMI类来获取不同的硬件信息。 最后,可以将获取到的硬件信息以合适的方式展示出来,例如通过控制台输出、保存到文件或者上传到服务器等。 总而言之,通过使用.NetCore提供的System.Management命名空间和相关类,我们可以方便地获取硬件信息。这样,我们就可以在.NetCore应用程序中准确地获取和利用硬件信息,以满足不同的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值