📄 如何获得硬盘序列号(2).htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0051)http://www3.ccw.com.cn/club/essence/200110/6642.htm -->
<HTML><HEAD><TITLE>如何获得硬盘序列号(2)</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">如何获得硬盘序列号(2)</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:39:18)
<HR style="COLOR: #000000; HEIGHT: 1px">
<P></P>
<DIV
style="FONT-SIZE: 12px">//=============================================================
<BR>// DoIDENTIFY <BR>// FUNCTION: Send an IDENTIFY command to the drive <BR>//
bDriveNum = 0-3 <BR>// bIDCmd = IDE_ATA_IDENTIFY or IDE_ATAPI_IDENTIFY
<BR>//==============================================================
<BR>#define SENDIDLENGTH sizeof (SENDCMDOUTPARAMS) +
IDENTIFY_BUFFER_SIZE <BR><BR>BOOL ReadIdeDriveAsScsiDriveInNT(void) <BR>{
<BR>BOOL bRet = FALSE; <BR>int controller = 0; <BR><BR>HANDLE hScsiDriveIOCTL =
0; <BR>char driveName [256]; <BR><BR>// Try to get a handle to
PhysicalDrive IOCTL, report failure <BR>// and exit if can't. <BR>sprintf
(driveName, "\\\\.\\Scsi%d:", controller); <BR><BR>// Windows NT, Windows 2000,
any rights should do <BR>hScsiDriveIOCTL = 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 (hScsiDriveIOCTL ==
INVALID_HANDLE_VALUE) <BR>// printf ("Unable to open SCSI controller %d, error
code: 0x%lX\n",
<BR>// controller,
GetLastError ()); <BR><BR>if (hScsiDriveIOCTL != INVALID_HANDLE_VALUE) <BR>{
<BR>int drive = 0; <BR><BR>char buffer[sizeof (SRB_IO_CONTROL) + SENDIDLENGTH];
<BR>SRB_IO_CONTROL *p = (SRB_IO_CONTROL *) buffer; <BR>SENDCMDINPARAMS *pin =
<BR> (SENDCMDINPARAMS
*) (buffer + sizeof (SRB_IO_CONTROL)); <BR>DWORD dummy;
<BR> <BR>memset(buffer, 0, sizeof (buffer));
<BR>p->HeaderLength = sizeof (SRB_IO_CONTROL); <BR>p->Timeout = 10000;
<BR>p->Length = SENDIDLENGTH; <BR>p->ControlCode =
IOCTL_SCSI_MINIPORT_IDENTIFY; <BR>strncpy((char *) p->Signature, "SCSIDISK",
8); <BR> <BR>pin->irDriveRegs.bCommandReg = IDE_ATA_IDENTIFY;
<BR>pin->bDriveNumber = drive; <BR><BR>if( DeviceIoControl( hScsiDriveIOCTL
<BR> , IOCTL_SCSI_MINIPORT <BR> , buffer
<BR> , sizeof(SRB_IO_CONTROL) +
<BR> sizeof
(SENDCMDINPARAMS) - 1 <BR> , buffer <BR> ,
sizeof (SRB_IO_CONTROL) + <BR>SENDIDLENGTH <BR> , &dummy
<BR> , NULL <BR> ) <BR> ) <BR>{
<BR>SENDCMDOUTPARAMS *pOut =
<BR> (SENDCMDOUTPARAMS
*) (buffer + sizeof (SRB_IO_CONTROL)); <BR>IDSECTOR *pId = (IDSECTOR
*)(pOut->bBuffer); <BR>if( pId->sModelNumber [0] ) <BR>{
<BR>memset(diskData, 0, 1024); <BR>int i = 0; <BR>USHORT *pIdSector = (USHORT
*)pId; <BR><BR>for( i = 0; i < 256; i++ ) <BR>diskData[i] = pIdSector[i];
<BR><BR>bRet = TRUE; <BR>} <BR>} <BR>CloseHandle (hScsiDriveIOCTL); <BR>}
<BR><BR>return bRet; <BR>} <BR><BR>char *ConvertToString( DWORD diskData[256]
<BR>, int firstIndex <BR>, int lastIndex <BR>) <BR>{ <BR>static char string
[1024]; <BR>int index = 0; <BR>int position = 0; <BR><BR>// each integer has two
characters stored in it backwards <BR>for( index = firstIndex; index <=
lastIndex; index++ ) <BR>{ <BR>// get high byte for 1st character
<BR>string[position] = (char)(diskData[index] / 256); <BR>position++; <BR><BR>//
get low byte for 2nd character <BR>string[position] = (char)(diskData[index] %
256); <BR>position++; <BR>} <BR><BR>// end the string <BR>string[position] =
'\0'; <BR><BR>// cut off the trailing blanks <BR>for(index = position-1; index
>= 0; index--){ <BR>if(string[index]!=' ') break;
<BR>else string[index]=0; <BR>} <BR>for(index = 0; index <
position; index++) <BR>if(string[index]!=' ') break; <BR>if(index!=0){
<BR>strncpy(string,string+index,position-index); <BR>string[position-index]=0;
<BR>} <BR><BR>return string; <BR>} <BR><BR>BOOL GetHDSerialCode(char*
lpSerialID) <BR>{ <BR>if( lpSerialID == NULL ) <BR>return FALSE;
<BR><BR>memset(strHDSerialNumber, 0, 1024); <BR><BR>BOOL bRet; <BR><BR>bRet =
ReadPhysicalDriveInNT(); <BR><BR>if( !bRet ) <BR>bRet =
ReadIdeDriveAsScsiDriveInNT(); <BR><BR>if( bRet ) <BR>{ <BR>strcpy(
strHDSerialNumber <BR> , ConvertToString(diskData, 10, 19)
<BR> ); <BR><BR>try <BR>{ <BR>strcpy(lpSerialID, strHDSerialNumber);
<BR>} <BR>catch(...) <BR>{ <BR>return FALSE; <BR>} <BR>} <BR><BR>return bRet;
<BR>} <BR><BR>void main(void) <BR>{ <BR>char strSerialNumber[16];
<BR>memset(strSerialNumber, 0, 16); <BR><BR>BOOL bRet =
GetHDSerialCode(strSerialNumber); <BR><BR>if( bRet )
<BR>printf("SerialNumber=%s\n",strSerialNumber); <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=6642"
target=_blank>如何获得硬盘序列号(2)</A></DIV>
<P></P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -