bcb中如何得到硬盘的序号?(200分)

G

gjxjq

Unregistered / Unconfirmed
GUEST, unregistred user!
我这里有delphi的方法:
function GetIdeDiskSerialNumber: string;
type
TSrbIoControl = packed record
HeaderLength: ULONG;
Signature: array[0..7] of Char;
Timeout: ULONG;
ControlCode: ULONG;
ReturnCode: ULONG;
Length: ULONG;
end;
SRB_IO_CONTROL = TSrbIoControl;
PSrbIoControl = ^TSrbIoControl;
TIDERegs = packed record
bFeaturesReg: Byte;
// Used for specifying SMART "commands".
bSectorCountReg: Byte;
// IDE sector count register
bSectorNumberReg: Byte;
// IDE sector number register
bCylLowReg: Byte;
// IDE low order cylinder value
bCylHighReg: Byte;
// IDE high order cylinder value
bDriveHeadReg: Byte;
// IDE drive/head register
bCommandReg: Byte;
// Actual IDE command.
bReserved: Byte;
// reserved. Must be zero.
end;
IDEREGS = TIDERegs;
PIDERegs = ^TIDERegs;
TSendCmdInParams = packed record
cBufferSize: DWORD;
irDriveRegs: TIDERegs;
bDriveNumber: Byte;
bReserved: array[0..2] of Byte;
dwReserved: array[0..3] of DWORD;
bBuffer: array[0..0] of Byte;
end;
SENDCMDINPARAMS = TSendCmdInParams;
PSendCmdInParams = ^TSendCmdInParams;
TIdSector = packed record
wGenConfig: Word;
wNumCyls: Word;
wReserved: Word;
wNumHeads: Word;
wBytesPerTrack: Word;
wBytesPerSector: Word;
wSectorsPerTrack: Word;
wVendorUnique: array[0..2] of Word;
sSerialNumber: array[0..19] of Char;
wBufferType: Word;
wBufferSize: Word;
wECCSize: Word;
sFirmwareRev: array[0..7] of Char;
sModelNumber: array[0..39] of Char;
wMoreVendorUnique: Word;
wDoubleWordIO: Word;
wCapabilities: Word;
wReserved1: Word;
wPIOTiming: Word;
wDMATiming: Word;
wBS: Word;
wNumCurrentCyls: Word;
wNumCurrentHeads: Word;
wNumCurrentSectorsPerTrack: Word;
ulCurrentSectorCapacity: ULONG;
wMultSectorStuff: Word;
ulTotalAddressableSectors: ULONG;
wSingleWordDMA: Word;
wMultiWordDMA: Word;
bReserved: array[0..127] of Byte;
end;
PIdSector = ^TIdSector;
const
IDE_ID_FUNCTION = $EC;
IDENTIFY_BUFFER_SIZE = 512;
DFP_RECEIVE_DRIVE_DATA = $0007C088;
IOCTL_SCSI_MINIPORT = $0004D008;
IOCTL_SCSI_MINIPORT_IDENTIFY = $001B0501;
DataSize = sizeof(TSendCmdInParams) - 1 + IDENTIFY_BUFFER_SIZE;
BufferSize = SizeOf(SRB_IO_CONTROL) + DataSize;
W9xBufferSize = IDENTIFY_BUFFER_SIZE + 16;
var
hDevice: THandle;
cbBytesReturned: DWORD;
pInData: PSendCmdInParams;
pOutData: Pointer;
// PSendCmdOutParams
Buffer: array[0..BufferSize - 1] of Byte;
srbControl: TSrbIoControl absolute Buffer;
procedure ChangeByteOrder(var Data;
Size: Integer);
var
ptr: PChar;
i: Integer;
c: Char;
begin
ptr := @Data;
for i := 0 to (Size shr 1) - 1do
begin
c := ptr^;
ptr^ := (ptr + 1)^;
(ptr + 1)^ := c;
Inc(ptr, 2);
end;
end;

