[source:Cprogramming]
you could check out DispHelper, especially the wmi.c sample. DispHelper allows you to use the WMI scripting interfaces, which are vastly easier to use than the C/C++ interfaces.
This is a simple sample to show how to get drive serial numbers.
Code:
#include "disphelper.h" #include <stdio.h> int main(void) { DISPATCH_OBJ(wmiSvc); DISPATCH_OBJ(colMedia); dhInitialize(TRUE); dhToggleExceptions(TRUE); dhGetObject(L"winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2", NULL, &wmiSvc); dhGetValue(L"%o", &colMedia, wmiSvc, L".ExecQuery(%S)", L"SELECT * FROM Win32_PhysicalMedia"); FOR_EACH(wmiMediaItem, colMedia, NULL) { char *pszSerial = NULL; char *pszTag = NULL; dhGetValue(L"%s", &pszTag, wmiMediaItem, L".Tag"); dhGetValue(L"%s", &pszSerial, wmiMediaItem, L".SerialNumber"); printf("The serial number of '%s' is '%s'.\n", pszTag, pszSerial); dhFreeString(pszTag); dhFreeString(pszSerial); } NEXT(wmiMediaItem); SAFE_RELEASE(colMedia); SAFE_RELEASE(wmiSvc); dhUninitialize(TRUE); getchar(); return 0; }
which will return something like:
Code:
The serial number of '\\.\PHYSICALDRIVE0' is 'WD-WM48D7214546'. The serial number of '\\.\PHYSICALDRIVE1' is 'WD-WM3493798728'.
However, the Win32_PhysicalMedia class is only available on WinXP and Win2003 and requires administrator access. For a more universal (and vastly more complex) solution, you may wish to look at the links below.
Get Hard Disk ID
Get Harddisk Information
EDIT: Make sure to remove the spaces that the board software has inserted in the word « root » in the sample code.
That all seems rather excessive. Why not just use the API salem provided?
Code:
#include <stdio.h> #include <windows.h> DWORD dwVolSerial; BOOL bIsRetrieved; int main() { bIsRetrieved = GetVolumeInformation("C:\\",NULL,NULL,&dwVolSerial,NULL,NULL,NULL,NULL); if (bIsRetrieved) { printf("Serial number of drive C is %X\n",dwVolSerial); } else { printf("Could not retrieve\n"); } return 0; }
returns:
Code:
Serial number of drive C is D403B623
That’s fine, if you want the volume serial number. However, the volume serial number is not the hard disk serial number and can change, either by formatting or with a simple utility.
The volume serial number can also be retrieved with WMI.
Code:
#include "disphelper.h" #include <stdio.h> int main(void) { DISPATCH_OBJ(wmiDisk); char* pszSerial = NULL; dhInitialize(TRUE); dhToggleExceptions(TRUE); dhGetObject(L"winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2:Win32_LogicalDisk='C:'", NULL, &wmiDisk); dhGetValue(L"%s", &pszSerial, wmiDisk, L".VolumeSerialNumber"); printf("The serial number of 'C:' is %s\n", pszSerial); dhFreeString(pszSerial); SAFE_RELEASE(wmiDisk); dhUninitialize(TRUE); getchar(); return 0; }
The cool thing about WMI is that you can change the dot in the connection string to a computer name and get the same information from a remote computer without any other changes to your code. Try doing that with most WinApi functions.
EDIT: Remove the spaces in the word « root » in the sample code to compile.