⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _getmacdemo1.~pas

📁 一个delphi的获取网卡信息的示范源码
💻 ~PAS
字号:
unit _GetMacDemo1;

interface

uses
  nb30,
  WinSock,

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Edit1: TEdit;
    procedure   Button1Click(Sender:   TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

    function GDLL_NBGetAdapterAddress(a:integer):String;
    function _SPGetIPAddress():string;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender:   TObject);
begin
  memo1.lines.add('您的第'+edit1.text+'个适配器的MAC地址为:'+GDLL_NBGetAdapterAddress(StrtoIntdef(Edit1.Text,0)));
  memo1.lines.add('您的IP地址为:'+_SPGetIPAddress());
end;

function GDLL_NBGetAdapterAddress(a:integer):String;
//a指定多个网卡适配器中的哪一个0,1,2...
Var
      NCB:TNCB;   //   Netbios   control   block   file://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
      //   Initialize
      Result   :=   '';

      Try
          //   Zero   control   blocl
          ZeroMemory(@NCB,   SizeOf(NCB));

          //   Issue   enum   command
          NCB.ncb_command:=Chr(NCBENUM);
          cRC   :=   NetBios(@NCB);

          //   Reissue   enum   command
          NCB.ncb_buffer   :=   @LANAENUM;
          NCB.ncb_length   :=   SizeOf(LANAENUM);
          cRC   :=   NetBios(@NCB);
          If   Ord(cRC)<>0   Then
              exit;

          //   Reset   adapter
          ZeroMemory(@NCB,   SizeOf(NCB));
          NCB.ncb_command   :=   Chr(NCBRESET);
          NCB.ncb_lana_num   :=   LANAENUM.lana[a];
          cRC   :=   NetBios(@NCB);
          If   Ord(cRC)<>0   Then
              exit;

          //   Get   adapter   address
          ZeroMemory(@NCB,   SizeOf(NCB));
          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);

          //   Convert   it   to   string
          strTemp   :=   '';
          For   intIdx   :=   0   To   5   Do
              strTemp   :=   strTemp   +   InttoHex(Integer(ADAPTER.adapter_address[intIdx]),2);
          Result   :=   strTemp;
      Finally
      End;

end;


function _SPGetIPAddress():string;
var
  ip    :string;
  ipstr :string;
  ch    :array [1..32] of char;
  i     :integer;
  WSData:TWSAData;
  MyHost:PHostEnt;

begin
  result:='';

  if WSAstartup(2,wsdata)<>0 then
    begin
        Exit;
    end;

  try
    if getHostName(@ch[1],32)<>0 then
      begin
        Exit;
      end;
  except
    begin
        Exit;
    end;

  MyHost:=GetHostByName(@ch[1]);
  if MyHost=NIL then
    begin
        Exit
    end
  else
    begin
       for i:=1 to 4 do
         begin
            ip:=inttostr(Ord(MyHost.h_addr^[i-1]));
            ipstr:=ipstr+ip;

            if i<4 then
              ipstr:=ipstr+'.'
            else
              result:=ipstr;
         end;
    end;
end;


end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -