Thread: HD Serial number

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    7

    HD Serial number

    How can I get the Hard disk serial number??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Start by saying which OS and compiler you have

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    Windows with GCC

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    I'm using MinGW and WindowsXP

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    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++.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> 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.
    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.
    Last edited by anonytmouse; 01-04-2005 at 02:45 PM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    Can you help me to compile in MinGW???

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    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

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    I replaced my old version of MinGW...(2.0) => (3.1.0)....Thank you!!!!

  12. #12
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    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
    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

  13. #13
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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.
    Last edited by anonytmouse; 01-05-2005 at 08:35 AM.

  14. #14
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Quote Originally Posted by anonytmouse
    However, the volume serial number is not the hard disk serial number and can change
    My mistake. I should read questions more carefully.

    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

  15. #15
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by eth0
    My mistake. I should read questions more carefully.

    At least my mistake managed to get more information out of you ..... lol
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detect SCSI Hard Drive Serial Number
    By mercury529 in forum Windows Programming
    Replies: 3
    Last Post: 10-17-2006, 06:23 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. hard disk serial number
    By vijayaroli in forum Windows Programming
    Replies: 1
    Last Post: 08-31-2006, 03:00 AM
  4. Help! (serial number, image files)
    By param in forum C Programming
    Replies: 1
    Last Post: 01-18-2003, 03:54 PM
  5. HELP - Reading CPU Serial Number
    By Rigoberto Croes in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2002, 06:15 AM