如何利用wmi 获取系统版本信息,操作系统信息, cpu占用,内存,以及电脑硬件信息
如,硬盘大小, 显卡信息等、小弟刚刚接触wmi, 但是 工作中要用到, 一时不知所措, 希望各位指点指点, 有资料, 代码什么的也请发上来参考一下, 谢谢。

解决方案 »

  1.   

    不懂,网上搜了一下http://hi.baidu.com/green_wolf/blog/item/6d95cd3fea1e3dc27d1e71cc.html
      

  2.   

    就是VC编程 实现  利用WMI 获取系统信息。
      

  3.   


    #include "stdafx.h"
    #define _WIN32_DCOM
    #include <iostream>
    using namespace std;
    #include <comdef.h>
    #include <Wbemidl.h>
    #include <conio.h>
    # pragma comment(lib, "wbemuuid.lib")
    BOOL ManageWMI();
    int _tmain(int argc, _TCHAR* argv[])
    {
    if(!ManageWMI()) printf("%WMI Error!");
    _getch();
    return 0;
    }
    BOOL ManageWMI()
    {
    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_NetworkAdapterConfiguration WHERE IPEnabled = ’TRUE’"),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    NULL,
    &pEnumerator);
    if (FAILED(hres))
    {
    cout << "Query for Network Adapter Configuration 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;
    VariantInit(&vtProp);
    //hr = pclsObj->Get(L"IPSubnet", 0, &vtProp, 0, 0);
    // Get the value of the Enable property
    hr = pclsObj->Get(L"IPEnabled", 0, &vtProp, 0, 0);
    // Get the value of the MacAddress property
    if(vtProp.boolVal)
    {
    hr = pclsObj->Get(L"MacAddress", 0, &vtProp, 0, 0);
    wcout << " MacAddress : " << vtProp.bstrVal << endl;
    }
    hr = pclsObj->Get(L"IPEnabled", 0, &vtProp, 0, 0);
    if(vtProp.boolVal)
    {
    LONG lstart, lend;
    LONG idx = -1;
    BSTR* pbstr;
    SAFEARRAY *sa;
    hr = pclsObj->Get(L"Description", 0, &vtProp, 0, 0);
    if(!FAILED(hr))
    {
    wcout << "Description: " << vtProp.bstrVal << endl;
    }
    hr = pclsObj->Get(L"DNSHostName", 0, &vtProp, 0, 0);
    if(!FAILED(hr))
    {
    cout << "DNS:" << vtProp.bstrVal << endl;
    }
    hr = pclsObj->Get(L"IPAddress", 0, &vtProp, 0, 0);
    if(!FAILED(hr))
    {
    SAFEARRAY *psa = vtProp.parray;
    cout << "IP Address::" << psa << endl;
    }
    }
    VariantClear(&vtProp);
    }
    // Cleanup
    // ========
    pSvc->Release();
    pLoc->Release();
    pEnumerator->Release();
    pclsObj->Release();
    CoUninitialize();
    return 0; // Program successfully completed.
    }
      

  4.   

    to Tr0j4n:我想知道获取CPU当前占用率, 如果是多核处理器的话怎么获取?  网卡信息业一样需要考虑多个网卡,网络流入流出速率等
      

  5.   

    WMI是Windows上做运维管理最好用的方式
    你也可以用SNMP啥~
      

  6.   

    他贴的这个代码一看就是MSDN中的。
      

  7.   

    您的这个工作跟我刚刚做的差不多。我读到的信息要比您的多。像BIOS版本,什么的都有。
    这些信息仅靠WMI是不够的。并且能用其他方法的就不要用WMI,WMI的很多方法都随系统变化,并且有增减。
    我主要用SetUpDi系列函数+部分API+DeviceIOControl+汇编调用+……
      

  8.   


    能把你获取系统信息,和性能信息的代码给我参考小吗? 谢谢, 
    Email [email protected]
      

  9.   

    获得CPU占用率根本不需要WMI。用PDH计数器就行了。
    HQUERY hQuery;
        PDH_STATUS pdhStatus = PdhOpenQuery(0, 0, &hQuery);
        HCOUNTER pCounterHandle;
        PDH_FMT_COUNTERVALUE fmtValue;
        DWORD dwctrType;
        pdhStatus = PdhAddCounter(hQuery, _TEXT("\\Process(conhost)\\% Processor Time"), 0, &pCounterHandle);
        pdhStatus = PdhCollectQueryData(hQuery);
        Sleep(1000);
        pdhStatus = PdhCollectQueryData(hQuery);
        pdhStatus = PdhGetFormattedCounterValue(pCounterHandle, PDH_FMT_DOUBLE, &dwctrType, &fmtValue);
        pdhStatus = PdhCloseQuery(hQuery);
      

  10.   


    问题是我现在就是必须要用WMI, 呵呵