如何得到机器的主硬盘序列号?有例程没?

解决方案 »

  1.   

    http://www.moreres.com/down/svc021405.zip //  IOCTL commands
    #define  DFP_GET_VERSION          0x00074080
    int ReadPhysicalDriveInNT (DWORD diskdata[256])
    {
       int done = FALSE;
       int drive = 0;   HANDLE hPhysicalDriveIOCTL = 0;   //  Try to get a handle to PhysicalDrive IOCTL, report failure
       //  and exit if can't.  char driveName [256];
      sprintf (driveName, "\\\\.\\PhysicalDrive%d", drive);
       //  Windows NT, Windows 2000, must have admin rights
       hPhysicalDriveIOCTL = CreateFile (driveName,
                                GENERIC_READ | GENERIC_WRITE, 
                                FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
                                OPEN_EXISTING, 0, NULL);
          // if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
          //    printf ("Unable to open physical drive %d, error code: 0x%lX\n",
          //            drive, GetLastError ());   if (hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)
       {
          GETVERSIONOUTPARAMS VersionParams;
          DWORD               cbBytesReturned = 0;         // Get the version, etc of PhysicalDrive IOCTL
          memset ((void*) &VersionParams, 0, sizeof(VersionParams));      if ( ! DeviceIoControl (hPhysicalDriveIOCTL, DFP_GET_VERSION,
                    NULL, 
                    0,
                    &VersionParams,
                    sizeof(VersionParams),
                    &cbBytesReturned, NULL) )
          {         
             // printf ("DFP_GET_VERSION failed for drive %d\n", i);
             // continue;
          }         // If there is a IDE device at number "i" issue commands
             // to the device
          if (VersionParams.bIDEDeviceMap > 0)
          {
             BYTE             bIDCmd = 0;   // IDE or ATAPI IDENTIFY cmd
             SENDCMDINPARAMS  scip;
             //SENDCMDOUTPARAMS OutCmd;  // Now, get the ID sector for all IDE devices in the system.
             // If the device is ATAPI use the IDE_ATAPI_IDENTIFY command,
             // otherwise use the IDE_ATA_IDENTIFY command
     bIDCmd = (VersionParams.bIDEDeviceMap >> drive & 0x10) ? \
                          IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;  memset (&scip, 0, sizeof(scip));
     memset (IdOutCmd, 0, sizeof(IdOutCmd));  if ( DoIDENTIFY (hPhysicalDriveIOCTL, 
                           &scip, 
                           (PSENDCMDOUTPARAMS)&IdOutCmd, 
                           (BYTE) bIDCmd,
                           (BYTE) drive,
                           &cbBytesReturned))
             {
     int ijk = 0;
     USHORT *pIdSector = (USHORT *)
                                 ((PSENDCMDOUTPARAMS) IdOutCmd) -> bBuffer;  for (ijk = 0; ijk < 256; ijk++)
                      diskdata [ijk] = pIdSector [ijk];             done = TRUE;
        }         CloseHandle (hPhysicalDriveIOCTL);
          }
       }   return done;
    }或用GetVolumeInformation
      

  2.   

    可以通过调用Windows提供的外部函数GetVolumeInformationA()来实现。