这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » STM32 » Windows 下读取DMI信息的方法

共1条 1/1 1 跳转至

Windows 下读取DMI信息的方法

高工
2017-12-27 12:04:14     打赏
在Windows下读取DMI信息的方法:使用Windows自带的函数完成:GetSystemFirmwareTable():
系统要求在:Windows Vista, Windows XP Professional x64 Edition以上[plain] view plain copy
  1. // wdmi.cpp : Defines the entry point for the console application.  

  2. // Open source.... you can refer to below code....  

  3.   

  4. #include "stdafx.h"  

  5. #include   

  6. #include   

  7. #include   

  8.   

  9. typedef struct _dmi_header  

  10. {  

  11.     BYTE type;  

  12.     BYTE length;  

  13.     WORD handle;  

  14. }dmi_header;  

  15.   

  16. typedef struct _RawSMBIOSData  

  17. {  

  18.     BYTE    Used20CallingMethod;  

  19.     BYTE    SMBIOSMajorVersion;  

  20.     BYTE    SMBIOSMinorVersion;  

  21.     BYTE    DmiRevision;  

  22.     DWORD   Length;  

  23.     BYTE    SMBIOSTableData[];  

  24. }RawSMBIOSData;  

  25.   

  26. static const char *dmi_chassis_type(BYTE code)  

  27. {  

  28.     /* 7.4.1 */  

  29.     static const char *type[] = {  

  30.         "Other", /* 0x01 */  

  31.         "Unknown",  

  32.         "Desktop",  

  33.         "Low Profile Desktop",  

  34.         "Pizza Box",  

  35.         "Mini Tower",  

  36.         "Tower",  

  37.         "Portable",  

  38.         "Laptop",  

  39.         "Notebook",  

  40.         "Hand Held",  

  41.         "Docking Station",  

  42.         "All In One",  

  43.         "Sub Notebook",  

  44.         "Space-saving",  

  45.         "Lunch Box",  

  46.         "Main Server Chassis", /* CIM_Chassis.ChassisPackageType says "Main System Chassis" */  

  47.         "Expansion Chassis",  

  48.         "Sub Chassis",  

  49.         "Bus Expansion Chassis",  

  50.         "Peripheral Chassis",  

  51.         "RAID Chassis",  

  52.         "Rack Mount Chassis",  

  53.         "Sealed-case PC",  

  54.         "Multi-system",  

  55.         "CompactPCI",  

  56.         "AdvancedTCA",  

  57.         "Blade",  

  58.         "Blade Enclosing" /* 0x1D */  

  59.     };  

  60.   

  61.     code &= 0x7F; /* bits 6:0 are chassis type, 7th bit is the lock bit */  

  62.   

  63.     if (code >= 0x01 && code <= 0x1D)  

  64.         return type[code - 0x01];  

  65.     return "unknown";  

  66. }  

  67.   

  68. static void dmi_system_uuid(const BYTE *p, short ver)  

  69. {  

  70.     int only0xFF = 1, only0x00 = 1;  

  71.     int i;  

  72.   

  73.     for (i = 0; i < 16 && (only0x00 || only0xFF); i++)  

  74.     {  

  75.         if (p[i] != 0x00) only0x00 = 0;  

  76.         if (p[i] != 0xFF) only0xFF = 0;  

  77.     }  

  78.   

  79.     if (only0xFF)  

  80.     {  

  81.         printf("Not Present");  

  82.         return;  

  83.     }  

  84.     if (only0x00)  

  85.     {  

  86.         printf("Not Settable");  

  87.         return;  

  88.     }  

  89.   

  90.     /*  

  91.      * As of version 2.6 of the SMBIOS specification, the first 3  

  92.      * fields of the UUID are supposed to be encoded on little-endian.  

  93.      * The specification says that this is the defacto standard,  

  94.      * however I've seen systems following RFC 4122 instead and use  

  95.      * network byte order, so I am reluctant to apply the byte-swapping  

  96.      * for older versions.  

  97.      */  

  98.     if (ver >= 0x0206)  

  99.         printf("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",  

  100.             p[3], p[2], p[1], p[0], p[5], p[4], p[7], p[6],  

  101.             p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);  

  102.     else  

  103.         printf("-%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",  

  104.             p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],  

  105.             p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);  

  106. }  

  107.   

  108. const char *dmi_string(const dmi_header *dm, BYTE s)  

  109. {  

  110.     char *bp = (char *)dm;  

  111.     size_t i, len;  

  112.   

  113.     if (s == 0)  

  114.         return "Not Specified";  

  115.   

  116.     bp += dm->length;  

  117.     while (s > 1 && *bp)  

  118.     {  

  119.         bp += strlen(bp);  

  120.         bp++;  

  121.         s--;  

  122.     }  

  123.   

  124.     if (!*bp)  

  125.         return "BAD_INDEX";  

  126.   

  127.     /* ASCII filtering */  

  128.     len = strlen(bp);  

  129.     for (i = 0; i < len; i++)  

  130.         if (bp[i] < 32 || bp[i] == 127)  

  131.             bp[i] = '.';  

  132.   

  133.     return bp;  

  134. }  

  135.   

  136. int _tmain(int argc, _TCHAR* argv[])  

  137. {  

  138.     DWORD bufsize = 0;  

  139.     BYTE buf[65536] = {0};  

  140.     int ret = 0;  

  141.     RawSMBIOSData *Smbios;  

  142.     dmi_header *h = NULL;  

  143.     int flag = 1;  

  144.       

  145.     ret = GetSystemFirmwareTable('RSMB', 0, 0, 0);  

  146.     if (!ret)  

  147.     {  

  148.         printf("Function failed!\n");  

  149.         return 1;  

  150.     }  

  151.   

  152.     printf("get buffer size is %d\n", ret);  

  153.     bufsize = ret;  

  154.   

  155.     ret = GetSystemFirmwareTable('RSMB', 0, buf, bufsize);  

  156.     if (!ret)  

  157.     {  

  158.         printf("Function failed!\n");  

  159.         return 1;  

  160.     }  

  161.   

  162.     Smbios = (RawSMBIOSData *)buf;  

  163.   

  164.     BYTE *p = Smbios->SMBIOSTableData;  

  165.   

  166.     if (Smbios->Length != bufsize -8 )  

  167.     {  

  168.         printf("Smbios length error\n");  

  169.         return 1;  

  170.     }  

  171.   

  172.     for(int i =0; iLength; i++){  

  173.         h = (dmi_header *)p;  

  174.   

  175.         if (h->type == 0 && flag) {  

  176.             printf("\nType %02d - [BIOS]\n", h->type);  

  177.             printf("\tBIOS Vendor : %s\n", dmi_string(h, p[0x4]));  

  178.             printf("\tBIOS Version: %s\n", dmi_string(h, p[0x5]));  

  179.             printf("\tRelease Date: %s\n", dmi_string(h, p[0x8]));  

  180.             if(p[0x16]!=0xff && p[0x17]!=0xff)  

  181.                 printf("\tEC version: %d.%d\n", p[0x16], p[0x17]);  

  182.   

  183.             flag = 0;  

  184.         }  

  185.   

  186.         else if (h->type == 1) {  

  187.             printf("\nType %02d - [System Information]\n", h->type);  

  188.             printf("\tManufacturer: %s\n", dmi_string(h, p[0x4]));  

  189.             printf("\tProduct Name: %s\n", dmi_string(h, p[0x5]));  

  190.             printf("\tVersion: %s\n", dmi_string(h, p[0x6]));  

  191.             printf("\tSerial Number: %s\n", dmi_string(h, p[0x7]));  

  192.             printf("\tUUID: "); dmi_system_uuid(p+0x8, Smbios->SMBIOSMajorVersion*0x100+Smbios->SMBIOSMinorVersion);  

  193.             printf("\tSKU Number: %s\n", dmi_string(h, p[0x19]));  

  194.             printf("\tFamily: %s\n", dmi_string(h, p[0x1a]));  

  195.         }  

  196.   

  197.         else if (h->type == 2) {  

  198.             printf("\nType %02d - [System Information]\n", h->type);  

  199.             printf("\tManufacturer: %s\n", dmi_string(h, p[0x4]));  

  200.             printf("\tProduct: %s\n", dmi_string(h, p[0x5]));  

  201.             printf("\tVersion: %s\n", dmi_string(h, p[0x6]));  

  202.             printf("\tSerial Number: %s\n", dmi_string(h, p[0x7]));  

  203.             printf("\tAsset Tag: %s\n", dmi_string(h, p[0x8]));  

  204.             printf("\tLocation in Chassis: %s\n", dmi_string(h, p[0x0a]));  

  205.         }  

  206.   

  207.         else if (h->type == 3) {  

  208.             printf("\nType %02d - [System Enclosure or Chassis]\n", h->type);  

  209.             printf("\tManufacturer: %s\n", dmi_string(h, p[0x04]));  

  210.             printf("\tType: %s\n", dmi_chassis_type(p[0x05]));  

  211.             printf("\tVersion: %s\n", dmi_string(h, p[0x06]));  

  212.             printf("\tSerial Number: %s\n", dmi_string(h, p[0x07]));  

  213.             printf("\tAsset Tag Number: %s\n", dmi_string(h, p[0x08]));  

  214.             printf("\tVersion: %s\n", dmi_string(h, p[0x10]));  

  215.         }  

  216.   

  217.         else if(h->type == 4){  

  218.             printf("\nType %02d - [Processor Information]\n", h->type);  

  219.             printf("\tSocket Designation: %s\n", dmi_string(h, p[0x04]));  

  220.             printf("\tProcessor Manufacturer: %s\n", dmi_string(h, p[0x07]));  

  221.             printf("\tProcessor Version: %s\n", dmi_string(h, p[0x10]));  

  222.             printf("\tVoltage: %d (Bit0 - 5v, Bit1 - 3.3v, Bit2 - 2.9v)\n", p[0x11]&0x7);  

  223.   

  224.             printf("\tExternal Clock: %d MHz\n", p[0x12]+p[0x13]*0x100);  

  225.             printf("\tMax Speed: %d MHz\n", p[0x14]+p[0x15]*0x100);  

  226.             printf("\tCurrent Speed: %d MHz\n", p[0x16]+p[0x17]*0x100);  

  227.             printf("\tSerial Number: %s\n", dmi_string(h, p[0x20]));  

  228.             printf("\tAsset Tag: %s\n", dmi_string(h, p[0x21]));  

  229.             printf("\tPart Number: %s\n", dmi_string(h, p[0x22]));  

  230.         }  

  231.   

  232.         else if (h->type == 17) {  

  233.             if (p[0xc]+p[0xd]*0x100 == 0)  

  234.                 continue;  

  235.             printf("\nType %02d - [Memory]\n", h->type);  

  236.             printf("\tTotal Width: %d\n", p[0x8]);  

  237.             printf("\tData Width:%d\n",   p[0xa]);  

  238.             printf("\tSize: %d MB\n",   p[0xc]+p[0xd]*0x100);  

  239.             printf("\tSpeed: %dMHz\n",  p[0x15]+p[0x16]*0x100);  

  240.             printf("\tBank Locator: %s\n", dmi_string(h, p[0x11]));  

  241.             printf("\tManufacturer: %s\n", dmi_string(h, p[0x17]));  

  242.             printf("\tSerial Number: %s\n", dmi_string(h, p[0x18]));  

  243.             printf("\tAsset Tag: %s\n", dmi_string(h, p[0x19]));  

  244.             printf("\tPart Number: %s\n", dmi_string(h, p[0x1A]));  

  245.         }  

  246.           

  247.         p += h->length;  

  248.         while((*(WORD *)p)!=0) p++;  

  249.         p += 2;  

  250.     }  

  251.   

  252.     getchar();  

  253.     return 0;  

  254. }  




共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]