我需要德尔福2010年工作函数来检查是否有可用的互联网连接。
我说工作,因为到目前为止,我尝试4种不同的方法,例如http://delphi.about.com/b/2005/04/22/how-to-check-for-internet-connection-using-delphi-code.htm但既不工作。
例如,一个方法送花儿给人回吐,有互联网连接,即使电缆是不是在PC,其他相反的(它总是说没有连接)。
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”回归互联网的状态和类型的值。见下文:
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;
唯一可靠的方法是尝试连接到一个真正的服务器在互联网上的某个地方,看看它是否成功或失败。不要使用依赖于OS的状态信息OS功能,因为这些数据可以很容易地获得同步。
您可以使用TIdHTTP
组件:
function TMainF.isInternetConnection: Boolean;
begin
try
IdHTTP.Get('http://www.svtech.cz');
except
on E: Exception do begin
if not (E is EIdHTTPProtocolException) then begin
Result := False;
Exit;
end;
end;
end;
Result := True;
end;