軟體授權綁定、機器唯一識別常需取得網卡實體位址 (MAC Address)。本文提供完整 Delphi 單元 (Nb.pas) 封裝 Windows NetBIOS API (Netbios),支援 32 位元環境,可列舉所有 LANA 編號並查詢各介面卡 MAC。
NbLanaEnum:列舉系統所有 LANA 編號。NbReset(LanaNum):重設指定 LANA (Win32 必須)。NbGetMacAddr(LanaNum): String:傳回格式 XX:XX:XX:XX:XX:XX 的 MAC 字串。unit Nb;
interface
uses SysUtils, WinProcs, WinTypes;
const
NCB_ENUM = $37; NCB_ADPSTAT = $33; NCB_RESET = $32;
NRC_GOODRET = $00; MAXLANAS = 254; NBNAMESIZE = 16;
type
TMacAddress = array[0..5] of Byte;
TNCB = packed record ... end; // NetBIOS Control Block
TAdpStat = packed record
ID: TMacAddress; // MAC 位址在此
...
end;
TLana_Enum = packed record Length: Byte; Lana: array[0..MAXLANAS-1] of Byte; end;
function NetbiosCmd(var NCB: TNCB): Word;
function NbLanaEnum: TLana_Enum;
function NbReset(Lana: Byte): Word;
function NbGetMacAddr(LanaNum: Integer): String;
implementation
// ... 完整實作包含 Win32 CallNetbios、結構定義、錯誤碼常數 ...
// 關鍵:NbGetMacAddr 發送 NCB_ADPSTAT 命令,CallName='*', 解析 AdpStat.ID
end.
unit Unit1;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Nb, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel; Memo1: TMemo; Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
end;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
var
LEnum: TLana_Enum; Ret: Word; I: Integer;
begin
LEnum := NbLanaEnum;
if LEnum.Length = 0 then
begin
Button1.Caption := Format('Enum Err: %.2X', [LEnum.Lana[0]]); Exit;
end;
for I := 0 to LEnum.Length - 1 do
begin
Ret := NbReset(LEnum.Lana[I]);
if Ret <> NRC_GOODRET then
begin
Button1.Caption := Format('Reset %d Err: %.2X', [I, Ret]); Exit;
end;
Memo1.Lines.Add(Format('LANA %.2X = %s', [LEnum.Lana[I], NbGetMacAddr(I)]));
end;
Button1.Caption := 'Done';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin Close; end;
end.
Nb.pas 加入專案,或編譯為獨立單元。TServiceApplication、TService、TServiceThread 架構解析
GDI+ 路徑漸變刷預設曲線:SetBlendTriangularShape 與 SetBlendBellShape 一鍵生成三角/鐘形混合
再談多型:表單插件架構的實戰應用範例