得本机器IP地址(100分)

  • 主题发起人 主题发起人 vividwang
  • 开始时间 开始时间
网关地址
 
似乎可以通过读注册表的方式得到网关地址
 
靠,来迟了
 
注册表好象只能在98下得到,我只指的是在Internet上的地址.
 
2000下也可以得到,不过好像每个人注册表中纪录网关的那个接口
{一长串数字} 都不一样 如何解决? 呵呵

to 龙星
你发的那个程序也只能得到内部IP
 
用IPHLPAPI.DLL中API嘛,很简单的。在BCB中有一个iphlpapi.h 的头文件,把它加到工程,并且把Lib/Psdk/iphlpapi.lib也加入工程,编辑就Ok啦。
用此文件中的API可以检测本地所有连接的IP地址,子网掩码,网关,DNS等,并可以做相应的设置,如更改或添加IP地址,路由;更有甚者它可以编写出网络监视软件,并断开指定的连接。
 
通过OICQ0285啊[:)][:D][:D]
 
楼主,这个问题其实不是很复杂。

我想你只是弄错了一个工作顺序,我的推测,你认为你先要知道所谓的Internet上的地址,然后把这个地址告诉你的服务器,你是不是这么想的?
实际上,你只要和一个服务器建立了联接,服务器自然就可以知道你的所谓的Internet上的地址,不需要你去告诉他。
或者说你像知道这个地址,最好是让服务器告诉你,为什么呢??因为ip地址是机上是相对连接存在的,一个机器可能有很多的连接。你想得到的ip只能说是相对那个联接的
 
To tinyfool,
多谢提醒,你说得很对,但我的问题不太一样,我没有服务器.
 
我说的服务器不是狭义的服务器,反正你写程序的时候,肯定是客户机+服务器,我不明白只有一端怎么联系的?
 
to skybuilder,
好像里面的函数微软的Msdn都没有写进去啊,只有能查到,没有公布设置的函数,若是兄台知道
发一份文档给我,好吗?
 
发这个过来也行啊iphlpapi.h
 
楼上的我有pas版本的……
 
选中模块文件:C:/WINNT/System32/IPHLPAPI.DLL
存盘时间:2003年03月21日 星期五 19时07分51秒
操作系统:Windows 2000, Build: 2195


项目名称 模块文件信息
说明 IP Helper API
版权 Copyright (C) Microsoft Corp. 1981-1999
公司名称 Microsoft Corporation
文件版本 5.00.2173.2
内部名称 IPHLPAPI
源文件名 IPHLPAPI.DLL
产品名称 Microsoft(R) Windows (R) 2000 Operating System
产品版本 5.00.2173.2
文件类型 应用程序扩展
创建时间 2002年07月18日 00:01:23
修改时间 2000年01月10日 12:00:00
访问时间 2003年03月21日
文件大小 67 KB (69,392 字节)

特性 0
函数数目 117
函数名数目 117
函数功能地址(RVA) 0000CCC8
函数名地址(RVA) 0000CF86
函数名序号地址(RVA) 0000CE9C


