How can I get the Hard disk serial number??
How can I get the Hard disk serial number??
Start by saying which OS and compiler you have
Windows with GCC
Nice
Windows could be win95, win98, winME, WinNT, Win2K, WinXP
GCC could be Dev-C++, MinGW, DJGPP, CygWin
A random guess at an API call would be
http://msdn.microsoft.com/library/de...nformation.asp
I'm using MinGW and WindowsXP
Do you know how to use WMI in "C pure"?? I found this link : http://msdn.microsoft.com/library/de...pplication.asp, but is c++.
Doesn't look too heavily dependent on C++.
Just change all the cout for printf() calls, and move all the variable declarations to the start of main(), and you should be pretty close to having something working.
>> Do you know how to use WMI in "C pure"?? <<
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.
which will return something like: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; }
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.Code:The serial number of '\\.\PHYSICALDRIVE0' is 'WD-WM48D7214546'. The serial number of '\\.\PHYSICALDRIVE1' is 'WD-WM3493798728'.
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.
Last edited by anonytmouse; 01-04-2005 at 02:45 PM.
Can you help me to compile in MinGW???
I've been compiling with mingw 3.1.0 on xp without any problem using this batch file
Code:gcc.exe -c disphelper.c -o disphelper.o -I"C:/MinGW/include" gcc.exe -c %1 -o %1.o -I"C:/MinGW/include" g++.exe %1.o disphelper.o -o %1.exe -L"C:/MinGW/lib" -lole32 -loleaut32 -luuid
I replaced my old version of MinGW...(2.0) => (3.1.0)....Thank you!!!!
That all seems rather excessive. Why not just use the API salem provided?
returns: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; }
Code:Serial number of drive C is D403B623
Open source isn't a matter of life or death......
.......its much more important than that!!
SuSE Linux - GCC 3.4.2
XP Pro - Visual Studio 2005 TS, MinGW 3.4.2
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.
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.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; }
EDIT: Remove the spaces in the word "root" in the sample code to compile.
Last edited by anonytmouse; 01-05-2005 at 08:35 AM.
My mistake. I should read questions more carefully.Originally Posted by anonytmouse
At least my mistake managed to get more information out of you ..... lol
Open source isn't a matter of life or death......
.......its much more important than that!!
SuSE Linux - GCC 3.4.2
XP Pro - Visual Studio 2005 TS, MinGW 3.4.2
Yep, it gave me a chance to post my volume serial code, which I just happened to have on hand because I originally mistook the volume serial number returned by Win32_LogicalDisk for the hard disk serial number.Originally Posted by eth0