begin
Result := '';
FillChar(Buffer, BufferSize, #0);
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
// Windows NT, Windows 2000
// Get SCSI port handle
hDevice := CreateFile('//./Scsi0:',
GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE,
nil, OPEN_EXISTING, 0, 0);
if hDevice = INVALID_HANDLE_VALUE then
Exit;
try
srbControl.HeaderLength := SizeOf(SRB_IO_CONTROL);
System.Move('SCSIDISK', srbControl.Signature, 8);
srbControl.Timeout := 2;
srbControl.Length := DataSize;
srbControl.ControlCode := IOCTL_SCSI_MINIPORT_IDENTIFY;
pInData := PSendCmdInParams(PChar(@Buffer)
+ SizeOf(SRB_IO_CONTROL));
pOutData := pInData;
with pInData^do
begin
cBufferSize := IDENTIFY_BUFFER_SIZE;
bDriveNumber := 0;
with irDriveRegsdo
begin
bFeaturesReg := 0;
bSectorCountReg := 1;
bSectorNumberReg := 1;
bCylLowReg := 0;
bCylHighReg := 0;
bDriveHeadReg := $A0;
bCommandReg := IDE_ID_FUNCTION;
end;
end;
if not DeviceIoControl(hDevice, IOCTL_SCSI_MINIPORT,
@Buffer, BufferSize, @Buffer, BufferSize,
cbBytesReturned, nil) then
Exit;
finally
CloseHandle(hDevice);
end;
end else
begin
// Windows 95 OSR2, Windows 98
hDevice := CreateFile('//./SMARTVSD', 0, 0, nil,
CREATE_NEW, 0, 0);
if hDevice = INVALID_HANDLE_VALUE then
Exit;
try
pInData := PSendCmdInParams(@Buffer);
pOutData := @pInData^.bBuffer;
with pInData^do
begin
cBufferSize := IDENTIFY_BUFFER_SIZE;
bDriveNumber := 0;
with irDriveRegsdo
begin
bFeaturesReg := 0;
bSectorCountReg := 1;
bSectorNumberReg := 1;
bCylLowReg := 0;
bCylHighReg := 0;
bDriveHeadReg := $A0;
bCommandReg := IDE_ID_FUNCTION;
end;
end;
if not DeviceIoControl(hDevice, DFP_RECEIVE_DRIVE_DATA,
pInData, SizeOf(TSendCmdInParams) - 1, pOutData,
W9xBufferSize, cbBytesReturned, nil) then
Exit;
finally
CloseHandle(hDevice);
end;
end;
with PIdSector(PChar(pOutData) + 16)^do
begin
ChangeByteOrder(sSerialNumber, SizeOf(sSerialNumber));
SetString(Result, sSerialNumber, SizeOf(sSerialNumber));
end;
end;
 
bcb中如何得到硬盘的序列号,在不同的操作系统有差别。
 
通过端口获取IDE硬盘ID:
#include
#include
#include
#include
#include
char *GetAscii(unsigned int inData[], int offStart, int offEnd);
int main(void)
{
unsigned int diskData[256];
/* Disk data */
unsigned int offset;
/* Disk data offset */
int loop;
int numDrv;
/* Number of IDE hard drives */
union REGS registers;
unsigned int biosCyl[2];
/* Cylinders, Heads, Sectors */
unsigned int biosHead[2];
unsigned int biosSec [2];
numDrv = peekb(0x40, 0x75);
/* BIOS Data area, Number of Hard disks */
for (loop = 0;
loop < numDrv;
loop++)
{
while (inp(0x01f7) != 0x50);
/* Wait for controller not busy */
outp(0x01f6, (loop == 0 ? 0xa0 : 0xb0));
/* Get first/second drive */
outp(0x01f7, 0xec);
/* Get drive info data */
while (inp(0x1f7) != 0x58);
/* Wait for data ready */
for (offset = 0;
offset != 256;
offset++) /* Read "sector" */
diskData[offset] = inpw(0x1f0);
/* Get BIOS drive info */
registers.h.ah = 0x08;
/* Get drive info */
registers.h.dl = 0x80 + loop;
/* Drive is 80H for Disk 0, 81H for Disk 1 */
int86(0x13, ?isters, ?isters);
if (!registers.x.cflag) /* All OK if carry not set */
{
biosHead[loop] = registers.h.dh + 1;
/* Heads are from 0 */
biosSec[loop] = registers.h.cl &amp;
0x3f;
/* sec is bits 5 - 0 */
/* +1 because starts from 0 and +1 for FDISK leaving one out */
biosCyl[loop] = ((registers.h.cl &amp;
0xc0) << 2) + registers.h.ch + 2;
} /* end of if */
printf("DRIVE %d:/n", loop);
printf("Model Number______________________: %s/n", GetAscii(diskData, 27, 46));
printf("Serial Number_____________________: %s/n", GetAscii(diskData, 10, 19));
printf("Controller Revision Number________: %s/n/n", GetAscii(diskData, 23, 26));
printf("Able todo
do
uble Word Transfer___: %6s/n", (diskData[48] == 0 ? "No" : "Yes"));
printf("Controller type___________________: %04X/n", diskData[20]);
printf("Controller buffer size (bytes)____: %6u/n", diskData[21] * 512);
printf("Number of ECC bytes transferred___: %6u/n", diskData[22]);
printf("Number of sectors per interrupt___: %6u/n/n", diskData[47]);
printf("Hard Disk Reports/n");
printf("Number of Cylinders (Fixed)_______: %6u/n", diskData[1]);
printf("Number of Heads___________________: %6u/n", diskData[3]);
printf("Number of Sectors per Track_______: %6u/n/n", diskData[6]);
printf("BIOS Reports/n");
printf("Number of Cylinders_______________: %6u/n", biosCyl[loop]);
printf("Number of Heads___________________: %6u/n", biosHead[loop]);
printf("Number of Sectors per Track_______: %6u/n/n", biosSec[loop]);
printf("Press any key to continue.../n/n");
getch();
} /* end of for */
return 0;
} /* main() */
char *GetAscii(unsigned int inData[], int offStart, int offEnd)
{
static char retVal[255];
int loop, loop1;
for (loop = offStart, loop1 = 0;
loop <= offend;
loop++)
{
retVal[loop1++] = (char )(inData[loop] / 256);
/* Get High byte */
retVal[loop1++] = (char )(inData[loop] % 256);
/* Get Low byte */
} /* end of for */
retVal[loop1] = '/0';
/* Make sure it ends in a NULL character */
return retVal;
} /* GetAscii() */
 
提这个问题
就说明很不了解情况
建议先看看其他语言的实现
语言是不重要的
 
用windows API函数
 
API 不行!
必须用汇编!
他要的是硬盘的出厂序列号!
这个一般Delphi的源码多,用Delphi做个控件得了。
 
顶部