I am looking to retrieve the UUID of a machine.
I want the equivalent of this, but using the Win32 API instead:
wmic csproduct get "UUID"
I don't want a dependency on WMI.
Here's the code example:(Use GetSystemFirmwareTable )
GetSystemFirmwareTable: Retrieves the specified firmware table from the firmware table provider.
#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\n",
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\n",
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!\n");
return 1;
}
printf("get buffer size is %d\n", ret);
bufsize = ret;
ret = GetSystemFirmwareTable('RSMB', 0, buf, bufsize);
if (!ret)
{
printf("Function failed!\n");
return 1;
}
Smbios = (RawSMBIOSData *)buf;
BYTE *p = Smbios->SMBIOSTableData;
if (Smbios->Length != bufsize - 8)
{
printf("Smbios length error\n");
return 1;
}
for (int i = 0; i < Smbios->Length; i++) {
h = (dmi_header *)p;
if (h->type == 0 && flag) {
printf("\nType %02d - [BIOS]\n", h->type);
printf("\tBIOS Vendor : %s\n", dmi_string(h, p[0x4]));
printf("\tBIOS Version: %s\n", dmi_string(h, p[0x5]));
printf("\tRelease Date: %s\n", dmi_string(h, p[0x8]));
if (p[0x16] != 0xff && p[0x17] != 0xff)
printf("\tEC version: %d.%d\n", p[0x16], p[0x17]);
flag = 0;
}
else if (h->type == 1) {
printf("\nType %02d - [System Information]\n", h->type);
printf("\tManufacturer: %s\n", dmi_string(h, p[0x4]));
printf("\tProduct Name: %s\n", dmi_string(h, p[0x5]));
printf("\tVersion: %s\n", dmi_string(h, p[0x6]));
printf("\tSerial Number: %s\n", dmi_string(h, p[0x7]));
printf("\tUUID: "); dmi_system_uuid(p + 0x8, Smbios->SMBIOSMajorVersion * 0x100 + Smbios->SMBIOSMinorVersion);
printf("\tSKU Number: %s\n", dmi_string(h, p[0x19]));
printf("\tFamily: %s\n", dmi_string(h, p[0x1a]));
}
p += h->length;
while ((*(WORD *)p) != 0) p++;
p += 2;
}
getchar();
return 0;
}
The Machine UUID is defined by SMBIOS v2.1+ (System Management BIOS (SMBIOS)) in the System Information (Type 1) structure.
Although you can retrieve raw SMBIOS data using GetSystemFirmwareTable() , the Windows SMBIOS driver actually writes most of the Type 1 defined values in the registry under HKLM\\HARDWARE\\DESCRIPTION\\System\\BIOS\\ComputerHardwareId
.
Simply reading the registry value will give you the Machine UUID without bothering going through the raw SMBIOS data.
Here is snippet of a Win32 function that should just retrieve the Machine's UUID. (untested)
#include <windows.h>
#include <winerror.h>
static HRESULT GetMachineUUID(
/* [out] */ WCHAR **ppszMachineUUID)
{
HRESULT hr = S_OK;
LPWSTR pszMachineUUID = NULL;
DWORD cbMachineUUID = 0;
DWORD cchMachineUUID = 0;
LONG lResult = ERROR_SUCCESS;
lResult = RegGetValue(HKEY_LOCAL_MACHINE,
L"HARDWARE\\DESCRIPTION\\System\\BIOS",
L"ComputerHardwareId",
RRF_RT_REG_SZ, NULL,
pszMachineUUID,
&cbMachineUUID);
if (ERROR_MORE_DATA == lResult || ERROR_SUCCESS == lResult)
{
cchMachineUUID = (cbMachineUUID / sizeof(WCHAR));
pszMachineUUID = SysAllocStringLen(NULL, cchMachineUUID);
if (pszMachineUUID == NULL)
{
hr = E_OUTOFMEMORY;
goto Error;
}
lResult = RegGetValue(HKEY_CLASSES_ROOT,
bstrNodeName,
sc_szV2CSPNodePathRegValueName,
RRF_RT_REG_SZ,
NULL,
pszMachineUUID,
&cbMachineUUID);
hr = HRESULT_FROM_WIN32(lResult);
if (FAILED(hr))
{
goto Error;
}
}
else
{
hr = HRESULT_FROM_WIN32(lResult);
if (FAILED(hr))
{
goto Error;
}
}
*ppszMachineUUID = pszMachineUUID;
pszMachineUUID = NULL;
Error:
SysFreeString(pszMachineUUID);
return hr;
}
I havn't tested the code above, but it should work.
See the latest SMBIOS reference specification posted at https://www.dmtf.org/standards/smbios
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.