我在网上找到了一些获取网卡MAC的方法,但是有时一台电脑有两块网卡,或者更多的网卡。甚至包含无线网卡和有线网卡并存的情况,那么如何能够用遍历的方法,获取本机所有的网卡MAC地址到Memo控件中呢,谢谢。{以下是两个获取本机MAC的方法,请参考修改}
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
Const
MAX_ADAPTER_NAME_LENGTH = 256;
MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
MAX_ADAPTER_ADDRESS_LENGTH = 8;
Type
TIPAddressString = Array[0..4*4-1] of Char;
PIPAddrString = ^TIPAddrString;
TIPAddrString = Record
Next : PIPAddrString;
IPAddress : TIPAddressString;
IPMask : TIPAddressString;
Context : Integer;
End;
PIPAdapterInfo = ^TIPAdapterInfo;
TIPAdapterInfo = Record { IP_ADAPTER_INFO }
Next : PIPAdapterInfo;
ComboIndex : Integer;
AdapterName : Array[0..MAX_ADAPTER_NAME_LENGTH+3] of Char;
Description : Array[0..MAX_ADAPTER_DESCRIPTION_LENGTH+3] of Char;
AddressLength : Integer;
Address : Array[1..MAX_ADAPTER_ADDRESS_LENGTH] of Byte;
Index : Integer;
_Type : Integer;
DHCPEnabled : Integer;
CurrentIPAddress : PIPAddrString;
IPAddressList : TIPAddrString;
GatewayList : TIPAddrString;
End;Function GetAdaptersInfo(AI : PIPAdapterInfo; Var BufLen : Integer) : Integer;
StdCall; External 'iphlpapi.dll' Name 'GetAdaptersInfo';Function MACToStr(ByteArr : PByte; Len : Integer) : String;
Begin
Result := '';
While (Len > 0) do Begin
//2007-02-03
// Result := Result+IntToHex(ByteArr^,2)+'-';
Result := Result+IntToHex(ByteArr^,2);
ByteArr := Pointer(Integer(ByteArr)+SizeOf(Byte));
Dec(Len);
End;
//2007-02-03
// SetLength(Result,Length(Result)-1); { remove last dash }
End;Function GetAddrString(Addr : PIPAddrString) : String;
Begin
Result := '';
While (Addr <> nil) do Begin
Result := Result+'A: '+Addr^.IPAddress+' M: '+Addr^.IPMask+#13;
Addr := Addr^.Next;
End;
End;//获取网卡MAC地址
Function GetMacAddress1: string;
var
AI,Work : PIPAdapterInfo;
Size : Integer;
Res : Integer;
begin
try
Size := 5120;
GetMem(AI,Size);
work:=ai;
Res := GetAdaptersInfo(AI,Size);
If (Res <> ERROR_SUCCESS) Then
Begin
SetLastError(Res);
RaiseLastWin32Error;
End;
showmessage(Work^.Description);
showmessage(inttostr(Work^._Type));
Result:=MACToStr(@Work^.Address,Work^.AddressLength);
except
Result:='';
//showmessage('发生错误:请安装网卡和网卡驱动程序,并重新启用网卡!');
end;
end;function GetMacAddress2: string;
var
Lib: Cardinal;
Func: function(GUID: PGUID): Longint; stdcall;
GUID1, GUID2: TGUID;
begin
Result :=' ';
Lib := LoadLibrary('rpcrt4.dll');
if Lib <> 0 then
begin
if Win32Platform <>VER_PLATFORM_WIN32_NT then
@Func := GetProcAddress(Lib, 'UuidCreate')
else @Func := GetProcAddress(Lib, 'UuidCreateSequential');
if Assigned(Func) then
begin
if (Func(@GUID1) = 0) and
(Func(@GUID2) = 0) and
(GUID1.D4[2] = GUID2.D4[2]) and
(GUID1.D4[3] = GUID2.D4[3]) and
(GUID1.D4[4] = GUID2.D4[4]) and
(GUID1.D4[5] = GUID2.D4[5]) and
(GUID1.D4[6] = GUID2.D4[6]) and
(GUID1.D4[7] = GUID2.D4[7]) then
begin
Result :=
IntToHex(GUID1.D4[2], 2) +
IntToHex(GUID1.D4[3], 2) +
IntToHex(GUID1.D4[4], 2) +
IntToHex(GUID1.D4[5], 2) +
IntToHex(GUID1.D4[6], 2) +
IntToHex(GUID1.D4[7], 2);
end;
end;
FreeLibrary(Lib);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.text:=GetMacAddress1;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Edit2.Text:=GetMacAddress2;
end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
Const
MAX_ADAPTER_NAME_LENGTH = 256;
MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
MAX_ADAPTER_ADDRESS_LENGTH = 8;
Type
TIPAddressString = Array[0..4*4-1] of Char;
PIPAddrString = ^TIPAddrString;
TIPAddrString = Record
Next : PIPAddrString;
IPAddress : TIPAddressString;
IPMask : TIPAddressString;
Context : Integer;
End;
PIPAdapterInfo = ^TIPAdapterInfo;
TIPAdapterInfo = Record { IP_ADAPTER_INFO }
Next : PIPAdapterInfo;
ComboIndex : Integer;
AdapterName : Array[0..MAX_ADAPTER_NAME_LENGTH+3] of Char;
Description : Array[0..MAX_ADAPTER_DESCRIPTION_LENGTH+3] of Char;
AddressLength : Integer;
Address : Array[1..MAX_ADAPTER_ADDRESS_LENGTH] of Byte;
Index : Integer;
_Type : Integer;
DHCPEnabled : Integer;
CurrentIPAddress : PIPAddrString;
IPAddressList : TIPAddrString;
GatewayList : TIPAddrString;
End;Function GetAdaptersInfo(AI : PIPAdapterInfo; Var BufLen : Integer) : Integer;
StdCall; External 'iphlpapi.dll' Name 'GetAdaptersInfo';Function MACToStr(ByteArr : PByte; Len : Integer) : String;
Begin
Result := '';
While (Len > 0) do Begin
//2007-02-03
// Result := Result+IntToHex(ByteArr^,2)+'-';
Result := Result+IntToHex(ByteArr^,2);
ByteArr := Pointer(Integer(ByteArr)+SizeOf(Byte));
Dec(Len);
End;
//2007-02-03
// SetLength(Result,Length(Result)-1); { remove last dash }
End;Function GetAddrString(Addr : PIPAddrString) : String;
Begin
Result := '';
While (Addr <> nil) do Begin
Result := Result+'A: '+Addr^.IPAddress+' M: '+Addr^.IPMask+#13;
Addr := Addr^.Next;
End;
End;//获取网卡MAC地址
Function GetMacAddress1: string;
var
AI,Work : PIPAdapterInfo;
Size : Integer;
Res : Integer;
begin
try
Size := 5120;
GetMem(AI,Size);
work:=ai;
Res := GetAdaptersInfo(AI,Size);
If (Res <> ERROR_SUCCESS) Then
Begin
SetLastError(Res);
RaiseLastWin32Error;
End;
showmessage(Work^.Description);
showmessage(inttostr(Work^._Type));
Result:=MACToStr(@Work^.Address,Work^.AddressLength);
except
Result:='';
//showmessage('发生错误:请安装网卡和网卡驱动程序,并重新启用网卡!');
end;
end;function GetMacAddress2: string;
var
Lib: Cardinal;
Func: function(GUID: PGUID): Longint; stdcall;
GUID1, GUID2: TGUID;
begin
Result :=' ';
Lib := LoadLibrary('rpcrt4.dll');
if Lib <> 0 then
begin
if Win32Platform <>VER_PLATFORM_WIN32_NT then
@Func := GetProcAddress(Lib, 'UuidCreate')
else @Func := GetProcAddress(Lib, 'UuidCreateSequential');
if Assigned(Func) then
begin
if (Func(@GUID1) = 0) and
(Func(@GUID2) = 0) and
(GUID1.D4[2] = GUID2.D4[2]) and
(GUID1.D4[3] = GUID2.D4[3]) and
(GUID1.D4[4] = GUID2.D4[4]) and
(GUID1.D4[5] = GUID2.D4[5]) and
(GUID1.D4[6] = GUID2.D4[6]) and
(GUID1.D4[7] = GUID2.D4[7]) then
begin
Result :=
IntToHex(GUID1.D4[2], 2) +
IntToHex(GUID1.D4[3], 2) +
IntToHex(GUID1.D4[4], 2) +
IntToHex(GUID1.D4[5], 2) +
IntToHex(GUID1.D4[6], 2) +
IntToHex(GUID1.D4[7], 2);
end;
end;
FreeLibrary(Lib);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.text:=GetMacAddress1;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Edit2.Text:=GetMacAddress2;
end;end.
解决方案 »
- VC中的!非 和 && || 符号对应 delphi 中该如何写?
- delphi 7 中,怎样把函数的返回值定义为整数数组类型?
- 请教大家!!! 程序找不到.dcu的情况??
- 我的LISTVIEW不能显示所有要显示的字段
- 怎么传值?比如单击DBGRID中的一个COLUMN就可以把值传到一张报表的QRDBTEXT上
- MS Agent的问题
- 请帮我看一下action激活的问题
- 如何将鼠标改为自己想要的图片内容??VB中控件就提供自定义鼠标图片,但DELPHI中不知如何做??
- if name=''or password='' then语句为什么错?
- 滚动条的问题
- delphi如何生成一个exe文件 可以在任何计算机上运行
- 帮忙看看 如何改正(',' or ';' expected but 'BEGIN' found)
unit MainMAC;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, NB30;type
TGetMACForm = class(TForm)
Edit1: TEdit;
ComboBox1: TComboBox;
MACAddrButton: TButton;
ResetButton: TButton;
Label1: TLabel;
Label2: TLabel;
procedure MACAddrButtonClick(Sender: TObject);
procedure ResetButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
GetMACForm: TGetMACForm;implementation{$R *.DFM}type
TNBLanaResources = (lrAlloc, lrFree);type
PMACAddress = ^TMACAddress;
TMACAddress = array[0..5] of Byte;{
The LanaEnum parameter specifies structure describing accessible numbers Lana.
The amounty Lana is defined by number of the installed network cards and network
protocols for each of them. Each accessible Lana identifies a unique combination
of one driver of the protocol and one network card. Each structure NCB contains
a field ncb_lana_num, in which necessary number Lana (i.e. identifier of pair
protocol/MAC) should be written.
In systems with one network card, Lana = 0, usually corresponds
"to the protocol by default".
}// Get the list of adapters
function GetLanaEnum(LanaEnum: PLanaEnum): Byte;
var
LanaEnumNCB: PNCB;
begin
New(LanaEnumNCB);
ZeroMemory(LanaEnumNCB, SizeOf(TNCB));
try
with LanaEnumNCB^ do
begin
ncb_buffer := PChar(LanaEnum);
ncb_length := SizeOf(TLanaEnum);
ncb_command := Char(NCBENUM);
NetBios(LanaEnumNCB);
Result := Byte(ncb_cmd_cplt);
end;
finally
Dispose(LanaEnumNCB);
end;
end;procedure TGetMACForm.FormCreate(Sender: TObject);
var
LanaEnum: PLanaEnum;
I: Integer;
begin
Edit1.Text := '';
New(LanaEnum);
ZeroMemory(LanaEnum, SizeOf(TLanaEnum));
try
if GetLanaEnum(LanaEnum) = NRC_GOODRET then
begin
with ComboBox1, Items do
begin
Sorted := True;
BeginUpdate;
Clear;
for I := 0 to Byte(LanaEnum.length) - 1 do
Add(IntToStr(Byte(LanaEnum.lana[I])));
ItemIndex := 0;
EndUpdate;
end;
end;
finally
Dispose(LanaEnum);
end;
ResetButton.Enabled := (Win32Platform = VER_PLATFORM_WIN32_NT);
end;function ResetLana(LanaNum, ReqSessions, ReqNames: Byte;
LanaRes: TNBLanaResources): Byte;
var
ResetNCB: PNCB;
begin
New(ResetNCB);
ZeroMemory(ResetNCB, SizeOf(TNCB));
try
with ResetNCB^ do
begin
ncb_lana_num := Char(LanaNum); // Set Lana_Num
ncb_lsn := Char(LanaRes); // Allocation of new resources
ncb_callname[0] := Char(ReqSessions); // Query of max sessions
ncb_callname[1] := #0; // Query of max NCBs (default)
ncb_callname[2] := Char(ReqNames); // Query of max names
ncb_callname[3] := #0; // Query of use NAME_NUMBER_1
ncb_command := Char(NCBRESET);
NetBios(ResetNCB);
Result := Byte(ncb_cmd_cplt);
end;
finally
Dispose(ResetNCB);
end;
end;function GetMACAddress(LanaNum: Byte; MACAddress: PMACAddress): Byte;
var
AdapterStatus: PAdapterStatus;
StatNCB: PNCB;
begin
New(StatNCB);
ZeroMemory(StatNCB, SizeOf(TNCB));
StatNCB.ncb_length := SizeOf(TAdapterStatus) + 255 * SizeOf(TNameBuffer);
GetMem(AdapterStatus, StatNCB.ncb_length);
try
with StatNCB^ do
begin
ZeroMemory(MACAddress, SizeOf(TMACAddress));
ncb_buffer := PChar(AdapterStatus);
ncb_callname := '* ' + #0;
ncb_lana_num := Char(LanaNum);
ncb_command := Char(NCBASTAT);
NetBios(StatNCB);
Result := Byte(ncb_cmd_cplt);
if Result = NRC_GOODRET then
MoveMemory(MACAddress, AdapterStatus, SizeOf(TMACAddress));
end;
finally
FreeMem(AdapterStatus);
Dispose(StatNCB);
end;
end;procedure TGetMACForm.MACAddrButtonClick(Sender: TObject);
var
LanaNum: Byte;
MACAddress: PMACAddress;
RetCode: Byte;
begin
LanaNum := StrToInt(ComboBox1.Text);
New(MACAddress);
try
RetCode := GetMACAddress(LanaNum, MACAddress);
if RetCode = NRC_GOODRET then
begin
Edit1.Text := Format('%2.2x-%2.2x-%2.2x-%2.2x-%2.2x-%2.2x',
[MACAddress[0], MACAddress[1], MACAddress[2],
MACAddress[3], MACAddress[4], MACAddress[5]]);
end else
begin
Beep;
Edit1.Text := 'Error';
ShowMessage('GetMACAddress Error! RetCode = $' + IntToHex(RetCode, 2));
end;
finally
Dispose(MACAddress);
end;
end;procedure TGetMACForm.ResetButtonClick(Sender: TObject);
var
RetCode: Byte;
LanaNum: Byte;
begin
LanaNum := StrToInt(ComboBox1.Text);
RetCode := ResetLana(LanaNum, 0, 0, lrAlloc);
if RetCode <> NRC_GOODRET then
begin
Beep;
ShowMessage('Reset Error! RetCode = $' + IntToHex(RetCode, 2));
end;
end;procedure TGetMACForm.ComboBox1Change(Sender: TObject);
begin
Edit1.Text := '';
end;end.
http://blog.csdn.net/flexitime/archive/2009/08/24/4478241.aspx
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Nb30, ComCtrls;const
MAX_ADAPTER_NAME_LENGTH = 256;
MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
MAX_ADAPTER_ADDRESS_LENGTH = 8;
type
TIPAddressString = array[0..4 * 4 - 1] of Char;
PIPAddrString = ^TIPAddrString;
TIPAddrString = record
Next: PIPAddrString;
IPAddress: TIPAddressString;
IPMask: TIPAddressString;
Context: Integer;
end;
PIPAdapterInfo = ^TIPAdapterInfo;
TIPAdapterInfo = record { IP_ADAPTER_INFO }
Next: PIPAdapterInfo;
ComboIndex: Integer;
AdapterName: array[0..MAX_ADAPTER_NAME_LENGTH + 3] of Char;
Description: array[0..MAX_ADAPTER_DESCRIPTION_LENGTH + 3] of Char;
AddressLength: Integer;
Address: array[1..MAX_ADAPTER_ADDRESS_LENGTH] of Byte;
Index: Integer;
_Type: Integer;
DHCPEnabled: Integer;
CurrentIPAddress: PIPAddrString;
IPAddressList: TIPAddrString;
GatewayList: TIPAddrString;
DHCPServer: TIPAddrString;
HaveWINS: Bool;
PrimaryWINSServer: TIPAddrString;
SecondaryWINSServer: TIPAddrString;
LeaseObtained: Integer;
LeaseExpires: Integer;
end;
type
TForm1 = class(TForm)
Button1: TButton;
ListView1: TListView;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;function GetAdaptersInfo(AI: PIPAdapterInfo; var BufLen: Integer): Integer; stdcall; external 'iphlpapi.dll' NAME 'GetAdaptersInfo';implementation{$R *.dfm}function GetAdapterInformation(AdapterInfo: TStringList): Boolean;
var
AI, Work: PIPAdapterInfo;
Size: Integer;
res: Integer;
i: Integer;
lst_info: TStringList; function MACToStr(ByteArr: PByte; Len: Integer): string;
begin
result := '';
while (Len > 0) do
begin
result := result + IntToHex(ByteArr^, 2) + '-';
ByteArr := Pointer(Integer(ByteArr) + SizeOf(Byte));
Dec(Len);
end; SetLength(result, length(result) - 1); { remove last dash } end; function GetAddrString(Addr: PIPAddrString): string;
begin
result := '';
while (Addr <> nil) do
begin
result := result + 'A: ' + Addr^.IPAddress + ' M: ' + Addr^.IPMask + #13;
Addr := Addr^.Next;
end; end; function TimeTToDateTimeStr(TimeT: Integer): string;
const
UnixDateDelta = 25569; { days between 12/31/1899 and 1/1/1970 }
var
DT: TDateTime;
TZ: TTimeZoneInformation;
res: DWORD; begin
if (TimeT = 0) then
result := ''
else
begin
{ Unix TIME_T is secs since 1/1/1970 }
DT := UnixDateDelta + (TimeT / (24 * 60 * 60)); { in UTC }
{ calculate bias }
res := GetTimeZoneInformation(TZ);
if (res = TIME_ZONE_ID_INVALID) then
RaiseLastWin32Error;
if (res = TIME_ZONE_ID_STANDARD) then
begin
DT := DT - ((TZ.Bias + TZ.StandardBias) / (24 * 60));
result := DateTimeToStr(DT) + ' ' + WideCharToString(TZ.StandardName);
end
else
begin { daylight saving time }
DT := DT - ((TZ.Bias + TZ.DaylightBias) / (24 * 60));
result := DateTimeToStr(DT) + ' ' + WideCharToString(TZ.DaylightName);
end;
end;
end;begin
Size := 5120;
GetMem(AI, Size);
res := GetAdaptersInfo(AI, Size); if (res <> ERROR_SUCCESS) then
begin
SetLastError(res);
RaiseLastWin32Error;
end; Work := AI;
repeat
lst_info := TStringList.Create;
lst_info.Add(Work^.Description);
lst_info.Add(MACToStr(@Work^.Address, Work^.AddressLength)); AdapterInfo.AddObject('', lst_info); Work := Work^.Next;
until (Work = nil); FreeMem(AI);
end;procedure TForm1.Button1Click(Sender: TObject);
var
lst_info: TStringList;
i: Integer;
begin
lst_info := TStringList.Create;
if GetAdapterInformation(lst_info) then ShowMessage('获取Adapter Information完成!'); for i := 0 to lst_info.Count - 1 do
begin
ListView1.Items.Add.SubItems := TStringList(lst_info.Objects[i]);
end;
end;end.
代码很精简:
try
idhttp1.ReadTimeout:=6000; //超时时间
str:=idhttp1.Get('http://www.xx.org/ip.asp');
if idhttp1.ResponseCode=200 then begin
str2:=GetMID(str,'<body>','</body>');
Label2.caption:='当前IP:【'+str2+'】';
end;
except
Label2.caption:='【当前网络异常!】';
end;
begin
result := '';
while (Addr <> nil) do
begin
result := result + 'A: ' + Addr^.IPAddress + ' M: ' + Addr^.IPMask + #13;
Addr := Addr^.Next;
end; end; function TimeTToDateTimeStr(TimeT: Integer): string;
const
UnixDateDelta = 25569; { days between 12/31/1899 and 1/1/1970 }
var
DT: TDateTime;
TZ: TTimeZoneInformation;
res: DWORD; begin
if (TimeT = 0) then
result := ''
else
begin
{ Unix TIME_T is secs since 1/1/1970 }
DT := UnixDateDelta + (TimeT / (24 * 60 * 60)); { in UTC }
{ calculate bias }
res := GetTimeZoneInformation(TZ);
if (res = TIME_ZONE_ID_INVALID) then
RaiseLastWin32Error;
if (res = TIME_ZONE_ID_STANDARD) then
begin
DT := DT - ((TZ.Bias + TZ.StandardBias) / (24 * 60));
result := DateTimeToStr(DT) + ' ' + WideCharToString(TZ.StandardName);
end
else
begin { daylight saving time }
DT := DT - ((TZ.Bias + TZ.DaylightBias) / (24 * 60));
result := DateTimeToStr(DT) + ' ' + WideCharToString(TZ.DaylightName);
end;
end;
end;