求一个正确的,可以运行的 Delphi 读取 MAC地址 例子, 多谢 求一个正确的,可以运行的 Delphi 读取 MAC地址 例子, 多谢在下愚钝, 网上找了很多代码都未成正确运行。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 const sNetBiosError = 'NetBIOS错误%d';type TMACAddress = packed array[0..5] of Byte; ENetBiosError = class(Exception); TAStat = record Adapt: TAdapterStatus; NameBuff: array[0..30] of TNameBuffer; end;function GetMacAddress(AdapterNum: Integer): TMACAddress;var Ncb: TNCB; uRetCode: Char; J: Integer; Adapter: TAStat;begin FillChar(NCB, SizeOf(NCB), 0); with NCB do begin ncb_command := Char(NCBRESET); ncb_lana_num := Char(AdapterNum); end; uRetCode := Netbios(@Ncb); if uRetCode <> #0 then raise Exception.CreateFmt(sNetBIOSError, [Ord(uRetCode)]); FillChar(NCB, SizeOf(NCB), 0); with NCB do begin ncb_command := Char(NCBASTAT); ncb_lana_num := Char(AdapterNum); StrCopy(ncb_callname, '*'); ncb_buffer := @Adapter; ncb_length := sizeof(Adapter); end; uRetCode := Netbios(@Ncb); if uRetCode <> #0 then raise Exception.CreateFmt(sNetBIOSError, [Ord(uRetCode)]); for J := 0 to 5 do Result[J] := Ord(Adapter.Adapt.Adapter_address[J]);end;方法2:uses nb30;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 Result := ''; try ZeroMemory(@NCB, SizeOf(NCB)); // Zero control blocl NCB.ncb_command := Chr(NCBENUM); // Issue enum command cRC := NetBios(@NCB); NCB.ncb_buffer := @LANAENUM; // Reissue enum command NCB.ncb_length := SizeOf(LANAENUM); cRC := NetBios(@NCB); if Ord(cRC) <> 0 then exit; ZeroMemory(@NCB, SizeOf(NCB)); // Reset adapter NCB.ncb_command := Chr(NCBRESET); NCB.ncb_lana_num := LANAENUM.lana[a]; cRC := NetBios(@NCB); if Ord(cRC) <> 0 then exit; ZeroMemory(@NCB, SizeOf(NCB)); // Get adapter address 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); strTemp := ''; // Convert it to string for intIdx := 0 to 5 do strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]), 2); Result := strTemp; finally end;end; //NetBios函数获取MAC地址有的系统里会取不到。const MAX_ADAPTER_NAME_LENGTH = 256; MAX_ADAPTER_DESCRIPTION_LENGTH = 128; MAX_ADAPTER_ADDRESS_LENGTH = 8;type TIP_ADDRESS_STRING = record IPstring: array [0..15] of Char; end; PIP_ADDRESS_STRING = ^TIP_ADDRESS_STRING; TIP_MASK_STRING = TIP_ADDRESS_STRING; PIP_MASK_STRING = ^TIP_MASK_STRING; PIP_ADDR_STRING = ^TIP_ADDR_STRING; TIP_ADDR_STRING = record Next: PIP_ADDR_STRING; IpAddress: TIP_ADDRESS_STRING; //IP地址字符串 IpMask: TIP_MASK_STRING; //子网掩码字符串 Context: DWORD; //Netword table entry end; PIP_ADAPTER_INFO = ^TIP_ADAPTER_INFO; TIP_ADAPTER_INFO = packed record Next: PIP_ADAPTER_INFO; ComboIndex: DWORD; AdapterName: array [0..MAX_ADAPTER_NAME_LENGTH + 4-1] of Char; Description: array [0..MAX_ADAPTER_DESCRIPTION_LENGTH + 4-1] of Char; AddressLength: UINT; Address: array [0..MAX_ADAPTER_ADDRESS_LENGTH-1] of BYTE; Index: DWORD; dwType: UINT; DhcpEnabled: UINT; CurrentIpAddress: PIP_ADDR_STRING; IpAddressList: TIP_ADDR_STRING; GatewayList: TIP_ADDR_STRING; DhcpServer: TIP_ADDR_STRING ; HaveWins: BOOL; PrimaryWinsServer: TIP_ADDR_STRING; SecondaryWinsServer: TIP_ADDR_STRING; end;function GetAdaptersInfo(pAdapterInfo: PIP_ADAPTER_INFO; pOutBufLen: PDWORD): DWORD; stdcall; external 'IPHLPAPI.DLL' name 'GetAdaptersInfo';implementationprocedure ShowMACS();var pbuf: PIP_ADAPTER_INFO; buflen: DWORD; i: integer; mac: string;begin buflen := 0; if GetAdaptersInfo(pbuf, @bufLen) = ERROR_BUFFER_OVERFLOW then begin pbuf := AllocMem(buflen); if GetAdaptersInfo(pbuf, @bufLen) = ERROR_SUCCESS then while pbuf <> nil do begin mac := ''; for i := 0 to 5 do mac := mac + IntToHex(pbuf.Address[i], 2); ShowMessage(mac); pbuf := pbuf.Next; end; FreeMem(pbuf); end;end; 我以前也问过这个问题可是我出了100分,(100分也不多!)最近d版好象越来越便宜,放眼望去,都是20分,30分的.更要鄙视的是,就这20分还TMD不结贴. tmd 还是三个裤衩的,真的怀疑这些裤衩是怎么来的。lj 有谁知道gprs拨号程序怎么写吗? IdTCPServer接收包时,如何在未正式接收时先知道包的大小???? 求购一段MRP运算的代码?只需能够在BOM里运算出产品的物料清单就可以了。 Navbar怎么和ExpressBars有冲突啊?有人能帮我吗? 怎样将选择ListBox多个条项值,统计出来。散多分 马上结帖,急问????? 简单问题:TTable连结InterBase数据库,如何不出现登录提示? ado的问题哟 比比谁的单条SQL语句最长。先看我的: 关于DLL中的MDI窗口问题... adoquery 自己跳过sql语句执行,请问各位大侠怎么办才好? 关于数据库锁表的问题
sNetBiosError = 'NetBIOS错误%d';type
TMACAddress = packed array[0..5] of Byte;
ENetBiosError = class(Exception);
TAStat = record Adapt: TAdapterStatus;
NameBuff: array[0..30] of TNameBuffer;
end;function GetMacAddress(AdapterNum: Integer): TMACAddress;
var
Ncb: TNCB;
uRetCode: Char;
J: Integer;
Adapter: TAStat;
begin
FillChar(NCB, SizeOf(NCB), 0);
with NCB do
begin
ncb_command := Char(NCBRESET);
ncb_lana_num := Char(AdapterNum);
end;
uRetCode := Netbios(@Ncb);
if uRetCode <> #0 then raise Exception.CreateFmt(sNetBIOSError, [Ord(uRetCode)]);
FillChar(NCB, SizeOf(NCB), 0);
with NCB do
begin
ncb_command := Char(NCBASTAT);
ncb_lana_num := Char(AdapterNum);
StrCopy(ncb_callname, '*');
ncb_buffer := @Adapter;
ncb_length := sizeof(Adapter);
end;
uRetCode := Netbios(@Ncb);
if uRetCode <> #0 then raise Exception.CreateFmt(sNetBIOSError, [Ord(uRetCode)]);
for J := 0 to 5 do
Result[J] := Ord(Adapter.Adapt.Adapter_address[J]);
end;
方法2:uses nb30;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
Result := ''; try
ZeroMemory(@NCB, SizeOf(NCB)); // Zero control blocl NCB.ncb_command := Chr(NCBENUM); // Issue enum command
cRC := NetBios(@NCB); NCB.ncb_buffer := @LANAENUM; // Reissue enum command
NCB.ncb_length := SizeOf(LANAENUM);
cRC := NetBios(@NCB);
if Ord(cRC) <> 0 then
exit; ZeroMemory(@NCB, SizeOf(NCB)); // Reset adapter
NCB.ncb_command := Chr(NCBRESET);
NCB.ncb_lana_num := LANAENUM.lana[a];
cRC := NetBios(@NCB);
if Ord(cRC) <> 0 then
exit;
ZeroMemory(@NCB, SizeOf(NCB)); // Get adapter address
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); strTemp := ''; // Convert it to string
for intIdx := 0 to 5 do
strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]), 2);
Result := strTemp;
finally
end;
end;
MAX_ADAPTER_NAME_LENGTH = 256;
MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
MAX_ADAPTER_ADDRESS_LENGTH = 8;type
TIP_ADDRESS_STRING = record
IPstring: array [0..15] of Char;
end;
PIP_ADDRESS_STRING = ^TIP_ADDRESS_STRING;
TIP_MASK_STRING = TIP_ADDRESS_STRING;
PIP_MASK_STRING = ^TIP_MASK_STRING; PIP_ADDR_STRING = ^TIP_ADDR_STRING;
TIP_ADDR_STRING = record
Next: PIP_ADDR_STRING;
IpAddress: TIP_ADDRESS_STRING; //IP地址字符串
IpMask: TIP_MASK_STRING; //子网掩码字符串
Context: DWORD; //Netword table entry
end;
PIP_ADAPTER_INFO = ^TIP_ADAPTER_INFO;
TIP_ADAPTER_INFO = packed record
Next: PIP_ADAPTER_INFO;
ComboIndex: DWORD;
AdapterName: array [0..MAX_ADAPTER_NAME_LENGTH + 4-1] of Char;
Description: array [0..MAX_ADAPTER_DESCRIPTION_LENGTH + 4-1] of Char;
AddressLength: UINT;
Address: array [0..MAX_ADAPTER_ADDRESS_LENGTH-1] of BYTE;
Index: DWORD;
dwType: UINT;
DhcpEnabled: UINT;
CurrentIpAddress: PIP_ADDR_STRING;
IpAddressList: TIP_ADDR_STRING;
GatewayList: TIP_ADDR_STRING;
DhcpServer: TIP_ADDR_STRING ;
HaveWins: BOOL;
PrimaryWinsServer: TIP_ADDR_STRING;
SecondaryWinsServer: TIP_ADDR_STRING;
end;function GetAdaptersInfo(pAdapterInfo: PIP_ADAPTER_INFO;
pOutBufLen: PDWORD): DWORD; stdcall;
external 'IPHLPAPI.DLL' name 'GetAdaptersInfo';implementation
procedure ShowMACS();
var
pbuf: PIP_ADAPTER_INFO;
buflen: DWORD;
i: integer;
mac: string;
begin
buflen := 0;
if GetAdaptersInfo(pbuf, @bufLen) = ERROR_BUFFER_OVERFLOW then
begin
pbuf := AllocMem(buflen);
if GetAdaptersInfo(pbuf, @bufLen) = ERROR_SUCCESS then
while pbuf <> nil do
begin
mac := '';
for i := 0 to 5 do
mac := mac + IntToHex(pbuf.Address[i], 2);
ShowMessage(mac);
pbuf := pbuf.Next;
end;
FreeMem(pbuf);
end;
end;
最近d版好象越来越便宜,放眼望去,都是20分,30分的.
更要鄙视的是,就这20分还TMD不结贴.