下面是DELPHI中获取电脑硬盘物理序列号的代码(网友写给)
functionGetVolumeID:string;varvVolumeNameBuffer:array[0..255]ofChar;vVolumeSerialNumb...
function GetVolumeID : string;
var
vVolumeNameBuffer: array[0..255]of Char;
vVolumeSerialNumber: DWORD;
vMaximumComponentLength: DWORD;
vFileSystemFlags: DWORD;
vFileSystemNameBuffer: array[0..255]of Char;
begin
if GetVolumeInformation('C:\', vVolumeNameBuffer, SizeOf(vVolumeNameBuffer),
@vVolumeSerialNumber, vMaximumComponentLength, vFileSystemFlags,
vFileSystemNameBuffer, SizeOf(vFileSystemNameBuffer)) then
begin
Result := IntToHex(vVolumeSerialNumber, 8);
end;
end;
但有时运行不正常,不能获取。请问在这种情况下如何处理
如何书写处理代码(如弹出消息框,告之不能获取序列号,程序不能运行,退出程序)
请指导,谢谢 展开
var
vVolumeNameBuffer: array[0..255]of Char;
vVolumeSerialNumber: DWORD;
vMaximumComponentLength: DWORD;
vFileSystemFlags: DWORD;
vFileSystemNameBuffer: array[0..255]of Char;
begin
if GetVolumeInformation('C:\', vVolumeNameBuffer, SizeOf(vVolumeNameBuffer),
@vVolumeSerialNumber, vMaximumComponentLength, vFileSystemFlags,
vFileSystemNameBuffer, SizeOf(vFileSystemNameBuffer)) then
begin
Result := IntToHex(vVolumeSerialNumber, 8);
end;
end;
但有时运行不正常,不能获取。请问在这种情况下如何处理
如何书写处理代码(如弹出消息框,告之不能获取序列号,程序不能运行,退出程序)
请指导,谢谢 展开
3个回答
展开全部
1、网上的半瓶子醋很多(包括我),当然,百度更多。GetVolumeInformation是获取分区序列号,不是物理序列号,重装系统、格式化硬盘就会变,甚至用软件都能修改。还有一种WMI方法,不过不保险(有的硬盘ID前面有空字符)。比较保险的是DeviceIoControl函数,代码较长。
2、如果楼主喜欢那个GetVolumeInformation,那可以继续用下去,当我没回帖。如果想获取真正的物理序列号,可以试试下面代码。
uses Windows, SysUtils;
type
TIDERegs = packed record
bFeaturesReg: Byte;
bSectorCountReg: Byte;
bSectorNumberReg: Byte;
bCylLowReg: Byte;
bCylHighReg: Byte;
bDriveHeadReg: Byte;
bCommandReg: Byte;
bReserved: Byte;
end;
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;
PIdSector = ^TIdSector;
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: DWORD;
wMultSectorStuff: Word;
ulTotalAddressableSectors: DWORD;
wSingleWordDMA: Word;
wMultiWordDMA: Word;
bReserved: array[0..127] of Byte;
end;
TDriverStatus = packed record
bDriverError: Byte;
bIDEStatus: Byte;
bReserved: array[0..1] of Byte;
dwReserved: array[0..1] of DWORD;
end;
TSendCmdOutParams = packed record
cBufferSize: DWORD;
DriverStatus: TDriverStatus;
bBuffer: array[0..0] of Byte;
end;
procedure ChangeByteOrder(var Data; Size: Integer);
var
p: PChar;
i: Integer;
c: Char;
begin
p := @Data;
for i := 0 to (Size shr 1) - 1 do
begin
c := p^;
p^ := (p + 1)^;
(p + 1)^ := c;
Inc(p, 2);
end;
end;
function DiskSerialNo: string;
const
IDENTIFY_BUFFER_SIZE = 512;
var
hDevice: THandle;
cbBytesReturned: DWORD;
SCIP: TSendCmdInParams;
aIdOutCmd: array[0..(SizeOf(TSendCmdOutParams) + IDENTIFY_BUFFER_SIZE - 1) - 1] of Byte;
IdOutCmd: TSendCmdOutParams absolute aIdOutCmd;
begin
if Win32Platform = VER_PLATFORM_WIN32_NT then
hDevice := CreateFile('\\.\PhysicalDrive0', GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0)
else hDevice := CreateFile('\\.\SMARTVSD', 0, 0, nil, CREATE_NEW, 0, 0);
if hDevice = INVALID_HANDLE_VALUE then Exit;
FillChar(SCIP, SizeOf(TSendCmdInParams) - 1, #0);
FillChar(aIdOutCmd, SizeOf(aIdOutCmd), #0);
cbBytesReturned := 0;
SCIP.cBufferSize := IDENTIFY_BUFFER_SIZE;
SCIP.irDriveRegs.bSectorCountReg := 1;
SCIP.irDriveRegs.bSectorNumberReg := 1;
SCIP.irDriveRegs.bDriveHeadReg := $A0;
SCIP.irDriveRegs.bCommandReg := $EC;
if DeviceIoControl(hDevice, $0007C088, @SCIP, SizeOf(TSendCmdInParams) - 1,
@aIdOutCmd, SizeOf(aIdOutCmd), cbBytesReturned, nil) then
begin
with PIdSector(@IdOutCmd.bBuffer)^ do
begin
ChangeByteOrder(sSerialNumber, SizeOf(sSerialNumber));
(Pchar(@sSerialNumber) + SizeOf(sSerialNumber))^ := #0;
Result := Pchar(@sSerialNumber);
end;
end;
CloseHandle(hDevice);
end;
2、如果楼主喜欢那个GetVolumeInformation,那可以继续用下去,当我没回帖。如果想获取真正的物理序列号,可以试试下面代码。
uses Windows, SysUtils;
type
TIDERegs = packed record
bFeaturesReg: Byte;
bSectorCountReg: Byte;
bSectorNumberReg: Byte;
bCylLowReg: Byte;
bCylHighReg: Byte;
bDriveHeadReg: Byte;
bCommandReg: Byte;
bReserved: Byte;
end;
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;
PIdSector = ^TIdSector;
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: DWORD;
wMultSectorStuff: Word;
ulTotalAddressableSectors: DWORD;
wSingleWordDMA: Word;
wMultiWordDMA: Word;
bReserved: array[0..127] of Byte;
end;
TDriverStatus = packed record
bDriverError: Byte;
bIDEStatus: Byte;
bReserved: array[0..1] of Byte;
dwReserved: array[0..1] of DWORD;
end;
TSendCmdOutParams = packed record
cBufferSize: DWORD;
DriverStatus: TDriverStatus;
bBuffer: array[0..0] of Byte;
end;
procedure ChangeByteOrder(var Data; Size: Integer);
var
p: PChar;
i: Integer;
c: Char;
begin
p := @Data;
for i := 0 to (Size shr 1) - 1 do
begin
c := p^;
p^ := (p + 1)^;
(p + 1)^ := c;
Inc(p, 2);
end;
end;
function DiskSerialNo: string;
const
IDENTIFY_BUFFER_SIZE = 512;
var
hDevice: THandle;
cbBytesReturned: DWORD;
SCIP: TSendCmdInParams;
aIdOutCmd: array[0..(SizeOf(TSendCmdOutParams) + IDENTIFY_BUFFER_SIZE - 1) - 1] of Byte;
IdOutCmd: TSendCmdOutParams absolute aIdOutCmd;
begin
if Win32Platform = VER_PLATFORM_WIN32_NT then
hDevice := CreateFile('\\.\PhysicalDrive0', GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0)
else hDevice := CreateFile('\\.\SMARTVSD', 0, 0, nil, CREATE_NEW, 0, 0);
if hDevice = INVALID_HANDLE_VALUE then Exit;
FillChar(SCIP, SizeOf(TSendCmdInParams) - 1, #0);
FillChar(aIdOutCmd, SizeOf(aIdOutCmd), #0);
cbBytesReturned := 0;
SCIP.cBufferSize := IDENTIFY_BUFFER_SIZE;
SCIP.irDriveRegs.bSectorCountReg := 1;
SCIP.irDriveRegs.bSectorNumberReg := 1;
SCIP.irDriveRegs.bDriveHeadReg := $A0;
SCIP.irDriveRegs.bCommandReg := $EC;
if DeviceIoControl(hDevice, $0007C088, @SCIP, SizeOf(TSendCmdInParams) - 1,
@aIdOutCmd, SizeOf(aIdOutCmd), cbBytesReturned, nil) then
begin
with PIdSector(@IdOutCmd.bBuffer)^ do
begin
ChangeByteOrder(sSerialNumber, SizeOf(sSerialNumber));
(Pchar(@sSerialNumber) + SizeOf(sSerialNumber))^ := #0;
Result := Pchar(@sSerialNumber);
end;
end;
CloseHandle(hDevice);
end;
更多追问追答
追问
上面的代码都放在何处,如何调用,如让窗体中的控件显示硬盘的序列号
请指导如何使用上面的代码
追答
你说代码放在何处,放在implementation后面。你原来的GetVolumeID怎么用,DiskSerialNo就怎么用。
怎么显示?
procedure TForm1.Button1Click(Sender: TObject);begin
Edit1.Text := DiskSerialNo;
end;
你不会弱到这种程度吧?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用pc3000修改
下载地址:http://www.onlinedown.net/soft/29459.htm
下载地址:http://www.onlinedown.net/soft/29459.htm
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这是啥- -....
来自:求助得到的回答
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询