【问题标题】:How to get system information in windows with c++ [closed]如何使用 C++ 在 Windows 中获取系统信息 [关闭]
【发布时间】:2017-10-30 20:14:56
【问题描述】:

我想用 c++ 代码获取我的系统硬件信息。

那么如何用c++在windows中获取系统信息呢?

【问题讨论】:

  • 您在尝试获取系统信息时遇到了什么具体问题?您能否展示一些代码示例并指出有问题的行?
  • 我想您可能正在寻找WMI。但这可能只是因为(a)您的问题不明确,并且(b)WMI 是如此全面和复杂,以至于很难将其识别为获取简单信息的地方。我花了 1 个多小时在网上搜索,但我仍然不能 100% 确定我应该使用 WMI 来告诉我我拥有什么 CPU 类型和操作系统...

标签: c++ windows


【解决方案1】:

您可以使用GetSystemInfo函数获取硬件信息,例如OEM标识符、处理器类型、页面大小等。

#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "user32.lib")

void main()
{
   SYSTEM_INFO siSysInfo;

   // Copy the hardware information to the SYSTEM_INFO structure. 

   GetSystemInfo(&siSysInfo); 

   // Display the contents of the SYSTEM_INFO structure. 

   printf("Hardware information: \n");  
   printf("  OEM ID: %u\n", siSysInfo.dwOemId);
   printf("  Number of processors: %u\n", 
      siSysInfo.dwNumberOfProcessors); 
   printf("  Page size: %u\n", siSysInfo.dwPageSize); 
   printf("  Processor type: %u\n", siSysInfo.dwProcessorType); 
   printf("  Minimum application address: %lx\n", 
      siSysInfo.lpMinimumApplicationAddress); 
   printf("  Maximum application address: %lx\n", 
      siSysInfo.lpMaximumApplicationAddress); 
   printf("  Active processor mask: %u\n", 
      siSysInfo.dwActiveProcessorMask); 
}

参考文献:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724423(v=vs.85).aspx

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724381(v=vs.85).aspx

http://www.cplusplus.com/forum/windows/124624/

【讨论】:

    【解决方案2】:

    处理器信息

    使用void WINAPI GetSystemInfo(..) (MSDN)

    这将返回一个 SYSTEM_INFO 结构,其中包含:

    typedef struct _SYSTEM_INFO {
      union {
        DWORD  dwOemId;
        struct {
          WORD wProcessorArchitecture;
          WORD wReserved;
        };
      };
      DWORD     dwPageSize;
      LPVOID    lpMinimumApplicationAddress;
      LPVOID    lpMaximumApplicationAddress;
      DWORD_PTR dwActiveProcessorMask;
      DWORD     dwNumberOfProcessors;
      DWORD     dwProcessorType;
      DWORD     dwAllocationGranularity;
      WORD      wProcessorLevel;
      WORD      wProcessorRevision;
    } SYSTEM_INFO;
    

    枚举设备

    使用IPortableDeviceManager 接口枚举设备。 MSDN 详细描述了这个 API。

    这是该页面的代码 sn-p:

    // Create PortableDeviceManager
    
    HRESULT hr = CoCreateInstance(CLSID_PortableDeviceManager,
                                  NULL,
                                  CLSCTX_INPROC_SERVER,
                                  IID_PPV_ARGS(&pPortableDeviceManager));
    if (FAILED(hr))
    {
        printf("! Failed to CoCreateInstance CLSID_PortableDeviceManager, hr = 0x%lx\n",hr);
        return;
    }
    
    // Get number of devices
    
    if (SUCCEEDED(hr))
    {
        hr = pPortableDeviceManager->GetDevices(NULL, &cPnPDeviceIDs);
        if (FAILED(hr))
        {
            printf("! Failed to get number of devices on the system, hr = 0x%lx\n",hr);
        }
    }
    
    // Report the number of devices found.  NOTE: we will report 0, if an error
    // occured.
    
    printf("\n%d Windows Portable Device(s) found on the system\n\n", cPnPDeviceIDs);
    
    // Enumerate the device information
    
    if (SUCCEEDED(hr) && (cPnPDeviceIDs > 0))
    {
        pPnpDeviceIDs = new (std::nothrow) PWSTR[cPnPDeviceIDs];
        if (pPnpDeviceIDs != NULL)
        {
            DWORD dwIndex = 0;
    
            hr = pPortableDeviceManager->GetDevices(pPnpDeviceIDs, &cPnPDeviceIDs);
            if (SUCCEEDED(hr))
            {
                // For each device found, display the devices friendly name,
                // manufacturer, and description strings.
                for (dwIndex = 0; dwIndex < cPnPDeviceIDs; dwIndex++)
                {
                    printf("[%d] ", dwIndex);
                    DisplayFriendlyName(pPortableDeviceManager, pPnpDeviceIDs[dwIndex]);
                    printf("    ");
                    DisplayManufacturer(pPortableDeviceManager, pPnpDeviceIDs[dwIndex]);
                    printf("    ");
                    DisplayDescription(pPortableDeviceManager, pPnpDeviceIDs[dwIndex]);
                }
            }
            else
            {
                printf("! Failed to get the device list from the system, hr = 0x%lx\n",hr);
            }
        }
    }
    
    // Free memory used to store the information
    
    for (dwIndex = 0; dwIndex < cPnPDeviceIDs; dwIndex++)
    {
        CoTaskMemFree(pPnpDeviceIDs[dwIndex]);
        pPnpDeviceIDs[dwIndex] = NULL;
    }
    
    // Delete the array of PWSTR pointers
    delete [] pPnpDeviceIDs;
    pPnpDeviceIDs = NULL;
    

    【讨论】:

      猜你喜欢
      • 2014-06-12
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 2012-05-07
      • 1970-01-01
      • 2010-09-25
      • 2016-03-12
      相关资源
      最近更新 更多