序号 地址 API函数解析
1 00007520 AddIPAddress
2 0000324A AllocateAndGetArpEntTableFromStack
3 00002749 AllocateAndGetIfTableFromStack
4 00008E0A AllocateAndGetIpAddrTableFromStack
5 000090D1 AllocateAndGetIpForwardTableFromStack
6 000092DE AllocateAndGetIpNetTableFromStack
7 00008E6F AllocateAndGetTcpTableFromStack
8 00008FDA AllocateAndGetUdpTableFromStack
9 00006550 CreateIpForwardEntry
10 000066E7 CreateIpNetEntry
11 00006993 CreateProxyArpEntry
12 000075EC DeleteIPAddress
13 000065FC DeleteIpForwardEntry
14 0000679E DeleteIpNetEntry
15 00006A90 DeleteProxyArpEntry
16 00007990 EnableRouter
17 000067EF FlushIpNetTable
18 0000963F FlushIpNetTableFromStack
19 00007445 GetAdapterIndex
20 00003F1E GetAdapterOrderMap
21 000011F8 GetAdaptersInfo
22 000068A0 GetBestInterface
23 00009981 GetBestInterfaceFromStack
24 00006949 GetBestRoute
25 00009A91 GetBestRouteFromStack
26 00006B8D GetFriendlyIfIndex
27 00002978 GetIcmpStatistics
28 000029C9 GetIcmpStatsFromStack
29 0000649E GetIfEntry
30 00008A6A GetIfEntryFromStack
31 0000431C GetIfTable
32 00004084 GetIfTableFromStack
33 00007342 GetIgmpList
34 00003D95 GetInterfaceInfo
35 000025A3 GetIpAddrTable
36 00002685 GetIpAddrTableFromStack
37 0000600A GetIpForwardTable
38 0000915C GetIpForwardTableFromStack
39 00005EC9 GetIpNetTable
40 00009374 GetIpNetTableFromStack
41 000042CB GetIpStatistics
42 00003E29 GetIpStatsFromStack
43 00007A7B GetNetworkParams
44 0000425D GetNumberOfInterfaces
45 00002175 GetPerAdapterInfo
46 00007701 GetRTTAndHopCount
47 00002804 GetTcpStatistics
48 00002855 GetTcpStatsFromStack
49 0000611C GetTcpTable
50 00008ECC GetTcpTableFromStack
51 000028BE GetUdpStatistics
52 0000290F GetUdpStatsFromStack
53 0000623C GetUdpTable
54 00009034 GetUdpTableFromStack
55 0000733C GetUniDirectionalAdapterInfo
56 000070DA InternalCreateIpForwardEntry
57 00007213 InternalCreateIpNetEntry
58 00007168 InternalDeleteIpForwardEntry
59 00007293 InternalDeleteIpNetEntry
60 00006C51 InternalGetIfTable
61 00006D2C InternalGetIpAddrTable
62 00006E8C InternalGetIpForwardTable
63 00006DDB InternalGetIpNetTable
64 00006F40 InternalGetTcpTable
65 00006FEF InternalGetUdpTable
66 00007097 InternalSetIfEntry
67 00007121 InternalSetIpForwardEntry
68 00007253 InternalSetIpNetEntry
69 000071D5 InternalSetIpStats
70 000072ED InternalSetTcpEntry
71 00007ADC IpReleaseAddress
72 00007B2E IpRenewAddress
73 00007817 IsLocalAddress
74 0000B24A NTPTimeToNTFileTime
75 0000B193 NTTimeToNTPTime
76 00008551 NhGetGuidFromInterfaceName
77 000084FC NhGetInterfaceNameFromGuid
78 00008CD3 NhpAllocateAndGetInterfaceInfoFromStack
79 00008C2F NhpGetInterfaceIndexFromStack
80 00007866 NotifyAddrChange
81 000078EB NotifyRouteChange
82 000078FD NotifyRouteChangeEx
83 00009BA2 _PfAddFiltersToInterface@24
84 00009C6C _PfAddGlobalFilterToInterface@8
85 00009DD7 _PfBindInterfaceToIPAddress@12
86 00009D17 _PfBindInterfaceToIndex@16
87 00009B6E _PfCreateInterface@24
88 00009B91 _PfDeleteInterface@4
89 00009F85 _PfDeleteLog@0
90 00009F45 _PfGetInterfaceStatistics@16
91 00009F0E _PfMakeLog@4
92 00009E7E _PfRebindFilters@8
93 00009C2F _PfRemoveFilterHandles@12
94 00009BEA _PfRemoveFiltersFromInterface@20
95 00009CA6 _PfRemoveGlobalFilterFromInterface@8
96 00009F1F _PfSetLogBuffer@28
97 00009FC7 _PfTestPacket@20
98 00009CE0 _PfUnBindInterface@4
99 00007B80 SendARP
100 0000BDA1 SetAdapterIpAddress
101 00007387 SetBlockRoutes
102 000064E0 SetIfEntry
103 00008B0A SetIfEntryToStack
104 000065A6 SetIpForwardEntry
105 00009839 SetIpForwardEntryToStack
106 0000991C SetIpMultihopRouteEntryToStack
107 0000674D SetIpNetEntry
108 00009567 SetIpNetEntryToStack
109 000098AD SetIpRouteEntryToStack
110 00006696 SetIpStatistics
111 000094DA SetIpStatsToStack
112 00006659 SetIpTTL
113 00009684 SetProxyArpEntryToStack
114 000073F6 SetRouteWithRef
115 0000684F SetTcpEntry
116 00008F6E SetTcpEntryToStack
117 00007A15 UnenableRouter
 
