DELPHI7.0 获取硬盘、CPU、网卡序列号的代码

代码如下:

//引用及TYPE变量申明

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,nb30; {重要引用}

type
PASTAT = ^TASTAT;
TASTAT = record
adapter : TAdapterStatus;
name_buf : TNameBuffer;
end;

TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Edit2: TEdit;
Edit3: TEdit;
Button2: TButton;
Edit4: TEdit;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
type
TCPUID = array[1..4] of Longint;

//取硬盘系列号:
function GetIdeSerialNumber: pchar; //获取硬盘的出厂系列号;
const IDENTIFY_BUFFER_SIZE = 512;
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;

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;

PIdSector = ^TIdSector;
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;
var
hDevice: Thandle;
cbBytesReturned: DWORD;
SCIP: TSendCmdInParams;
aIdOutCmd: array[0..(SizeOf(TSendCmdOutParams) + IDENTIFY_BUFFER_SIZE-1)-1] of Byte;
IdOutCmd: TSendCmdOutParams absolute aIdOutCmd;

procedure ChangeByteOrder(var Data; Size: Integer);//函数中的过程
var
ptr: Pchar;
i: Integer;
c: Char;
begin
ptr := @Data;
for I := 0 to (Size shr 1) - 1 do begin
c := ptr^;
ptr^ := (ptr + 1)^;
(ptr + 1)^ := c;
Inc(ptr, 2);
end;
end;

begin //函数主体
Result := '';
if SysUtils.Win32Platform = VER_PLATFORM_WIN32_NT then
begin // Windows NT, Windows 2000
hDevice := CreateFile('\\.\PhysicalDrive0', GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
end
else // Version Windows 95 OSR2, Windows 98
hDevice := CreateFile('\\.\SMARTVSD', 0, 0, nil, Create_NEW, 0, 0);
if hDevice = INVALID_HANDLE_VALUE then Exit;
try
FillChar(SCIP, SizeOf(TSendCmdInParams) - 1, #0);
FillChar(aIdOutCmd, SizeOf(aIdOutCmd), #0);
cbBytesReturned := 0;
with SCIP do
begin
cBufferSize := IDENTIFY_BUFFER_SIZE;
with irDriveRegs do
begin
bSectorCountReg := 1;
bSectorNumberReg := 1;
bDriveHeadReg := $A0;
bCommandReg := $EC;
end;
end;
if not DeviceIoControl(hDevice, $0007C088, @SCIP, SizeOf(TSendCmdInParams) - 1,@aIdOutCmd, SizeOf(aIdOutCmd), cbBytesReturned, nil) then Exit;
finally
CloseHandle(hDevice);
end;
with PIdSector(@IdOutCmd.bBuffer)^ do
begin
ChangeByteOrder(sSerialNumber, SizeOf(sSerialNumber));
(Pchar(@sSerialNumber) + SizeOf(sSerialNumber))^:= #0;
Result := Pchar(@sSerialNumber);
end;
end;
//=================================================================

//CPU系列号:
FUNCTION GetCPUID : TCPUID; assembler; register;
asm
PUSH EBX {Save affected register}
PUSH EDI
MOV EDI,EAX {@Resukt}
MOV EAX,1
DW $A20F {CPUID Command}
STOSD {CPUID[1]}
MOV EAX,EBX
STOSD {CPUID[2]}
MOV EAX,ECX
STOSD {CPUID[3]}
MOV EAX,EDX
STOSD {CPUID[4]}
POP EDI {Restore registers}
POP EBX
END;

function GetCPUIDStr:String;
var
CPUID:TCPUID;
begin
CPUID:=GetCPUID;
Result:=IntToHex(CPUID[1],8)+IntToHex(CPUID[2],8)+IntToHex(CPUID[3],8)+IntToHex(CPUID[4],8);
end;

///==================================================================================

///取MAC(非集成网卡):

function NBGetAdapterAddress(a: Integer): string;
var
NCB: TNCB; // Netbios control block //NetBios控制块
ADAPTER: TADAPTERSTATUS; // Netbios adapter status//取网卡状态
LANAENUM: TLANAENUM; // Netbios lana
intIdx: Integer; // Temporary work value//临时变量
cRC: Char; // Netbios return code//NetBios返回值
strTemp: string; // Temporary string//临时变量
begin
// Initialize
Result := '';
try
// Zero control blocl
ZeroMemory(@NCB, SizeOf(NCB));
// Issue enum command
NCB.ncb_command := Chr(NCBENUM);
cRC := NetBios(@NCB);
// Reissue enum command
NCB.ncb_buffer := @LANAENUM;
NCB.ncb_length := SizeOf(LANAENUM);
cRC := NetBios(@NCB);
if ord(cRC) <> 0 then
exit;
// Reset adapter
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBRESET);
NCB.ncb_lana_num := LANAENUM.lana[a];
cRC := NetBios(@NCB);
if ord(cRC) <> 0 then
exit;
// Get adapter address
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBASTAT);
NCB.ncb_lana_num := LANAENUM.lana[a];
StrPCopy(NCB.ncb_callname, '*');
NCB.ncb_buffer := @ADAPTER;
NCB.ncb_length := SizeOf(ADAPTER);
cRC := NetBios(@NCB);
// Convert it to string
strTemp := '';
for intIdx := 0 to 5 do
strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]), 2);
Result := strTemp;
finally
end;
end;
//==========================================================================
//取MAC地址(集成网卡和非集成网卡):

