全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:3504
推到 Plurk!
推到 Facebook!

請問取得所有 IP ? (Delphi 2009)

缺席
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-05-05 00:21:09 IP:59.104.xxx.xxx 訂閱
請問一下
如何把這個取得 IP 程式由 Delphi 7 改為 Delphi 2009 用 ?
謝謝 ^_^


[code delphi]
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
Function GetLocalIp(InternetIP:boolean):shortstring;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Clear;
Memo1.Lines.Add(GetLocalIP(true));
Memo1.Lines.Add(GetLocalIP(false));
end;

Function Tform1.GetLocalIp(InternetIP:boolean):shortstring;
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: shortstring;
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^[I] <> nil do
begin
IP := inet_ntoa(pptr^[I]^);
Inc(I);
end;
end
else
IP := inet_ntoa(pptr^[0]^);
WSACleanup;
Result:=IP;//如果上網則為上網ip否則是網卡ip
finally
Screen.Cursor := crDefault;
end;
end;

end.

[/code]
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
編輯記錄
pcboy 重新編輯於 2009-05-05 14:54:11, 註解 無‧
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-05-05 11:19:52 IP:61.220.xxx.xxx 訂閱
// 這個 Delphi 7, 2009 都可用
// 把 Memo 和 IdIPWatch 元件放 Form 上 ( InIPWatch 在 Indy Misc 分類)

[code delphi]
Memo1.Lines.Add(IdIPWatch1.LocalIP); // 顯示本機 IP (但是不是所有IP)
[/code]

------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
編輯記錄
pcboy 重新編輯於 2009-05-05 11:20:18, 註解 無‧
HikaruGo
中階會員


發表:22
回覆:69
積分:88
註冊:2007-12-09

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-05-05 12:21:07 IP:61.62.xxx.xxx 訂閱
我覺得API 跟 Version 無關係

while pPtr^[I] <> nil do
begin
IP := inet_ntoa(pptr^[I]^);
Inc(I);
end;



FIX:

while pPtr^[I] <> nil do
begin
IP := IP #13#10 inet_ntoa(pptr^[I]^);
Inc(I);
end;



===================引 用 pcboy 文 章===================
請問一下
如何把這個取得 IP 程式由 Delphi 7 改為 Delphi 2009 用 ?
謝謝 ^_^

編輯記錄
pcboy 重新編輯於 2009-05-05 12:44:34, 註解 無‧
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2009-05-05 12:28:13 IP:61.220.xxx.xxx 訂閱

終於改出一個 Delphi 7, Delphi 2009 都可以用的了


[code delphi]
// Delphi 7, Delphi 2009
//
// GetLoadlIP : 顯示本機器所有 IP (測試正常)
// Get_HostName : 顯示本機器 hostname (測試正常)
//
/// 請放 Memo, Button 元件, uses 加上 WinSock

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, WinSock, StdCtrls;
// 要加上 WinSock

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
{$R *.dfm}
function Get_HostName : string;
//
// Return computer IP if you are connected in a network
// Declare Winsock in the uses clause
//
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
Buffer : array [0..63] of char;
GInitData : TWSADATA;
begin
WSAStartup($101, GInitData);
Result := '';
GetHostName(@Buffer, SizeOf(Buffer));
phe :=GetHostByName(@buffer);
result := String(phe^.h_Name);
WSACleanup;
end;

function GetAllLocalIP : string;
//
// Return computer IP if you are connected in a network
// Declare Winsock in the uses clause
//
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;
begin
WSAStartup($101, GInitData);
Result := '';
GetHostName(@Buffer, SizeOf(Buffer));
phe :=GetHostByName(@buffer);
if phe = nil then
begin
Exit;
end;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do
begin
if i=0 then
result:= String(StrPas(inet_ntoa(pptr^[I]^)))
else
result:= result ' , ' String(StrPas(inet_ntoa(pptr^[I]^)));
Inc(I);
end;
WSACleanup;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Clear;
Memo1.Lines.Add(Get_HostName);
Memo1.Lines.Add(GetAllLocalIP);
end;

end.
[/code]
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#5 引用回覆 回覆 發表時間:2009-05-05 12:42:37 IP:61.220.xxx.xxx 訂閱
感謝您的關心 ^_^ 
小弟在 Delphi 2009 上 compile 無法過才貼出來問的
剛剛照您只修改這一行, 測試發現 compile 是過不了的

