第一种,依托WMI
#define _WIN32_DCOM #include <iostream> using namespace std; #include <comdef.h> #include <Wbemidl.h> # pragma comment(lib, "wbemuuid.lib") int main(int argc, char **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 rootcimv2 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 se 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_ComputerSystemProduct"), 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. } IWbemClassObject *pclsObj; ULONG uReturn = 0; string test; 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"UUID", 0, &vtProp, 0, 0); wcout << " OS Name : " << vtProp.bstrVal << endl; VariantClear(&vtProp); //pclsObj->Release(); } //pSvc->Release(); //pLoc->Release(); //pEnumerator->Release(); //pclsObj->Release(); //CoUninitialize(); system("PAUSE"); return 0; // Program successfully completed. }
第二种,依托GetSystemFirmwareTable
#include <Windows.h> #include <string> #include <tchar.h> typedef struct _dmi_header { BYTE type; BYTE length; WORD handle; }dmi_header; typedef struct _RawSMBIOSData { BYTE Used20CallingMethod; BYTE SMBIOSMajorVersion; BYTE SMBIOSMinorVersion; BYTE DmiRevision; DWORD Length; BYTE SMBIOSTableData[]; }RawSMBIOSData; static void dmi_system_uuid(const BYTE *p, short ver) { int only0xFF = 1, only0x00 = 1; int i; for (i = 0; i < 16 && (only0x00 || only0xFF); i++) { if (p[i] != 0x00) only0x00 = 0; if (p[i] != 0xFF) only0xFF = 0; } if (only0xFF) { printf("Not Present"); return; } if (only0x00) { printf("Not Settable"); return; } if (ver >= 0x0206) printf("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X ", p[3], p[2], p[1], p[0], p[5], p[4], p[7], p[6], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); else printf("-%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X ", p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); } const char *dmi_string(const dmi_header *dm, BYTE s) { char *bp = (char *)dm; size_t i, len; if (s == 0) return "Not Specified"; bp += dm->length; while (s > 1 && *bp) { bp += strlen(bp); bp++; s--; } if (!*bp) return "BAD_INDEX"; /* ASCII filtering */ len = strlen(bp); for (i = 0; i < len; i++) if (bp[i] < 32 || bp[i] == 127) bp[i] = '.'; return bp; } int _tmain(int argc, _TCHAR* argv[]) { DWORD bufsize = 0; BYTE buf[65536] = { 0 }; int ret = 0; RawSMBIOSData *Smbios; dmi_header *h = NULL; int flag = 1; ret = GetSystemFirmwareTable('RSMB', 0, 0, 0); if (!ret) { printf("Function failed! "); return 1; } printf("get buffer size is %d ", ret); bufsize = ret; ret = GetSystemFirmwareTable('RSMB', 0, buf, bufsize); if (!ret) { printf("Function failed! "); return 1; } Smbios = (RawSMBIOSData *)buf; BYTE *p = Smbios->SMBIOSTableData; if (Smbios->Length != bufsize - 8) { printf("Smbios length error "); return 1; } for (int i = 0; i < Smbios->Length; i++) { h = (dmi_header *)p; if (h->type == 0 && flag) { printf(" Type %02d - [BIOS] ", h->type); printf(" BIOS Vendor : %s ", dmi_string(h, p[0x4])); printf(" BIOS Version: %s ", dmi_string(h, p[0x5])); printf(" Release Date: %s ", dmi_string(h, p[0x8])); if (p[0x16] != 0xff && p[0x17] != 0xff) printf(" EC version: %d.%d ", p[0x16], p[0x17]); flag = 0; } else if (h->type == 1) { printf(" Type %02d - [System Information] ", h->type); printf(" Manufacturer: %s ", dmi_string(h, p[0x4])); printf(" Product Name: %s ", dmi_string(h, p[0x5])); printf(" Version: %s ", dmi_string(h, p[0x6])); printf(" Serial Number: %s ", dmi_string(h, p[0x7])); printf(" UUID: "); dmi_system_uuid(p + 0x8, Smbios->SMBIOSMajorVersion * 0x100 + Smbios->SMBIOSMinorVersion); printf(" SKU Number: %s ", dmi_string(h, p[0x19])); printf(" Family: %s ", dmi_string(h, p[0x1a])); } p += h->length; while ((*(WORD *)p) != 0) p++; p += 2; } getchar(); return 0; }