问题描述
我需要Delphi 2010的工作功能来检查是否有Internet连接可用。
I need a working function for Delphi 2010 to check if there is Internet connection available.
我之所以说工作是因为到目前为止,我尝试了4种不同的方法,例如,但都没有。
I say working because so far I tried 4 different methods e.g. http://delphi.about.com/b/2005/04/22/how-to-check-for-internet-connection-using-delphi-code.htm but neither worked.
例如,一种方法始终返回Internet连接,即使电缆不在计算机中也是如此,而另一种方法则相反(它总是说没有连接)。
For example one method alway gave back that there was internet connection even when the cable was not in the pc, the other the opposite (it always said there was no connection).
procedure TForm1.Button1Click(Sender: TObject) ;
function FuncAvail(_dllname, _funcname: string;
var _p: pointer): boolean;
{return True if _funcname exists in _dllname}
var _lib: tHandle;
begin
Result := false;
if LoadLibrary(PChar(_dllname)) = 0 then exit;
_lib := GetModuleHandle(PChar(_dllname)) ;
if _lib <> 0 then begin
_p := GetProcAddress(_lib, PChar(_funcname)) ;
if _p <> NIL then Result := true;
end;
end;
{
Call SHELL32.DLL for Win < Win98
otherwise call URL.dll
}
{button code:}
var
InetIsOffline : function(dwFlags: DWORD):
BOOL; stdcall;
begin
if FuncAvail('URL.DLL', 'InetIsOffline',
@InetIsOffline) then
if InetIsOffLine(0) = true
then ShowMessage('Not connected')
else ShowMessage('Connected!') ;
end;
推荐答案
在使用中添加单位 WinNet。使用功能 InternetGetConnectedState返回互联网状态和类型的值。见下文:
Add in your uses the unit "WinNet". With the function "InternetGetConnectedState" return a value for internet state and type. See below:
function YourFunctionName : boolean;
var
origin : cardinal;
begin
result := InternetGetConnectedState(@origin,0);
//connections origins by origin value
//NO INTERNET CONNECTION = 0;
//INTERNET_CONNECTION_MODEM = 1;
//INTERNET_CONNECTION_LAN = 2;
//INTERNET_CONNECTION_PROXY = 4;
//INTERNET_CONNECTION_MODEM_BUSY = 8;
end;
这篇关于delphi检查互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!