近日偶要做个程序,需要获得外网IP,偶去网上找相关资料,发现都不尽如人意,有的 只能获得本地网卡的IP,有的通过httpget控件获取IP,还有甚者做个asp再调用偶是个 懒人,而且上面提到的方法,不是获取不了就是速度太慢或者是太麻烦,无意中发现 有个系统提供的API,利用它能够获取适配器信息,其中就包括外网IP,下面把代码给 大家,重点处我做了注释。
注意: 程序必须运行在直接拨号联网的用户,不适用于内网用户。 //常量定义 Const MAX_HOSTNAME_LEN = 128; MAX_DOMAIN_NAME_LEN = 128; MAX_SCOPE_ID_LEN = 256; 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;
PFixedInfo = ^TFixedInfo; TFixedInfo = Record { FIXED_INFO } HostName : Array[0..MAX_HOSTNAME_LEN+3] of Char; DomainName : Array[0..MAX_DOMAIN_NAME_LEN+3] of Char; CurrentDNSServer : PIPAddrString; DNSServerList : TIPAddrString; NodeType : Integer; ScopeId : Array[0..MAX_SCOPE_ID_LEN+3] of Char; EnableRouting : Integer; EnableProxy : Integer; EnableDNS : 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; DHCPServer : TIPAddrString; HaveWINS : Bool; PrimaryWINSServer : TIPAddrString; SecondaryWINSServer : TIPAddrString; LeaseObtained : Integer; LeaseExpires : Integer; End; type TGetAdaptersInfo=function(AI : PIPAdapterInfo; Var BufLen : Integer) : Integer;StdCall; var GetAdaptersInfo:TGetAdaptersInfo; h:hmodule;
implementation
function Loadiphlpapidll:boolean;//动态加载iphlpapi.dll中的GetAdaptersInfo begin if h>0 then exit; h:=Loadlibrary('iphlpapi.dll'); if h>0 then GetAdaptersInfo:=GetProcAddress(h,'GetAdaptersInfo'); result:=assigned(GetAdaptersInfo); end; function GetWanIP: string;//获取外网IP Var AI,Work : PIPAdapterInfo; Size : Integer; Res : Integer; Description:string; WanIP:string; 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; begin Loadiphlpapidll; result:='无法获得外网IP'; Size := 5120; GetMem(AI,Size); Res := GetAdaptersInfo(AI,Size); If (Res <> ERROR_SUCCESS) Then Begin MessageBoxA(0,'获取外网IP失败 ','错误',MB_OK or MB_ICONERROR); exit; End; Work := AI; Repeat Description:=strpas(Work^.Description); if pos('WAN',Description)>0 then begin WanIP:=GetAddrString(@Work^.IPAddressList); //这里返回的WanIP是这种格式 A: 222.111.25.32 M: 255.255.255.0 //其中A到M中间就是外网IP了 WanIP:=copy(WanIP,pos(':',WanIP)+1,pos('M',WanIP)-pos(':',WanIP)-2); result:=trim(WanIP);//这里就是最终的外网IP了 exit; end; Work := Work^.Next; Until (Work = nil); FreeMem(AI);
end;
http://blog.csdn.net/yanjiaye520/article/details/7702409
|
请发表评论