在网上search了几天,找到的代码都是使用Netbios这个API发出命令来列举网卡数量,进而读取网卡地址。  但我经过多次测试,发现有如下问题:
插入网线,接通网络后,可以search到网卡,并读得网卡Mac地址。
但断开网线后,就查不到网卡了大家有没遇到这个问题,请问可以如何解决?????另: 从网上得知,网卡安装后,其MAC地址被记录在注册表中。请问如何从注册表中读取网卡MAC地址???

解决方案 »

  1.   

    注册表中好像要自己增加项目后才有,不用netbios,可WMI:
    unit wmiu;interfaceuses
      Windows, Messages, SysUtils, Variants, 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
    ActiveX, WbemScripting_TLB;
    function ADsEnumerateNext(pEnumVariant: IEnumVARIANT; cElements: ULONG;
      var pvar: OleVARIANT; var pcElementsFetched: ULONG): HRESULT; safecall; external 'activeds.dll';procedure DumpWMI_Process(Process: SWBemObject);
    var
      Enum: IEnumVARIANT;
      varArr: OleVariant;
      lNumElements: ULong;
      SProp: ISWbemProperty;
      Prop: OleVariant;
      PropName: string;
      PropType: string;
      PropValue: string;
    begin
      Form1.Memo1.Lines.Add('+ WMI Path: ' + Process.Path_.Path);
      Enum := Process.Properties_._NewEnum as IEnumVariant;
      while (Succeeded(ADsEnumerateNext(Enum, 1, VarArr, lNumElements))) and
        (lNumElements > 0) do
      begin
        if Succeeded(IDispatch(varArr).QueryInterface(SWBemProperty, SProp)) and
          Assigned(SProp) then
        begin
          try
            PropName  := SProp.Name;
            Prop := SProp.Get_Value;
            PropType := inttostr((VarType(Prop)));
            PropValue := VarToStr(Prop);
            Form1.Memo1.Lines.Add('  + ' + PropName + '[' + PropType + '] = ' + PropValue);
          except
            on E: Exception do
            begin
              // WriteLn(ErrOutput, PropName, ': ', E.Message);
            end;
          end;
        end;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      Server: string;
      Enum: IEnumVARIANT;
      varArr: OleVariant;
      lNumElements: ULong;
      AName: array[0..255] of Char;
      ASize: DWORD;
    begin
      if (ParamCount = 0) then
      begin
        Server := '';
        ASize  := SizeOf(AName) - 1;
        if GetComputerName(@AName, ASize) then Server := AName;
      end
      else
      begin
        Server := ParamStr(1);
      end;
      try
        Memo1.Lines.BeginUpdate;
        Enum := CoSWbemLocator.Create.ConnectServer(Server, 'root\cimv2', '',
          '', '', '', 0, nil).ExecQuery('Select  Caption,MACAddress from Win32_NetworkAdapterConfiguration where  MACAddress is not null', 'WQL',
          wbemFlagBidirectional, nil)._NewEnum as IEnumVariant;
        while (Succeeded(ADsEnumerateNext(Enum, 1, varArr, lNumElements))) and
          (lNumElements > 0) do
        begin
          DumpWMI_Process(IUnknown(varArr) as SWBemObject);
        end;
      finally
        Memo1.Lines.EndUpdate;
      end;
    end;end.
    ////其中,WbemScripting_TLB.pas可从
    ///http://www.truth4all.org/WbemScripting_TLB.pas
    ///下载
      

  2.   

    to : keiy() 
     大哥,用你的方法试了,可行,但运行结果内容太多,可否为小弟过滤掉多余的信息,小弟只要网卡的Mac地址,以检查应用程序的合法性,及用于生成注册码。。先谢了。。感激
      

  3.   

    //返回第一块网卡的MAC到edit1.text
    unit wmimac;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}uses
    ActiveX, WbemScripting_TLB;
    function ADsEnumerateNext(pEnumVariant: IEnumVARIANT; cElements: ULONG;
      var pvar: OleVARIANT; var pcElementsFetched: ULONG): HRESULT; safecall; external 'activeds.dll';procedure DumpWMI_Process(Process: SWBemObject);
    var
      Enum: IEnumVARIANT;
      varArr: OleVariant;
      lNumElements: ULong;
      SProp: ISWbemProperty;
      Prop: OleVariant;
      PropName: string;
      PropType: string;
      PropValue: string;
    begin
     // Form1.Memo1.Lines.Add('+ WMI Path: ' + Process.Path_.Path);
      Enum := Process.Properties_._NewEnum as IEnumVariant;
      while (Succeeded(ADsEnumerateNext(Enum, 1, VarArr, lNumElements))) and
        (lNumElements > 0) do
      begin
        if Succeeded(IDispatch(varArr).QueryInterface(SWBemProperty, SProp)) and
          Assigned(SProp) then
        begin
          try
            PropName  := SProp.Name;
            Prop := SProp.Get_Value;
            PropType := inttostr((VarType(Prop)));
            PropValue := VarToStr(Prop);
            if PropName='Caption' then
              if copy(PropValue,1,10)<>'[00000000]' then
                 exit;
            if PropName='MACAddress' then
            begin
               form1.edit1.Text:=PropValue;
               exit;
            end;
              // Form1.Memo1.Lines.Add('  + ' + PropName + '[' + PropType + '] = ' + PropValue);
          except
            on E: Exception do
            begin
              // WriteLn(ErrOutput, PropName, ': ', E.Message);
            end;
          end;
        end;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      Server: string;
      Enum: IEnumVARIANT;
      varArr: OleVariant;
      lNumElements: ULong;
      AName: array[0..255] of Char;
      ASize: DWORD;
    begin
      if (ParamCount = 0) then
      begin
        Server := '';
        ASize  := SizeOf(AName) - 1;
        if GetComputerName(@AName, ASize) then Server := AName;
      end
      else
      begin
        Server := ParamStr(1);
      end;
      try
      //  Memo1.Lines.BeginUpdate;
        Enum := CoSWbemLocator.Create.ConnectServer(Server, 'root\cimv2', '',
          '', '', '', 0, nil).ExecQuery('Select  Caption,MACAddress   from Win32_NetworkAdapterConfiguration where  MACAddress is not null ', 'WQL',
          wbemFlagBidirectional, nil)._NewEnum as IEnumVariant;
        while (Succeeded(ADsEnumerateNext(Enum, 1, varArr, lNumElements))) and
          (lNumElements > 0) do
        begin
          DumpWMI_Process(IUnknown(varArr) as SWBemObject);
        end;
      finally
    //    Memo1.Lines.EndUpdate;
      end;
    end;end.