windows c语言 sata 序列号,【Delphi】获取IDE/SATA硬盘序列号
網(wǎng)上有這種代碼了,只是因?yàn)閷?xiě)的較早,有些地方寫(xiě)的不那么言簡(jiǎn)意賅,而且在XE后的版本中有問(wèn)題(因?yàn)閁nicode字符的關(guān)系)
下面是精簡(jiǎn)修改過(guò)的代碼,只取第一塊硬盤(pán)的序列號(hào),Win7 + XE 測(cè)試通過(guò)。需要先安裝JwApi函數(shù)庫(kù),因?yàn)橛行┙Y(jié)構(gòu)體在庫(kù)中有聲明了
unit uGetHDSN;
interface
uses
Windows, JwaWinIoctl;
function GetIdeSerialNumber: AnsiString;
implementation
type
TIdSector = packed record
wGenConfig: USHORT;
wNumCyls: USHORT;
wReserved: USHORT;
wNumHeads: USHORT;
wBytesPerTrack: USHORT;
wBytesPerSector: USHORT;
wSectorsPerTrack: USHORT;
wVendorUnique: array [0 .. 2] of USHORT;
sSerialNumber: array [0 .. 19] of AnsiChar;
wBufferType: USHORT;
wBufferSize: USHORT;
wECCSize: USHORT;
sFirmwareRev: array [0 .. 7] of AnsiChar;
sModelNumber: array [0 .. 39] of AnsiChar;
wMoreVendorUnique: USHORT;
wDoubleWordIO: USHORT;
wCapabilities: USHORT;
wReserved1: USHORT;
wPIOTiming: USHORT;
wDMATiming: USHORT;
wBS: USHORT;
wNumCurrentCyls: USHORT;
wNumCurrentHeads: USHORT;
wNumCurrentSectorsPerTrack: USHORT;
ulCurrentSectorCapacity: ULONG;
wMultSectorStuff: USHORT;
ulTotalAddressableSectors: ULONG;
wSingleWordDMA: USHORT;
wMultiWordDMA: USHORT;
bReserved: array [0 .. 127] of Byte;
end;
PIdSector = ^TIdSector;
const
IDE_ATA_IDENTIFY = $EC;
function LittleToBig(Data: Word): Word;
asm
xchg ah, al
end;
function GetIdeSerialNumber: AnsiString;
var
hDevice: THandle;
Size, cbBytesReturned: DWORD;
SCIP: TSendCmdInParams;
SCOP: PSendCmdOutParams;
P: PWORD;
I: Integer;
begin
Result := '';
hDevice := CreateFile('\\.\PhysicalDrive0', GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or
FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
if hDevice = INVALID_HANDLE_VALUE then
Exit;
Size := SizeOf(TSendCmdOutParams) + IDENTIFY_BUFFER_SIZE - 1;
SCOP := AllocMem(Size);
SCIP.irDriveRegs.bCommandReg := IDE_ATA_IDENTIFY;
if DeviceIoControl(hDevice, SMART_RCV_DRIVE_DATA, @SCIP, SizeOf(TSendCmdInParams) - 1, SCOP, Size,
cbBytesReturned, nil) = False then
begin
FreeMem(SCOP);
CloseHandle(hDevice);
Exit;
end;
// 處理一下序列號(hào)在內(nèi)存中的順序
with PIdSector(@SCOP^.bBuffer[0])^ do
begin
SetLength(Result, Length(sSerialNumber));
P := @Result[1];
CopyMemory(P, @sSerialNumber[0], Length(sSerialNumber));
for I := 1 to Length(sSerialNumber) div 2 do
begin
P^ := LittleToBig(P^);
Inc(P);
end;
end;
FreeMem(SCOP);
end;
end.
總結(jié)
以上是生活随笔為你收集整理的windows c语言 sata 序列号,【Delphi】获取IDE/SATA硬盘序列号的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 本机不装Oracle,远程访问服务器上O
- 下一篇: 常用Windows运行命令大全