function Getmac:string;
var
ncb : TNCB;
s:string;
adapt : TASTAT;
lanaEnum : TLanaEnum;
i, j, m : integer;
strPart, strMac : string;
begin
FillChar(ncb, SizeOf(TNCB), 0);
ncb.ncb_command := Char(NCBEnum);
ncb.ncb_buffer := PChar(@lanaEnum);
ncb.ncb_length := SizeOf(TLanaEnum);
s:=Netbios(@ncb);
for i := 0 to integer(lanaEnum.length)-1 do
begin
FillChar(ncb, SizeOf(TNCB), 0);
ncb.ncb_command := Char(NCBReset);
ncb.ncb_lana_num := lanaEnum.lana[i];
Netbios(@ncb);
Netbios(@ncb);
FillChar(ncb, SizeOf(TNCB), 0);
ncb.ncb_command := Chr(NCBAstat);
ncb.ncb_lana_num := lanaEnum.lana[i];
ncb.ncb_callname := '* ';
ncb.ncb_buffer := PChar(@adapt);
ncb.ncb_length := SizeOf(TASTAT);
m:=0;
if (Win32Platform = VER_PLATFORM_WIN32_NT) then
m:=1;
if m=1 then
begin
if Netbios(@ncb) = Chr(0) then
strMac := '';
for j := 0 to 5 do
begin
strPart := IntToHex(integer(adapt.adapter.adapter_address[j]), 2);
strMac := strMac + strPart + '-';
end;
SetLength(strMac, Length(strMac)-1);
end;
if m=0 then
if Netbios(@ncb) <> Chr(0) then
begin
strMac := '';
for j := 0 to 5 do
begin
strPart := IntToHex(integer(adapt.adapter.adapter_address[j]), 2);
strMac := strMac + strPart + '-';
end;
SetLength(strMac, Length(strMac)-1);
end;
end;
result:=strmac;
end;

function PartitionString(StrV,PrtSymbol: string): TStringList;
var
iTemp: integer;
begin
result := TStringList.Create;
iTemp := pos(PrtSymbol,StrV);
while iTemp>0 do begin
if iTemp>1 then result.Append(copy(StrV,1,iTemp-1));
delete(StrV,1,iTemp+length(PrtSymbol)-1);
iTemp := pos(PrtSymbol,StrV);
end;
if Strv<>'' then result.Append(StrV);
end;

function MacStr():String;
var
Str:TStrings;
i:Integer;
MacStr:String;
begin
MacStr:='';
Str:=TStringList.Create;
Str:=PartitionString(Getmac,'-');
for i:=0 to Str.Count-1 do
MacStr:=MacStr+Str[i];
Result:=MacStr;
end;

//==============================================

//调用示例
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit3.Text:=strpas(GetIdeSerialNumber);//取硬盘号
Edit2.text:=GetCPUIDStr;//CPU系列号
edit4.Text:=NBGetAdapterAddress(12);//非集成网卡
Edit1.text:=MacStr;//集成和非集成网卡

end;

(0)

