DELPHI简单问题
我想通过iphlpapi.dll 获取子网掩码和IP地址,怎么用的,给点代码啊,我查不到 不想通过注册表获取
我想通过iphlpapi.dll 获取子网掩码和IP地址,怎么用的,给点代码啊,我查不到 不想通过注册表获取
答案:参考代码:
unit Unit1;
inte易做图ce
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
Const
MAX_ADAPTER_NAME_LENGTH = 256;
MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
MAX_ADAPTER_ADDRESS_LENGTH = 8;
Type
TIPAddressString = Array [0 .. 4 * 4 - 1] of Char;
PIPAddrString = ^TIPAddrString;
TIPAddrString = Record
Next: PIPAddrString;
IPAddress: TIPAddressString;
IPMask: TIPAddressString;
Context: Integer;
End;
PIPAdapterInfo = ^TIPAdapterInfo;
TIPAdapterInfo = Record { IP_ADAPTER_INFO }
Next: PIPAdapterInfo;
ComboIndex: Integer;
AdapterName: Array [0 .. MAX_ADAPTER_NAME_LENGTH + 3] of Char;
Description: Array [0 .. MAX_ADAPTER_DESCRIPTION_LENGTH + 3] of Char;
AddressLength: Integer;
Address: Array [1 .. MAX_ADAPTER_ADDRESS_LENGTH] of Byte;
Index: Integer;
_Type: Integer;
DHCPEnabled: Integer;
CurrentIPAddress: PIPAddrString;
IPAddressList: TIPAddrString;
GatewayList: TIPAddrString;
End;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
Function GetAdaptersInfo(AI: PIPAdapterInfo; Var BufLen: Integer): Integer;
StdCall; External 'iphlpapi.dll' Name 'GetAdaptersInfo';
Function MACToStr(ByteArr: PByte; Len: Integer): String;
Begin
Result := '';
While (Len > 0) do
Begin
Result := Result + IntToHex(ByteArr^, 2) + '-';
ByteArr := Pointer(Integer(ByteArr) + SizeOf(Byte));
Dec(Len);
End;
SetLength(Result, Length(Result) - 1); { remove last dash }
End;
Function GetAddrString(Addr: PIPAddrString): String;
Begin
Result := '';
While (Addr <> nil) do
Begin
Result := Result + 'A: ' + Addr^.IPAddress + ' M: ' + Addr^.IPMask + #13;
Addr := Addr^.Next;
End;
End;
procedure TForm1.Button1Click(Sender: TObject);
var
AI, Work: PIPAdapterInfo;
Size: Integer;
Res: Integer;
begin
Size := 5120;
GetMem(AI, Size);
Work := AI;
Res := GetAdaptersInfo(AI, Size);
If (Res <> ERROR_SUCCESS) Then
Begin
SetLastError(Res);
RaiseLastWin32Error;
exit;
End;
Memo1.Lines.Add('Adapter address: ' + MACToStr(@Work^.Address,
Work^.AddressLength));
repeat
Memo1.Lines.Add(' IP addresses: ' + Work.IPAddressList.IPAddress +
' Gateway Address=' + Work.GatewayList.IPAddress);
Work := Work^.Next;
until (Work = nil);
end;
end.
其他:菜鸟飞过 你把C:\Program Files\Borland\Delphi7\Projects\Project1.exe
这个文件 删除
在点击delphi编程环境里面的运行就可以了
如果不行 就把 delphi关了重新打开这个程序在 编译执行以下就可以了
上一个:谁有delphi的视频教程,网址链接的,要能看的,清晰的。。。
下一个:delphi难啊 高手光顾一下,解释一下行吗