看到强横的美国现在到处横行霸道……真是令人气愤
吾辈当努力学习科学文化知识,学习美国最好的技术将来对付这个中国未来的敌人!
等全部转化完毕后,有意者跟我联系吧!
里面有可以解决你问题的函数!!!需要DEMO也可以跟我联系!
unit IPHLPAPI;

//------------------------------------------------------------------------------
// 部分转化自IPHLPAPI.DLL(IP-Helper API)
// 改良自:嘟嘟猫 ddt_2k2@163.com
//------------------------------------------------------------------------------

interface
uses
Windows, winsock;

//------------- 从Microsoft IPTYPES.H文件转化--------------------------------

const
ANY_SIZE = 1;
MAX_ADAPTER_DESCRIPTION_LENGTH = 128; // arb.
MAX_ADAPTER_NAME_LENGTH = 256; // arb.
MAX_ADAPTER_ADDRESS_LENGTH = 8; // arb.
DEFAULT_MINIMUM_ENTITIES = 32; // arb.
MAX_HOSTNAME_LEN = 128; // arb.
MAX_DOMAIN_NAME_LEN = 128; // arb.
MAX_SCOPE_ID_LEN = 256; // arb.

//节点类型 ( NETBIOS)
BROADCAST_NODETYPE = 1;
PEER_TO_PEER_NODETYPE = 2;
MIXED_NODETYPE = 4;
HYBRID_NODETYPE = 8;

NETBIOSTypes : array[0..8] of string[20] =
( 'UNKNOWN', 'BROADCAST', 'PEER_TO_PEER', '', 'MIXED', '', '', '', 'HYBRID'
);

//适配器类型
IF_OTHER_ADAPTERTYPE = 0;
IF_ETHERNET_ADAPTERTYPE = 1;
IF_TOKEN_RING_ADAPTERTYPE = 2;
IF_FDDI_ADAPTERTYPE = 3;
IF_PPP_ADAPTERTYPE = 4;
IF_LOOPBACK_ADAPTERTYPE = 5;
IF_SLIP_ADAPTERTYPE = 6;
//
AdaptTypes : array[0..6] of string[10] =
( 'other', 'ethernet', 'tokenring', 'FDDI', 'PPP', 'loopback', 'SLIP' );

//-----从其他MS头文件取得的信息---------------------------------------

