天哪,我居然连怎么获得网络IP都不知道?谁能给我个通过MODOM上网的得到IP的方法,要运行的出的(35分)

  • 主题发起人 主题发起人 xhyph
  • 开始时间 开始时间
X

xhyph

Unregistered / Unconfirmed
GUEST, unregistred user!
谁能给我个通过MODOM上网的得到IP的方法,要运行的出的,
我在网上看见过许多例子,但是都不对,多出不来
请给我个可以运行的例子
 
留下邮件我给你一个例子!
 
还是贴给你吧!

unit GetDnsList;

interface

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

type
TIPMainForm = class(TForm)
Info: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Procedure GetNetworkParameters;
Procedure GetAdapterInformation;
end;

var
IPMainForm: TIPMainForm;
aDnsList:TStrings;
implementation

{$R *.DFM}

Const
MAX_HOSTNAME_LEN = 128; { from IPTYPES.H }
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;

Function GetNetworkParams(FI : PFixedInfo; Var BufLen : Integer) : Integer;
StdCall; External 'iphlpapi.dll' Name 'GetNetworkParams';

Function GetAdaptersInfo(AI : PIPAdapterInfo; Var BufLen : Integer) : Integer;
StdCall; External 'iphlpapi.dll' Name 'GetAdaptersInfo';

procedure TIPMainForm.GetAdapterInformation;
Var
AI,Work : PIPAdapterInfo;
Size : Integer;
Res : Integer;
I : Integer;

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;

Function TimeTToDateTimeStr(TimeT : Integer) : String;
Const UnixDateDelta = 25569; { days between 12/31/1899 and 1/1/1970 }
Var
DT : TDateTime;
TZ : TTimeZoneInformation;
Res : DWord;

Begin
If (TimeT = 0) Then Result := ''
Else Begin
{ Unix TIME_T is secs since 1/1/1970 }
DT := UnixDateDelta+(TimeT / (24*60*60)); { in UTC }
{ calculate bias }
Res := GetTimeZoneInformation(TZ);
If (Res = TIME_ZONE_ID_INVALID) Then RaiseLastWin32Error;
If (Res = TIME_ZONE_ID_STANDARD) Then Begin
DT := DT-((TZ.Bias+TZ.StandardBias) / (24*60));
Result := DateTimeToStr(DT)+' '+WideCharToString(TZ.StandardName);
End
Else Begin { daylight saving time }
DT := DT-((TZ.Bias+TZ.DaylightBias) / (24*60));
Result := DateTimeToStr(DT)+' '+WideCharToString(TZ.DaylightName);
End;
End;
End;

begin
Size := 5120;
GetMem(AI,Size);
Res := GetAdaptersInfo(AI,Size);
If (Res <> ERROR_SUCCESS) Then Begin
SetLastError(Res);
RaiseLastWin32Error;
End;
With Info,Lines do Begin
Work := AI;
I := 1;
Repeat
Add('');
Add('Adapter '+IntToStr(I));
Add(' ComboIndex: '+IntToStr(Work^.ComboIndex));
Add(' Adapter name: '+Work^.AdapterName);
Add(' Description: '+Work^.Description);
Add(' Adapter address: '+MACToStr(@Work^.Address,Work^.AddressLength));
Add(' Index: '+IntToStr(Work^.Index));
Add(' Type: '+IntToStr(Work^._Type));
Add(' DHCP: '+IntToStr(Work^.DHCPEnabled));
Add(' Current IP: '+GetAddrString(Work^.CurrentIPAddress));
Add(' IP addresses: '+GetAddrString(@Work^.IPAddressList));
Add(' Gateways: '+GetAddrString(@Work^.GatewayList));
Add(' DHCP servers: '+GetAddrString(@Work^.DHCPServer));
Add(' Has WINS: '+IntToStr(Integer(Work^.HaveWINS)));
Add(' Primary WINS: '+GetAddrString(@Work^.PrimaryWINSServer));
Add(' Secondary WINS: '+GetAddrString(@Work^.SecondaryWINSServer));
Add(' Lease obtained: '+TimeTToDateTimeStr(Work^.LeaseObtained));
Add(' Lease expires: '+TimeTToDateTimeStr(Work^.LeaseExpires));
Inc(I);
Work := Work^.Next;
Until (Work = nil);
End;
FreeMem(AI);
end;

