如何用Delphi写得到网卡MAC地址?希望提供源程序!谢谢

解决方案 »

  1.   

    看看:http://www.delphifans.com/SoftView/SoftView_249.htmlhttp://www.lshdic.com/bbs/files.asp?dex=5&page=1&myclass=1680
      

  2.   


    netbios的要uses nb30function 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 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;function GetMACAdress_netbios(id:integer):string;
    var
    ts:Tstringlist;
    LanaEnum: PLanaEnum;
    I: Integer;
    AdapterStatus: PAdapterStatus;
    StatNCB: PNCB;
    MACAddress: PMACAddress;
    begin
    ts:=Tstringlist.create;
      New(LanaEnum);
      New(StatNCB);
      New(MACAddress);
      
      ZeroMemory(StatNCB, SizeOf(TNCB));
      StatNCB.ncb_length := SizeOf(TAdapterStatus) +  255 * SizeOf(TNameBuffer);
      GetMem(AdapterStatus, StatNCB.ncb_length);  ZeroMemory(LanaEnum, SizeOf(TLanaEnum));
      ZeroMemory(MACAddress, SizeOf(TMACAddress));  try
        if GetLanaEnum(LanaEnum) = NRC_GOODRET then
        begin
            for I := 0 to Byte(LanaEnum.length) - 1 do
              ts.Add(IntToStr(Byte(LanaEnum.lana[I])));
              with StatNCB^ do
              begin
               ResetLana(strtoint(Ts[0]), 0, 0, lrAlloc);
              ncb_buffer := PChar(AdapterStatus);
              ncb_callname := '*              ' + #0;
    //          ncb_lana_num := Char(LanaNum);
              ncb_lana_num := Char(strtoint(Ts[0]));
              ncb_command  := Char(NCBASTAT);
              NetBios(StatNCB);
    //          Result := Byte(ncb_cmd_cplt);
             if Byte(ncb_cmd_cplt) = NRC_GOODRET then
                 MoveMemory(MACAddress, AdapterStatus, SizeOf(TMACAddress));
             result:=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]])+#13;
        end;                    
        end;
     finally
    ts.free;
    Dispose(LanaEnum);
        FreeMem(AdapterStatus);
        Dispose(StatNCB);
        freemem(MACAddress);
    end;end;
      

  3.   

    请问,uses nb30,这个是什么???
      

  4.   

    呵呵 uses nb30 就是要引用nb30这个VCL单元
    如 VCL Reference
     TLabel  Hierarchy Properties Methods Events Using TLabel See also
    UnitStdCtrlsDescriptionUse TLabel to add text  that the user can抰 edit to a form. This text  can be used to label another control, and can set focus to that control when the user types an accelerator key.Because TLabel is not a descendant of TWinControl, it does not have its own window and can抰 receive direct input from the keyboard. To add an object to a form that can respond to keyboard input (other than setting focus to another object when an accelerator key is typed) in addition to displaying text, use TStaticText.To add an object to a form that displays text that a user can scroll or edit, use TEdit.
      

  5.   

    FORM:object Form1: TForm1
      Left = 223
      Top = 107
      Width = 335
      Height = 409
      Caption = 'Form1'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -12
      Font.Name = '宋体'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 12
      object Button1: TButton
        Left = 240
        Top = 352
        Width = 75
        Height = 25
        Caption = 'Button1'
        TabOrder = 0
        OnClick = Button1Click
      end
      object Memo1: TMemo
        Left = 8
        Top = 8
        Width = 309
        Height = 333
        TabOrder = 1
      end
    endPAS:unit Main;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}
    uses
      NB30;function GetAdapterInfo(Adapter_num: Char; var AMac_Addr: string): Boolean;
    type
      TStat = record
        Adapt: TAdapterStatus;
        Name_Buffer: TNameBuffer;
      end;
    var
      P: PNCB;
      Adapter: TStat;
      Temp: Char;
    begin
      New(P);
      try
      // 重置网卡,以便我们可以查询
        FillChar(P^, SizeOf(TNcb), #0);
        P^.ncb_command := Chr(NCBRESET);
        P^.ncb_lana_num := adapter_num;
        Temp := Netbios(P);
        if Temp <> Chr(NRC_GOODRET) then
        begin
          AMac_Addr := 'bad (NCBRESET): ' + P^.Ncb_retcode;
          Result := False;
          Exit;
        end;
        FillChar(P^, SizeOf(TNcb), #0);
        P^.ncb_command := Chr(NCBASTAT);
        P^.ncb_lana_num := Adapter_num;
        P^.ncb_callname := '*';
        FillChar(Adapter, SizeOf(TStat), #0);
        P^.ncb_buffer := @Adapter;
        P^.ncb_length := Sizeof(Adapter);
      // 取得网卡的信息,并且如果网卡正常工作的话,返回标准的冒号分隔格式。
        Temp := Netbios(P);
        if Temp = #0 then
        begin
          AMac_Addr := Format('%0.2x:%0.2x:%0.2x:%0.2x:%0.2x:%0.2x', [Ord(Adapter.adapt.adapter_address[0]),
            Ord(Adapter.adapt.adapter_address[1]), Ord(Adapter.adapt.adapter_address[2]),
              Ord(Adapter.adapt.adapter_address[3]), Ord(Adapter.adapt.adapter_address[4]),
              Ord(Adapter.adapt.adapter_address[5])]);
        end;
      finally
        Dispose(P);
      end;
      Result := True;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      AdapterList: TLanaEnum;
      Ncb: PNcb;
      I: Integer;
      Mac_addr: string;
    begin
      New(Ncb);
      try
      // 取得网卡列表
        FillChar(Ncb^, SizeOf(TNcb), #0);
        FillChar(AdapterList, Sizeof(AdapterList), #0);
        Ncb.ncb_command := Chr(NCBENUM);
        Ncb.ncb_buffer := @AdapterList;
        Ncb.ncb_length := Sizeof(AdapterList);
        Netbios(Ncb);
        for I := 0 to Ord(AdapterList.Length) - 1 do
        begin
          if GetAdapterInfo(AdapterList.lana[i], Mac_addr) then
            Memo1.Lines.Add(Format('Adapter %d''s MAC is %s', [Ord(AdapterList.lana[i]), Mac_addr]))
          else
          begin
            Memo1.Lines.Add('Failed to get MAC address! Do you have the NetBIOS protocol installed?');
            Break;
          end;
        end;
      finally
        Dispose(Ncb);
      end;
    end;end.
      

  6.   

    Netbios,这个是标准的API函数吗?