MAX_INTERFACE_NAME_LEN = 256; { mrapi.h }
MAXLEN_PHYSADDR = 8; { iprtrmib.h }
MAXLEN_IFDESCR = 256; { --"--- }

//------------------------------------------------------------------------------

type
TMacAddress = array[1..MAX_ADAPTER_ADDRESS_LENGTH] of byte;

//------IP地址结构---------------------------------------------------

PTIP_ADDRESS_STRING = ^TIP_ADDRESS_STRING;
TIP_ADDRESS_STRING = array[0..15] of char; // IP as xxx.xxx.xxx.xxx string
//
PTIP_ADDR_STRING = ^TIP_ADDR_STRING;
TIP_ADDR_STRING = packed record // for use in linked lists
Next : PTIP_ADDR_STRING;
IpAddress : TIP_ADDRESS_STRING;
IpMask : TIP_ADDRESS_STRING;
Context : DWORD;
end;

//--------固定信息结构---------------------------------------------

PTFixedInfo = ^TFixedInfo;
TFixedInfo = packed record
HostName : array[0..MAX_HOSTNAME_LEN + 4] of Char;
DomainName : array[0..MAX_DOMAIN_NAME_LEN + 4] of Char;
CurrentDNSServer : PTIP_ADDR_STRING;
DNSServerList : TIP_ADDR_STRING;
NodeType : UINT;
ScopeID : array[0..MAX_SCOPE_ID_LEN + 4] of char;
EnableRouting : UINT;
EnableProxy : UINT;
EnableDNS : UINT;
end;

//--------界面结构------------------------------------------------


PTMibIfRow = ^TMibIfRow;
TMibIfRow = packed record
wszName : array[1..MAX_INTERFACE_NAME_LEN] of WCHAR;
dwIndex : DWORD;
dwType : DWORD;
dwMTU : DWORD;
dwSpeed : DWORD;
dwPhysAddrLen : DWORD;
bPhysAddr : array[1..MAXLEN_PHYSADDR] of byte;
dwAdminStatus : DWORD;
dwOperStatus : DWORD;
dwLastChange : DWORD;
dwInOctets : DWORD;
dwInUcastPkts : DWORD;
dwInNUCastPkts : DWORD;
dwInDiscards : DWORD;
dwInErrors : DWORD;
dwInUnknownProtos : DWORD;
dwOutOctets : DWORD;
dwOutUCastPkts : DWORD;
dwOutNUCastPkts : DWORD;
dwOutDiscards : DWORD;
dwOutErrors : DWORD;
dwOutQLen : DWORD;
dwDescrLen : DWORD;
bDescr: array[1..MAXLEN_IFDESCR] of char; //byte;
end;

TMIBIfArray = array of TMIBIFRow;


//
PTMibIfTable = ^TMIBIfTable;
TMibIfTable = packed record
dwNumEntries : DWORD;
Table : array[0..ANY_SIZE - 1] of TMibIfRow;
end;

//------适配器信息结构-------------------------------------------------

TTIME_T = array[1..325] of Byte; //MS time.h 无此结构

PTIP_ADAPTER_INFO = ^TIP_ADAPTER_INFO;
TIP_ADAPTER_INFO = packed record
Next : PTIP_ADAPTER_INFO;
ComboIndex : DWORD;
AdapterName : array[1..MAX_ADAPTER_NAME_LENGTH + 4] of char;
Description : array[1..MAX_ADAPTER_DESCRIPTION_LENGTH + 4] of char;
AddressLength : UINT;
Address : array[1..MAX_ADAPTER_ADDRESS_LENGTH] of byte;
Index : DWORD;
aType : UINT;
DHCPEnabled : UINT;
CurrentIPAddress : PTIP_ADDR_STRING;
IPAddressList : TIP_ADDR_STRING;
GatewayList : TIP_ADDR_STRING;
DHCPServer : TIP_ADDR_STRING;
HaveWINS : BOOL;
PrimaryWINSServer : TIP_ADDR_STRING;
SecondaryWINSServer: TIP_ADDR_STRING;
LeaseObtained : TTIME_T; //?
LeaseExpires : TTIME_T; //?
end;

//----------------TCP结构------------------------------------------------

PTMibTCPRow = ^TMibTCPRow;
TMibTCPRow = packed record
dwState : DWORD;
dwLocalAddr : DWORD;
dwLocalPort : DWORD;
dwRemoteAddr: DWORD;
dwRemotePort: DWORD;
end;
//
PTMibTCPTable = ^TMibTCPTable;
TMibTCPTable = packed record
dwNumEntries : DWORD;
Table : array[0..0] of TMibTCPRow;
end;
//
PTMibTCPStats = ^TMibTCPStats;
TMibTCPStats = packed record
dwRTOAlgorithm : DWORD;
dwRTOMin : DWORD;
dwRTOMax : DWORD;
dwMaxConn : DWORD;
dwActiveOpens : DWORD;
dwPassiveOpens : DWORD;
dwAttemptFails : DWORD;
dwEstabResets : DWORD;
dwCurrEstab : DWORD;
dwInSegs : DWORD;
dwOutSegs : DWORD;
dwRetransSegs : DWORD;
dwInErrs : DWORD;
dwOutRsts : DWORD;
dwNumConns : DWORD;
end;

//---------UDP结构------------------------------------------------------

PTMibUDPRow = ^TMibUDPRow;
TMibUDPRow = packed record
dwLocalAddr : DWORD;
dwLocalPort : DWORD;
end;
//
PTMibUDPTable = ^TMIBUDPTable;
TMIBUDPTable = packed record
dwNumEntries : DWORD;
UDPTable : array[0..ANY_SIZE - 1] of TMibUDPRow;
end;
//
PTMibUdpStats = ^TMIBUdpStats;
TMIBUdpStats = packed record
dwInDatagrams : DWORD;
dwNoPorts : DWORD;
dwInErrors : DWORD;
dwOutDatagrams : DWORD;
dwNumAddrs : DWORD;
end;

//-----------IP结构 ------------------------------------------------------

//
PTMibIPNetRow = ^TMibIPNetRow;
TMibIPNetRow = packed record
dwIndex : DWORD;
dwPhysAddrLen : DWORD;
bPhysAddr : TMACAddress;
dwAddr : DWORD;
dwType : DWORD;
end;
//
PTMibIPNetTable = ^TMibIPNetTable;
TMibIPNetTable = packed record
dwNumEntries : DWORD;
Table : array[0..ANY_SIZE - 1] of TMibIPNetRow;
end;
//
PTMibIPStats = ^TMibIPStats;
TMibIPStats = packed record
dwForwarding : DWORD;
dwDefaultTTL : DWORD;
dwInReceives : DWORD;
dwInHdrErrors : DWORD;
dwInAddrErrors : DWORD;
dwForwDatagrams : DWORD;
dwInUnknownProtos : DWORD;
dwInDiscards : DWORD;
dwInDelivers : DWORD;
dwOutRequests : DWORD;
dwRoutingDiscards : DWORD;
dwOutDiscards : DWORD;
dwOutNoRoutes : DWORD;
dwReasmTimeOut : DWORD;
dwReasmReqds : DWORD;
dwReasmOKs : DWORD;
dwReasmFails : DWORD;
dwFragOKs : DWORD;
dwFragFails : DWORD;
dwFragCreates : DWORD;
dwNumIf : DWORD;
dwNumAddr : DWORD;
dwNumRoutes : DWORD;
end;
//
PTMibIPAddrRow = ^TMibIPAddrRow;
TMibIPAddrRow = packed record
dwAddr : DWORD;
dwIndex : DWORD;
dwMask : DWORD;
dwBCastAddr: DWORD;
dwReasmSize: DWORD;
Unused1, Unused2: WORD;
end;

TMibIPAddrArray = array of TMIBIPAddrRow;


//
PTMibIPAddrTable = ^TMibIPAddrTable;
TMibIPAddrTable = packed record
dwNumEntries : DWORD;
Table : array[0..ANY_SIZE - 1] of TMibIPAddrRow;
end;

//
PTMibIPForwardRow = ^TMibIPForwardRow;
TMibIPForwardRow = packed record
dwForwardDest : DWORD;
dwForwardMask : DWORD;
dwForwardPolicy : DWORD;
dwForwardNextHop : DWORD;
dwForwardIFIndex : DWORD;
dwForwardType : DWORD;
dwForwardProto : DWORD;
dwForwardAge : DWORD;
dwForwardNextHopAS: DWORD;
dwForwardMetric1 : DWORD;
dwForwardMetric2 : DWORD;
dwForwardMetric3 : DWORD;
dwForwardMetric4 : DWORD;
dwForwardMetric5 : DWORD;
end;
//
PTMibIPForwardTable = ^TMibIPForwardTable;
TMibIPForwardTable = packed record
dwNumEntries : DWORD;
Table : array[0..ANY_SIZE - 1] of TMibIPForwardRow;
end;

//--------ICMP结构----------------------------------------------------

PTMibICMPStats = ^TMibICMPStats;
TMibICMPStats = packed record
dwMsgs : DWORD;
dwErrors : DWORD;
dwDestUnreachs : DWORD;
dwTimeEcxcds : DWORD;
dwParmProbs : DWORD;
dwSrcQuenchs : DWORD;
dwRedirects : DWORD;
dwEchos : DWORD;
dwEchoReps : DWORD;
dwTimeStamps : DWORD;
dwTimeStampReps: DWORD;
dwAddrMasks : DWORD;
dwAddrReps : DWORD;
end;

PTMibICMPInfo = ^TMibICMPInfo;
TMibICMPInfo = packed record
InStats : TMibICMPStats;
OutStats : TMibICMPStats;
end;

//------------------从IPHLPAPI.DLL输入的API函数-----------------------------------

function GetAdaptersInfo( pAdapterInfo: PTIP_ADAPTER_INFO;
pOutBufLen: PULONG ): DWORD;
stdcall; external 'IPHLPAPI.DLL';

function GetNetworkParams( FixedInfo: PTFixedInfo; pOutPutLen: PULONG ): DWORD;
stdcall; external 'IPHLPAPI.DLL';

function GetTcpTable( pTCPTable: PTMibTCPTable; pDWSize: PDWORD;
bOrder: BOOL ): DWORD;
stdcall; external 'IPHLPAPI.DLL';

function GetTcpStatistics( pStats: PTMibTCPStats ): DWORD;
stdcall; external 'IPHLPAPI.DLL';

function GetUdpTable( pUdpTable: PTMibUDPTable; pDWSize: PDWORD;
bOrder: BOOL ): DWORD;
stdcall; external 'IPHLPAPI.DLL';

function GetUdpStatistics( pStats: PTMibUdpStats ): DWORD;
stdcall; external 'IPHLPAPI.DLL';

function GetIpStatistics( pStats: PTMibIPStats ): DWORD;
stdcall; external 'IPHLPAPI.DLL';

function GetIpNetTable( pIpNetTable: PTMibIPNetTable;
pdwSize: PULONG; bOrder: BOOL ): DWORD;
stdcall; external 'IPHLPAPI.DLL';

function GetIpAddrTable( pIpAddrTable: PTMibIPAddrTable;
pdwSize: PULONG; bOrder: BOOL ): DWORD;
stdcall; external 'IPHLPAPI.DLL';

function GetIpForwardTable( pIPForwardTable: PTMibIPForwardTable;
pdwSize: PULONG; bOrder: BOOL ): DWORD;
stdCall; external 'IPHLPAPI.DLL';

function GetIcmpStatistics( pStats: PTMibICMPInfo ): DWORD;
stdCall; external 'IPHLPAPI.DLL';

function GetRTTAndHopCount( DestIPAddress: DWORD; HopCount: PULONG;
MaxHops: ULONG; RTT: PULONG ): BOOL;
stdCall; external 'IPHLPAPI.DLL';

function GetIfTable( pIfTable: PTMibIfTable; pdwSize: PULONG;
bOrder: boolean ): DWORD;
stdCall; external 'IPHLPAPI.DLL';

function GetIfEntry( pIfRow: PTMibIfRow ): DWORD;
stdCall; external 'IPHLPAPI.DLL';

implementation

end.
 
duducat:
麻烦发一份Demo,先谢了. vividwang2003@yahoo.com.cn
 
dream40@etang.com
 
在interface
uses 加入winsock单元.
procedure Tform.button1Click(Sender: TObject);
type
TaPInAddr = Array[0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe: PHostEnt;
pptr: PaPInAddr;
Buffer: Array[0..63] of Char;
I: Integer;
GInitData: TWSAData;
IP: String;
begin
Screen.Cursor := crHourGlass;
try
WSAStartup($101, GInitData);
IP:='0.0.0.0';
GetHostName(Buffer, SizeOf(Buffer));
phe := GetHostByName(buffer);
if phe = nil then
begin
ShowMessage(IP);
Exit;
end;
pPtr := PaPInAddr(phe^.h_addr_list);
I := 0;
while pPtr^ <> nil do
begin
IP := inet_ntoa(pptr^^);
Inc(I);
end;
WSACleanup;
ShowMessage(IP);//如果上网则为上网ip否则是网卡ip
finally
Screen.Cursor := crDefault;
end;
end;
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部