procedure TIPMainForm.GetNetworkParameters;
Var
FI : PFixedInfo;
Size : Integer;
Res : Integer;
I : Integer;
DNS : PIPAddrString;

begin
Size := 1024;
GetMem(FI,Size);
Res := GetNetworkParams(FI,Size);
If (Res <> ERROR_SUCCESS) Then Begin
SetLastError(Res);
RaiseLastWin32Error;
End;
With Info do Begin
Clear;
Lines.Add('Host name: '+FI^.HostName);
Lines.Add('Domain name: '+FI^.DomainName);
aDnsList.Clear;
If (FI^.CurrentDNSServer <> nil) Then
begin
Lines.Add('Current DNS Server: '+FI^.CurrentDNSServer^.IPAddress);
end
Else Lines.Add('Current DNS Server: (none)');
I := 1;
DNS := @FI^.DNSServerList;
Repeat
Lines.Add('DNS '+IntToStr(I)+': '+DNS^.IPAddress);
aDnsList.Add(DNS^.IPAddress);
Inc(I);
DNS := DNS^.Next;
Until (DNS = nil);
Lines.Add('Scope ID: '+FI^.ScopeId);
Lines.Add('Routing: '+IntToStr(FI^.EnableRouting));
Lines.Add('Proxy: '+IntToStr(FI^.EnableProxy));
Lines.Add('DNS: '+IntToStr(FI^.EnableDNS));
End;
FreeMem(FI);
end;

procedure TIPMainForm.Button1Click(Sender: TObject);
begin
GetNetworkParameters;
GetAdapterInformation;

end;

procedure TIPMainForm.FormCreate(Sender: TObject);
begin
aDnsList:=TStringList.Create;
end;

end.
 
如果不是编程的话,在Windows下你可以用winipcfg这个程序获取你的IP地址
 
怎么那么复杂啊?
我知道VC的,但是不知道怎么转换为Delphi
char name[255];
in_addr addr[2];
gethostname(name,sizeof(name));
this->m_name=name;
hostent *hostinfo;
if((hostinfo = gethostbyname(name)) != NULL)
{

for (int i=0; hostinfo->h_addr_list!=0; i++)
{
memcpy(&addr,hostinfo->h_addr_list,
sizeof(in_addr));
}

}
m_IP=inet_ntoa(addr[0]);
m_IPnet=inet_ntoa(addr[1]);

 
如果仅仅是IP不复杂,俺的程序可以提示您几乎所并的网络信息
包括您动态获得的DNS服务器等等!
一次性解决,省的以后还要问!
 
如果你的机器有多个IP地址时用 GetLocateIp(TRUE)即可获得上网IP地址。
uses winsock;

//获取本机IP地址
Function GetLocateIp(InternetIp:Boolean):String;
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);
Result:=IP;
Exit;
end;
pPtr := PaPInAddr(phe^.h_addr_list);
if InternetIp then
begin
I := 0;
while pPtr^ <> nil do
begin
IP := inet_ntoa(pptr^^);
Inc(I);
end;
end
else
IP:=StrPas(inet_ntoa(pptr^[0]^));
WSACleanup;
Result:=IP;//如果上网则为上网ip否则是网卡ip
finally
Screen.Cursor := crDefault;
end;
end;

 
[blue][/blue]全是废话!没有 一个敢说自己是老大的,你们为什么不去看看我的问题,我问了一年了!没有
人回答真让我感觉到这里的技术是不是已经过时了,或者是这是这里的人已经不
是做编程的!
netstat -n -p tcp
ipconfig 加个参数就得了!
http://www.delphibbs.com/delphibbs/dispq.asp?lid=719446
http://www.delphibbs.com/delphibbs/dispq.asp?lid=666070
http://www.delphibbs.com/delphibbs/dispq.asp?lid=716037
http://www.delphibbs.com/delphibbs/dispq.asp?lid=730641
http://www.delphibbs.com/delphibbs/dispq.asp?lid=655233
如果能有人回答这些问题才是真的高手呀!
 
多人接受答案了。
 
后退
顶部