===================引 用 HikaruGo 文 章===================
我覺得API 跟 Version 無關係

while pPtr^[I] <> nil do
begin
IP := inet_ntoa(pptr^[I]^);
Inc(I);
end;



FIX:

while pPtr^[I] <> nil do
begin
IP := IP #13#10 inet_ntoa(pptr^[I]^);
Inc(I);
end;

------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
編輯記錄
pcboy 重新編輯於 2009-05-05 12:43:47, 註解 無‧
HikaruGo
中階會員


發表:22
回覆:69
積分:88
註冊:2007-12-09

發送簡訊給我
#6 引用回覆 回覆 發表時間:2009-05-05 12:50:48 IP:61.62.xxx.xxx 訂閱
查一下Help inet_ntoa returns a char pointer 應該是轉型問題,
Delphi 2009 我沒用過,只用過Delphi 6
比較 Win32 API 與 .NET, 感覺.NET 順多了
Win32 Version ?

[code c#]
public static void ShowNetworkInterfaces()
{
IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
Console.WriteLine("Interface information for {0}.{1} ",
computerProperties.HostName, computerProperties.DomainName);
if (nics == null || nics.Length < 1)
{
Console.WriteLine(" No network interfaces found.");
return;
}
Console.WriteLine(" Number of interfaces .................... : {0}", nics.Length);
foreach (NetworkInterface adapter in nics)
{
IPInterfaceProperties properties = adapter.GetIPProperties();
Console.WriteLine();
Console.WriteLine(adapter.Description);
Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'='));
Console.WriteLine(" Interface type .......................... : {0}", adapter.NetworkInterfaceType);
Console.WriteLine(" Physical Address ........................ : {0}",
adapter.GetPhysicalAddress().ToString());
Console.WriteLine(" Operational status ...................... : {0}",
adapter.OperationalStatus);
string versions ="";
// Create a display string for the supported IP versions.
if (adapter.Supports(NetworkInterfaceComponent.IPv4))
{
versions = "IPv4";
}
if (adapter.Supports(NetworkInterfaceComponent.IPv6))
{
if (versions.Length > 0)
{
versions = " ";
}
versions = "IPv6";
}
Console.WriteLine(" IP version .............................. : {0}", versions);
ShowIPAddresses(properties);
// The following information is not useful for loopback adapters.
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)
{
continue;
}
Console.WriteLine(" DNS suffix .............................. : {0}",
properties.DnsSuffix);
string label;
if (adapter.Supports(NetworkInterfaceComponent.IPv4))
{
IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();
Console.WriteLine(" MTU...................................... : {0}", ipv4.Mtu);
if (ipv4.UsesWins)
{
IPAddressCollection winsServers = properties.WinsServersAddresses;
if (winsServers.Count > 0)
{
label = " WINS Servers ............................ :";
ShowIPAddresses(label, winsServers);
}
}
}
Console.WriteLine(" DNS enabled ............................. : {0}",
properties.IsDnsEnabled);
Console.WriteLine(" Dynamically configured DNS .............. : {0}",
properties.IsDynamicDnsEnabled);
Console.WriteLine(" Receive Only ............................ : {0}",
adapter.IsReceiveOnly);
Console.WriteLine(" Multicast ............................... : {0}",
adapter.SupportsMulticast);
ShowInterfaceStatistics(adapter);
Console.WriteLine();
}

[/code]
編輯記錄
HikaruGo 重新編輯於 2009-05-05 12:52:17, 註解 無‧
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#7 引用回覆 回覆 發表時間:2009-05-05 14:53:24 IP:61.220.xxx.xxx 訂閱
又找到一種方法


[code delphi]
// Delphi 7 ( Delphi 2009 測試失敗 )
// Windows XP
//
// 取得所有 IP 位址
// uses 要有 IdStack

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
IdStack; // 要有 IdStack

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 GetAllIP:String;
var
IdStack: TIdStack;
begin
IdStack := TIdStack.CreateStack; // Delphi 7, 可以
//IdStack := TIdStack.Create; // Delphi 2009, 失敗, 不知為何
try
Result := IdStack.LocalAddresses.Text;
finally
IdStack.Free;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines.Add(GetAllIP);
end;
end.

[/code]


------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
系統時間:2024-04-20 22:20:11
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!