⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 如何获得硬盘序列号(1).htm

📁 如何获得硬盘序列号(1).
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0051)http://www3.ccw.com.cn/club/essence/200110/6643.htm -->
<HTML><HEAD><TITLE>如何获得硬盘序列号(1)</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR></HEAD>
<BODY>
<P style="FONT-SIZE: 26px; FONT-WEIGHT: bold">如何获得硬盘序列号(1)</P>
<P style="FONT-SIZE: 18px; FONT-WEIGHT: bold; TEXT-INDENT: 20px">cyberghost</P>
<P style="FONT-SIZE: 12px; TEXT-ALIGN: right">C、C++ (2001-10-30 12:38:11) 
<HR style="COLOR: #000000; HEIGHT: 1px">

<P></P>
<DIV style="FONT-SIZE: 12px">#include &lt;stdio.h&gt; <BR>#include 
&lt;stdlib.h&gt; <BR>#include &lt;windows.h&gt; <BR><BR><BR>// Required to 
ensure correct PhysicalDrive IOCTL structure setup <BR>#pragma pack(1) 
<BR><BR>#define&nbsp;&nbsp;IDENTIFY_BUFFER_SIZE&nbsp;&nbsp;512 <BR><BR>// IOCTL 
commands 
<BR>#define&nbsp;&nbsp;DFP_GET_VERSION&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0x00074080 
<BR>#define&nbsp;&nbsp;DFP_SEND_DRIVE_COMMAND&nbsp;&nbsp;&nbsp;0x0007c084 
<BR>#define&nbsp;&nbsp;DFP_RECEIVE_DRIVE_DATA&nbsp;&nbsp;&nbsp;0x0007c088 
<BR><BR>#define&nbsp;&nbsp;FILE_DEVICE_SCSI&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0x0000001b 
<BR>#define&nbsp;&nbsp;IOCTL_SCSI_MINIPORT_IDENTIFY&nbsp;&nbsp;((FILE_DEVICE_SCSI 
&lt;&lt; 16) + 0x0501) <BR>#define&nbsp;&nbsp;IOCTL_SCSI_MINIPORT 0x0004D008 
//&nbsp;&nbsp;see NTDDSCSI.H for definition <BR><BR>// GETVERSIONOUTPARAMS 
contains the data returned from the <BR>// Get Driver Version function. 
<BR>typedef struct _GETVERSIONOUTPARAMS <BR>{ <BR>&nbsp;&nbsp;&nbsp;BYTE 
bVersion;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Binary driver version. 
<BR>&nbsp;&nbsp;&nbsp;BYTE bRevision;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Binary 
driver revision. <BR>&nbsp;&nbsp;&nbsp;BYTE 
bReserved;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Not used. <BR>&nbsp;&nbsp;&nbsp;BYTE 
bIDEDeviceMap; // Bit map of IDE devices. <BR>&nbsp;&nbsp;&nbsp;DWORD 
fCapabilities; // Bit mask of driver capabilities. <BR>&nbsp;&nbsp;&nbsp;DWORD 
dwReserved[4]; // For future use. <BR>}GETVERSIONOUTPARAMS, 
*PGETVERSIONOUTPARAMS, *LPGETVERSIONOUTPARAMS; <BR><BR>// Bits returned in the 
fCapabilities member of GETVERSIONOUTPARAMS 
<BR>#define&nbsp;&nbsp;CAP_IDE_ID_FUNCTION&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;// 
ATA ID command supported 
<BR>#define&nbsp;&nbsp;CAP_IDE_ATAPI_ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;// 
ATAPI ID command supported 
<BR>#define&nbsp;&nbsp;CAP_IDE_EXECUTE_SMART_FUNCTION&nbsp;&nbsp;4&nbsp;&nbsp;// 
SMART commannds supported <BR><BR>// IDE registers <BR>typedef struct _IDEREGS 
<BR>{ <BR>&nbsp;&nbsp;&nbsp;BYTE 
bFeaturesReg;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Used for specifying SMART 
"commands". <BR>&nbsp;&nbsp;&nbsp;BYTE 
bSectorCountReg;&nbsp;&nbsp;&nbsp;&nbsp;// IDE sector count register 
<BR>&nbsp;&nbsp;&nbsp;BYTE bSectorNumberReg;&nbsp;&nbsp;&nbsp;// IDE sector 
number register <BR>&nbsp;&nbsp;&nbsp;BYTE 
bCylLowReg;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// IDE low 
order cylinder value <BR>&nbsp;&nbsp;&nbsp;BYTE 
bCylHighReg;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// IDE high order 
cylinder value <BR>&nbsp;&nbsp;&nbsp;BYTE 
bDriveHeadReg;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// IDE drive/head register 
<BR>&nbsp;&nbsp;&nbsp;BYTE 
bCommandReg;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Actual IDE 
command. <BR>&nbsp;&nbsp;&nbsp;BYTE 
bReserved;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
reserved for future use. Must be zero. <BR>}IDEREGS, *PIDEREGS, *LPIDEREGS; 
<BR><BR>// SENDCMDINPARAMS contains the input parameters for the <BR>// Send 
Command to Drive function. <BR>typedef struct _SENDCMDINPARAMS <BR>{ 
<BR>&nbsp;&nbsp;DWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cBufferSize;&nbsp;&nbsp;&nbsp;// 
Buffer size in bytes 
<BR>&nbsp;&nbsp;IDEREGS&nbsp;&nbsp;&nbsp;irDriveRegs;&nbsp;&nbsp;&nbsp;// 
Structure with drive register values. <BR>&nbsp;&nbsp;&nbsp;BYTE 
bDriveNumber;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Physical drive number 
to send <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
command to (0,1,2,3). <BR>&nbsp;&nbsp;&nbsp;BYTE 
bReserved[3];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Reserved for future 
expansion. <BR>&nbsp;&nbsp;DWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwReserved[4]; // 
For future use. 
<BR>&nbsp;&nbsp;BYTE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bBuffer[1];&nbsp;&nbsp;&nbsp;&nbsp;// 
Input buffer. <BR>}SENDCMDINPARAMS, *PSENDCMDINPARAMS, *LPSENDCMDINPARAMS; 
<BR><BR>// Valid values for the bCommandReg member of IDEREGS. 
<BR>#define&nbsp;&nbsp;IDE_ATAPI_IDENTIFY&nbsp;&nbsp;0xA1&nbsp;&nbsp;// Returns 
ID sector for ATAPI. 
<BR>#define&nbsp;&nbsp;IDE_ATA_IDENTIFY&nbsp;&nbsp;&nbsp;&nbsp;0xEC&nbsp;&nbsp;// 
Returns ID sector for ATA. <BR><BR>// Status returned from driver <BR>typedef 
struct _DRIVERSTATUS <BR>{ 
<BR>&nbsp;&nbsp;&nbsp;BYTE&nbsp;&nbsp;bDriverError;&nbsp;&nbsp;// Error code 
from driver, or 0 if no error. 
<BR>&nbsp;&nbsp;&nbsp;BYTE&nbsp;&nbsp;bIDEStatus;&nbsp;&nbsp;&nbsp;&nbsp;// 
Contents of IDE Error register. 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Only valid 
when bDriverError is SMART_IDE_ERROR. 
<BR>&nbsp;&nbsp;&nbsp;BYTE&nbsp;&nbsp;bReserved[2];&nbsp;&nbsp;// Reserved for 
future expansion. <BR>&nbsp;&nbsp;&nbsp;DWORD&nbsp;&nbsp;dwReserved[2];// 
Reserved for future expansion. <BR>}DRIVERSTATUS, *PDRIVERSTATUS, 
*LPDRIVERSTATUS; <BR><BR>// Structure returned by PhysicalDrive IOCTL for 
several commands <BR>typedef struct _SENDCMDOUTPARAMS <BR>{ 
<BR>&nbsp;&nbsp;DWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cBufferSize;&nbsp;&nbsp;// 
Size of bBuffer in bytes 
<BR>&nbsp;&nbsp;&nbsp;DRIVERSTATUS&nbsp;&nbsp;DriverStatus;&nbsp;&nbsp;// Driver 
status structure. 
<BR>&nbsp;&nbsp;BYTE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bBuffer[1];&nbsp;&nbsp;&nbsp;&nbsp;// 
Buffer of arbitrary length <BR>// in which to store the data <BR>// read from 
the drive. <BR>}SENDCMDOUTPARAMS, *PSENDCMDOUTPARAMS, *LPSENDCMDOUTPARAMS; 
<BR><BR>// The following struct defines the interesting part of the IDENTIFY 
<BR>// buffer: <BR>typedef struct _IDSECTOR <BR>{ 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wGenConfig; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wNumCyls; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wReserved; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wNumHeads; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wBytesPerTrack; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wBytesPerSector; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wSectorsPerTrack; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wVendorUnique[3]; 
<BR>&nbsp;&nbsp;CHAR&nbsp;&nbsp;&nbsp;&nbsp;sSerialNumber[20]; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wBufferType; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wBufferSize; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wECCSize; 
<BR>&nbsp;&nbsp;CHAR&nbsp;&nbsp;&nbsp;&nbsp;sFirmwareRev[8]; 
<BR>&nbsp;&nbsp;CHAR&nbsp;&nbsp;&nbsp;&nbsp;sModelNumber[40]; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wMoreVendorUnique; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wDoubleWordIO; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wCapabilities; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wReserved1; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wPIOTiming; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wDMATiming; <BR>&nbsp;&nbsp;USHORT wBS; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wNumCurrentCyls; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wNumCurrentHeads; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wNumCurrentSectorsPerTrack; 
<BR>&nbsp;&nbsp;ULONG&nbsp;&nbsp;&nbsp;ulCurrentSectorCapacity; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wMultSectorStuff; 
<BR>&nbsp;&nbsp;ULONG&nbsp;&nbsp;&nbsp;ulTotalAddressableSectors; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wSingleWordDMA; 
<BR>&nbsp;&nbsp;USHORT&nbsp;&nbsp;wMultiWordDMA; 
<BR>&nbsp;&nbsp;BYTE&nbsp;&nbsp;&nbsp;&nbsp;bReserved[128]; <BR>}IDSECTOR, 
*PIDSECTOR; <BR><BR>typedef struct _SRB_IO_CONTROL <BR>{ 
<BR>&nbsp;&nbsp;&nbsp;ULONG HeaderLength; <BR>&nbsp;&nbsp;&nbsp;UCHAR 
Signature[8]; <BR>&nbsp;&nbsp;&nbsp;ULONG Timeout; <BR>&nbsp;&nbsp;&nbsp;ULONG 
ControlCode; <BR>&nbsp;&nbsp;&nbsp;ULONG ReturnCode; <BR>&nbsp;&nbsp;&nbsp;ULONG 
Length; <BR>}SRB_IO_CONTROL, *PSRB_IO_CONTROL; <BR><BR>// Define global buffers. 
<BR>BYTE IdOutCmd [sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1]; 
<BR>char strHDSerialNumber[1024]; <BR>DWORD diskData[256]; <BR><BR>BOOL 
DoIDENTIFY( HANDLE hPhysicalDriveIOCTL <BR>&nbsp;&nbsp;&nbsp;, PSENDCMDINPARAMS 
pSCIP <BR>&nbsp;&nbsp;&nbsp;, PSENDCMDOUTPARAMS pSCOP <BR>&nbsp;&nbsp;, BYTE 
bIDCmd <BR>&nbsp;&nbsp;, BYTE bDriveNum <BR>&nbsp;&nbsp;&nbsp;, PDWORD 
lpcbBytesReturned <BR>&nbsp;&nbsp;&nbsp;) <BR>{ <BR>// Set up data structures 
for IDENTIFY command. <BR>pSCIP-&gt;cBufferSize = IDENTIFY_BUFFER_SIZE; 
<BR>pSCIP-&gt;irDriveRegs.bFeaturesReg = 0; 
<BR>pSCIP-&gt;irDriveRegs.bSectorCountReg = 1; 
<BR>pSCIP-&gt;irDriveRegs.bSectorNumberReg = 1; 
<BR>pSCIP-&gt;irDriveRegs.bCylLowReg = 0; <BR>pSCIP-&gt;irDriveRegs.bCylHighReg 
= 0; <BR><BR>// Compute the drive number. 
<BR>pSCIP-&gt;irDriveRegs.bDriveHeadReg = 0xA0 | ((bDriveNum &amp; 1) &lt;&lt; 
4); <BR><BR>// The command can either be IDE identify or ATAPI identify. 
<BR>pSCIP-&gt;irDriveRegs.bCommandReg = bIDCmd; <BR>pSCIP-&gt;bDriveNumber = 
bDriveNum; <BR>pSCIP-&gt;cBufferSize = IDENTIFY_BUFFER_SIZE; <BR><BR>return 
(DeviceIoControl( hPhysicalDriveIOCTL <BR>&nbsp;&nbsp;&nbsp;, 
DFP_RECEIVE_DRIVE_DATA <BR>&nbsp;&nbsp;&nbsp;, (LPVOID) pSCIP 
<BR>&nbsp;&nbsp;&nbsp;, sizeof(SENDCMDINPARAMS) - 1 <BR>&nbsp;&nbsp;&nbsp;, 
(LPVOID) pSCOP <BR>&nbsp;&nbsp;&nbsp;, sizeof(SENDCMDOUTPARAMS) + 
IDENTIFY_BUFFER_SIZE - 1 <BR>&nbsp;&nbsp;&nbsp;, lpcbBytesReturned, NULL) 
<BR>&nbsp;&nbsp;&nbsp;); <BR>} <BR><BR>BOOL ReadPhysicalDriveInNT(void) <BR>{ 
<BR>BOOL bRet = FALSE; <BR><BR>HANDLE hPhysicalDriveIOCTL = 0; <BR>int drive = 
0; <BR><BR>// Try to get a handle to PhysicalDrive IOCTL, <BR>// report failure 
and exit if can't. <BR>char driveName [256]; <BR><BR>sprintf(driveName, 
"\\\\.\\PhysicalDrive0"); <BR><BR>// Windows NT, Windows 2000, must have admin 
rights <BR>hPhysicalDriveIOCTL = CreateFile( driveName <BR>, GENERIC_READ | 
GENERIC_WRITE <BR>, FILE_SHARE_READ | FILE_SHARE_WRITE <BR>, NULL <BR>, 
OPEN_EXISTING <BR>, 0 <BR>, NULL <BR>); <BR><BR>// if (hPhysicalDriveIOCTL == 
INVALID_HANDLE_VALUE) <BR>//&nbsp;&nbsp;&nbsp;&nbsp;printf ("Unable to open 
physical drive %d, error code: 0x%lX\n", 
<BR>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drive, 
GetLastError ()); <BR><BR>if( hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE ) 
<BR>{ <BR>GETVERSIONOUTPARAMS VersionParams; 
<BR>DWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cbBytesReturned 
= 0; <BR><BR>// Get the version, etc of PhysicalDrive IOCTL 
<BR>memset((void*)&amp;VersionParams, 0, sizeof(VersionParams)); <BR><BR>if( 
!DeviceIoControl( hPhysicalDriveIOCTL <BR>, DFP_GET_VERSION <BR>, NULL <BR>, 0 
<BR>, &amp;VersionParams <BR>, sizeof(VersionParams) <BR>, &amp;cbBytesReturned 
<BR>, NULL <BR>) <BR>&nbsp;&nbsp;) <BR>{ <BR>} <BR><BR>// If there is a IDE 
device at number "i" issue commands <BR>// to the device <BR>if( 
VersionParams.bIDEDeviceMap &gt; 0 ) <BR>{ 
<BR>BYTE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bIDCmd = 0; 
// IDE or ATAPI IDENTIFY cmd <BR>SENDCMDINPARAMS&nbsp;&nbsp;scip; <BR><BR>// 
Now, get the ID sector for all IDE devices in the <BR>// system. If the device 
is ATAPI use the <BR>// IDE_ATAPI_IDENTIFY command, otherwise use the <BR>// 
IDE_ATA_IDENTIFY command <BR>bIDCmd = (VersionParams.bIDEDeviceMap &gt;&gt; 
drive &amp; 0x10) ? \ 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IDE_ATAPI_IDENTIFY 
: IDE_ATA_IDENTIFY; <BR><BR>memset (&amp;scip, 0, sizeof(scip)); <BR>memset 
(IdOutCmd, 0, sizeof(IdOutCmd)); <BR><BR>if( DoIDENTIFY( hPhysicalDriveIOCTL 
<BR>&nbsp;&nbsp;, &amp;scip <BR>&nbsp;&nbsp;, (PSENDCMDOUTPARAMS)&amp;IdOutCmd 
<BR>&nbsp;&nbsp;, (BYTE)bIDCmd <BR>&nbsp;&nbsp;, (BYTE)drive <BR>&nbsp;&nbsp;, 
&amp;cbBytesReturned <BR>) <BR>&nbsp;&nbsp;) <BR>{ <BR>memset(diskData, 0, 
1024); <BR>int i = 0; <BR><BR>USHORT *pIdSector = (USHORT *) 
<BR>((PSENDCMDOUTPARAMS)IdOutCmd)-&gt;bBuffer; <BR><BR>for( i = 0; i &lt; 256; 
i++ ) <BR>diskData[i] = pIdSector[i]; <BR><BR>bRet = TRUE; <BR>} <BR>} 
<BR><BR>CloseHandle(hPhysicalDriveIOCTL); <BR>} <BR><BR>return bRet; <BR>} 
<BR>因为程序太大,分两篇。 <BR><BR></DIV>
<P>
<HR style="COLOR: #000000; HEIGHT: 1px">

<DIV style="FONT-SIZE: 12px; TEXT-ALIGN: right">i社区原文:<A 
href="http://www3.ccw.com.cn/club/bbs/bbsView.asp?essenceID=6643" 
target=_blank>如何获得硬盘序列号(1)</A></DIV>
<P></P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -