怎么取得本机的拨号的互联网IP啊(ADSL拨号的)?(50分)

  • 主题发起人 主题发起人 SeekMyself
  • 开始时间 开始时间
S

SeekMyself

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么取得本机的拨号的互联网IP啊(ADSL拨号的)?
 
没有人知道吗???
 
拿拨号上网来说
每次ISP分配给的IP是不一样的
所以只能是在拨号连接成功后动态地取IP地址
你可以用RASGetProjectionInfoA可以获取有关的信息
程序如下
仅供参考
procedure TRASDialerForm.IPbuttonclick(sender:tobject);
var
ipaddr:string;
begin
ipaddr:=rasgetipaddress;
ipaddr> '' then
infolistbox.items.add('ip'+ipaddr);
else
infolistbox.items.add('ip unknown');
end;
function RASGetIPaddress:string;
var
rasconns:trasconn;
swsize:dword;
dwcount:dword;
raspppip:traspppip;
begin
result:='';
rasconns.dwsize:=sizeof(trasconn);
raspppip.dwsize:=sizeof(raspppip);
dwsize:=sizeof(rasconns);
if rasenumconnectionsa(@rasconns,@dwsize,@dwcount)=0 then
begin
dwsize:=sizeof(raspppip);
raspppip.dwsize:=sizeof(rasconns.hrasconn,rasp_pppip,@raspppip,@dwsize)=0 then
result:=strpas(raspppip.szipaddress);
end;
end;
end;
不知能不能帮上你
 
InternetGetConnectedState

function IsConnectedNet(var asIP: string): Boolean; //网线拔掉稍候可以检测到
var
WSData: TWSAData;
Buffer:array[0..63]of Char;
HostEnt:PHostEnt;
PPInAddr:^PInAddr;
IPString:String;
begin
Result := False;
IPString := '';
WSAStartUp($101,WSData);
try
GetHostName(Buffer,SizeOf(Buffer));
HostEnt:=GetHostByName(Buffer);
if Assigned(HostEnt) then
begin
PPInAddr:=@(PInAddr(HostEnt.H_Addr_List^));
while Assigned(PPInAddr^) do
begin
IPString := StrPas(INet_NToA(PPInAddr^^));
asIP := IPString;
Result := IPString <> '127.0.0.1';
Inc(PPInAddr);
end;
end;
finally
try WSACleanUp except end;
end;
end;
 
GipsyCN
可以遍历所有的IP,但怎么知道哪个是 WWW 的IP呢?
 
感谢 舞鹤,

你的程序正在测试! :)
 
InternetGetConnectedState
function TwndData.InternetConnected: Boolean; //IP读取、共享
VAR
Flags : DWORD;
BEGIN
Flags :=7;
Result := InternetGetConnectedState(@Flags,0);
{if Result then
begin
if (flags and INTERNET_CONNECTION_MODEM) = INTERNET_CONNECTION_MODEM then
begin
showmessage('Modem');
end;
if (flags and INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN then
begin
showmessage('LAN');
end;
if (flags and INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY then
begin
showmessage('Proxy');
end;
if (flags and INTERNET_CONNECTION_MODEM_BUSY)=INTERNET_CONNECTION_MODEM_BUSY then
begin
showmessage('Modem Busy');
end;
end;}
end;

我先用ADSL拨号上网,然后断开,
用以上程序测试显示,是错误的结果,-> 始终是返回True,即在线!!!
 
上网得到internet的IP,没有上得到本机IP

function MYIP: 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;
begin
WSAStartup($101, GInitData);
Result := '';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^ <> nil do begin
result:=StrPas(inet_ntoa(pptr^^));
Inc(I);
end;
WSACleanup;
end;
 
去掉几个保留地址段
127开头为测试地址
192开头一般局域网
10开头为局域网
如果你的网管很变态那没办法了,只能一个个设定
比如可以确定每个地址的地域 211.xxx.x.x -北京 》》那应该是外部地址,不过不保险
最保险的方法是tracert?
 
舞鹤 : trasconn 是什么单元的??
 
-_-; : 你取的是最后一个的地址,测试的时候是对的!
但所有的都是这样吗?有什么依据?

heipi2002 的也不为一法 :》
 
能不能调用 ipconfig 命令
 
既然这个是最后一个IP,那么你有2种情况
1,OnLine 2,OffLine
当你下线以后 你就是本地IP, 我的函数获得的就是这个, 不可能得到上次上网的IP,因为你已经下线了,函数得到的是这个IP,也就是说,你的电脑不可能同时 在线和离线, 所以这个函数是绝对OK滴~``
 
-_-; 说的不错!!

vividw, 怎么y用delphi调用IPConfig ??
 
引用单元 Shellapi 的 ShellExecute
顺便查了sdk 附上
HINSTANCE ShellExecute(

HWND hwnd, // handle to parent window
LPCTSTR lpOperation, // pointer to string that specifies operation to perform
LPCTSTR lpFile, // pointer to filename or folder name string
LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters
LPCTSTR lpDirectory, // pointer to string that specifies default directory
INT nShowCmd // whether file is shown when opened
);


Parameters

hwnd

Specifies a parent window. This window receives any message boxes that an application produces. For example, an application may report an error by producing a message box.

lpOperation

Pointer to a null-terminated string that specifies the operation to perform. The following operation strings are valid:

String Meaning
"open" The function opens the file specified by lpFile. The file can be an executable file or a document file. The file can be a folder to open.
"print" The function prints the file specified by lpFile. The file should be a document file. If the file is an executable file, the function opens the file, as if "open" had been specified.
"explore" The function explores the folder specified by lpFile.


The lpOperation parameter can be NULL. In that case, the function opens the file specified by lpFile.

lpFile

Pointer to a null-terminated string that specifies the file to open or print or the folder to open or explore. The function can open an executable file or a document file. The function can print a document file.

lpParameters

If lpFile specifies an executable file, lpParameters is a pointer to a null-terminated string that specifies parameters to be passed to the application.
If lpFile specifies a document file, lpParameters should be NULL.

lpDirectory

Pointer to a null-terminated string that specifies the default directory.

nShowCmd

If lpFile specifies an executable file, nShowCmd specifies how the application is to be shown when it is opened. This parameter can be one of the following values:

Value Meaning
SW_HIDE Hides the window and activates another window.
SW_MAXIMIZE Maximizes the specified window.
SW_MINIMIZE Minimizes the specified window and activates the next top-level window in the Z order.
SW_RESTORE Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.
SW_SHOW Activates the window and displays it in its current size and position.
SW_SHOWDEFAULT Sets the show state based on the SW_ flag specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. An application should call ShowWindow with this flag to set the initial show state of its main window.
SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window.
SW_SHOWMINIMIZED Activates the window and displays it as a minimized window.
SW_SHOWMINNOACTIVE Displays the window as a minimized window. The active window remains active.
SW_SHOWNA Displays the window in its current state. The active window remains active.
SW_SHOWNOACTIVATE Displays a window in its most recent size and position. The active window remains active.
SW_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.


If lpFile specifies a document file, nShowCmd should be zero.



Return Values

If the function succeeds, the return value is the instance handle of the application that was run, or the handle of a dynamic data exchange (DDE) server application.
If the function fails, the return value is an error value that is less than or equal to 32. The following table lists these error values:

Value Meaning
0 The operating system is out of memory or resources.
ERROR_FILE_NOT_FOUND The specified file was not found.
ERROR_PATH_NOT_FOUND The specified path was not found.
ERROR_BAD_FORMAT The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).
SE_ERR_ACCESSDENIED The operating system denied access to the specified file.
SE_ERR_ASSOCINCOMPLETE The filename association is incomplete or invalid.
SE_ERR_DDEBUSY The DDE transaction could not be completed because other DDE transactions were being processed.
SE_ERR_DDEFAIL The DDE transaction failed.
SE_ERR_DDETIMEOUT The DDE transaction could not be completed because the request timed out.
SE_ERR_DLLNOTFOUND The specified dynamic-link library was not found.
SE_ERR_FNF The specified file was not found.
SE_ERR_NOASSOC There is no application associated with the given filename extension.
SE_ERR_OOM There was not enough memory to complete the operation.
SE_ERR_PNF The specified path was not found.
SE_ERR_SHARE A sharing violation occurred.


Remarks

The file specified by the lpFile parameter can be a document file or an executable file. If the file is a document file, the ShellExecute function opens or prints it, depending on the value of the lpOperation parameter. If the file is an executable file, the ShellExecute function opens it, even if lpOperation specifies printing.
You can use ShellExecute to open or explore a shell folder. To open a folder, use either of the following calls:

ShellExecute(handle, NULL, path_to_folder, NULL, NULL, SW_SHOWNORMAL);

or


ShellExecute(handle, "open", path_to_folder, NULL, NULL, SW_SHOWNORMAL);


To explore a folder, use the following call:

ShellExecute(handle, "explore", path_to_folder, NULL, NULL, SW_SHOWNORMAL);


If lpOperation is NULL, the function opens the file specified by lpFile. If lpOperation is "open" or "explore", the function will force an open window or explorer.
 
这只是执行,但怎么返回 当前的www 上的 IP 给程序啊?
 
分数太少不想深入,可以参看IP Helper API
下载 ftp://delphi-jedi.org/api/IPHlpAPI.zip 可以看到例程和代码
资料可参看 http://www.delphizine.com/features/2001/06/di200106jp_f/di200106jp_f.asp
 
heipi2002, 的网站没有CustomerID ,进不去 :)

我测试了上面的方法,但又有个问题,如果是局域网共享上网,那么客户机取的是本机的地址,但是还可以上网,这种情况下,我怎么取服务器上网的IP地址呢?
 
多人接受答案了。
 
后退
顶部