相关推荐

  • delphi7连接mysql5的实现方法

    本文简单介绍了Delphi7连接MySQL数据库的实现方法,具体步骤如下: 首先先去下载:http://www.justsoftwaresolutions.co.uk/delphi/dbexpress_and_mysql_5.html 然后将下载到的dbxopenmysql5_dll.zip解压出来,再把dbxopenmysql50.dll和libmysql.dll都放到工程文件夹下. 在Form上放上TSQLConnection.TSQLQuery.TStringGrid.3个TButton.

  • Delphi中对时间操作方法汇总

    一般来说在delphi中用于描述时间的有几种数据结构,而对时间的操作,实质上就是对这些结构的操作. TDateTime类型: Delphi中最常用的表示日期时间的数据类型TDateTime类型,和普通的整形数一样,你可以给日期定义一个日期型变量以便在程序中进行操作.TdateTime类型实质上是一个Double型的数,在Delphi中是这样定义TdateTime类型: type TDateTime = type Double ,具体的算法是用Double数的整数部分表示日期,以1989年12月3

  • Delphi实现窗口文字淡入淡出渐变效果的方法

    本文所述实例为Dlephi实现的窗口渐变文字效果,文字可以不停的变化,颜色由浅入深,由清淅变模糊,文字渐变的时间可在代码中自己调整.主要实现代码如下: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Timer1: TTimer;

  • Delphi隐藏TPageControl的标签实例介绍

    下面是个简单的例子: ..... 复制代码 代码如下: begin //先屏蔽掉所有标签 for i := 0 to PageControl.PageCount - 1 do PageControl.Pages[i].TabVisible := False;//隐藏 //再开启首先要显示的tab页,Tab_Page1:TTabSheet; PageControl.ActivePage := Tab_Page1; //这就是要控制跳转的页面 end;

  • Delphi实现限定软件使用时间的方法

    我们经常看到很多网上下载的试用版软件,都有使用时间的限制,就其商业角度而言也是处于软件效益保护的一种措施,可以让用户免费试用一段时间,若满意就可以购买商业软件.本文所述实例代码功能就是如何为Delphi所编写的程序添加使用时间的限制功能,这里默认的时限为30天. 主要代码如下: unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Registry, Dialogs;

  • 一个简单的花指令伪装器-Delphi版木马彩衣

    说明:以VC++6的花指令为例说明 //VC++6外衣 1 OEPCODEFIVE: THEAD = ($55, $8B, $EC, $6A, $FF, $68, $00, $00, $00, $00, $68, $00, $00, $00, $00, $64, $A1, $00, $00, $00, $00, $50, $64, $89, $25, $00, $00, $00, $00, $83, $EC, $68, $53, $56, $57, $58, $58, $58, $83, $C4

  • Delphi实现图片滚动切换的完整实例代码

    本文以实例介绍了Delphi实现图片滚动切换的方法.该程序可以实现图像的滚动播出,并且通过本代码可控制窗口中的图像向上滚动,通过调节速度滚动条的值还可以调整滚动的速度:点击"停止"按钮,图像即停止滚动. 完整的功能代码如下: unit Unit1; interface uses Windows, Classes, Graphics, Forms, Controls, Menus, Dialogs, StdCtrls, ExtCtrls, SysUtils, ComCtrls, Butt

  • delphi设置开机自动启动函数具体实现

    有些程序要设置为开机启动,所以自己写了个函数方便以后使用,供大家参考 复制代码 代码如下: procedure TMainForm.SetAutoRun(ok: boolean); var Reg:TRegistry; //首先定义一个TRegistry类型的变量Reg begin Reg:=TRegistry.Create; try //创建一个新键 Reg.RootKey:=HKEY_LOCAL_MACHINE; //将根键设置为HKEY_LOCAL_MACHINE Reg.OpenKey(

  • Delphi实现木马自我拷贝方法

    木马实现自我拷贝的原理是程序运行时先查看自己是不是在特定目录下,如果是就继续运行,如果不是就把自己拷贝到特定目录下,然后运行新程序,继而退出旧程序. 本例即以Delphi实现木马的自我拷贝. 首先打开Delphi,新建一个工程,在窗口的Create事件中写入如下代码: procedure TForm1.FormCreate(Sender: TObject); var myname: string; begin myname := ExtractFilename(Application.Exena

  • Delphi实现图像文本旋转特效完整实例代码

    本文以实例讲述了Delphi实现图像文本旋转特效的解决方法,在本程序中利用的控件主要是Panel 控件.Image 控件.Edit 控件.Label 控件和Button 控件.本程序的关键是利用Delphi 的bmp_rotate()函数来实现旋转图像的功能.并巧妙地调用相关Windows API 函数来实现对文本的旋转特效. 完整的实例代码如下: unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics,

  • Delphi实现读取系统时间与日期完整实例

    本文讲述了Delphi读取系统时间与日期的实现方法,首先设置各个控件用于显示时间.读取时间与设置时间.再添加如下代码: unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; Button2: TButton; pr

随机推荐