使用  系统的 url.dll 它存在于 Win98 or comes with Internet Explorer 4 or later, Office 97 etc. function InetIsOffline(Flag: Integer): Boolean; stdcall; external 'URL.DLL'; 
if InetIsOffline(0) then 
   ShowMessage('This computer is not connected to Internet!') 
else 
   ShowMessage(You are connected to Internet!'); 

解决方案 »

  1.   

    您好!
        本人初涉delphi,请多多指教。
        请问这个如何使用?
        function InetIsOffline(Flag: Integer): Boolean; stdcall; external 'URL.DLL';
      

  2.   

    放在 implementation 语句的上方。
      

  3.   

    您好! 
       可是这样不能准确地判断是否连接internet
    还需要进行其它什么设置吗?救急啊!
     
      

  4.   

    测试连接是否有效,可以用:InternetCheckConnection检测计算机是否联网比较简单的做法可以通过一个 Win32 Internet(WinInet) 函数 InternetCheckConnection来实现;
    这个函数的功能是检查是否能够建立 Internet 连接。
    它的实现是在 %SystemRoot%\System32\wininet.dll 中,Delphi 调用声明在 WinInet.pas 中,
    其 API 声明如下:BOOL InternetCheckConnection(
        IN LPCSTR lpszUrl,
        IN DWORD dwFlags,
        IN DWORD dwReserved
    );参数的意义是:lpszUrl: 需要测试能否建立连接的 URL 地址,可以为空;
    dwFlags: 目前只能是 FLAG_ICC_FORCE_CONNECTION(这个常量 Delphi 中没有声明,其值为 $00000001);
    dwReserved: 目前只能为 0。调用的说明:如果 lpszUrl 是非空的,Windows 从中解析出 Host 名然后 Ping 这个指定的 Host。
    如果 lpszUrl 是空的,并且 WinInet 内部服务器的 database 中有一个关于最近的 Server 的纪录,Windows 就从这条纪录中解析出 Host 名然后 Ping 它。如果能够成功的连接返回 True,否则返回 False;以下是一个判断当前计算机是否联网的例子:procedure TForm1.Button1Click(Sender: TObject);
    begin
        if InternetCheckConnection('http://www.yahoo.com/', 1, 0) then
            edit1.text:= 'Connected'
        else
            edit1.text:= 'Disconnected';
    end;通过上述的方法只能检测出当前计算机是否物理联网,即网线是否接好,网卡是否能顺利工作,不能确定是否能够实现获得 Internet 服务,即是否能和 ISP 进行 Internet 连接。
    这时可以通过另一个 Win32 Internet(WinInet) 函数 InternetQueryOption 来检测;
    这个函数的功能是查询指定 Internet 句柄的状态、选项。
    其 API 声明如下:BOOL InternetQueryOption(
        IN HINTERNET hInternet,
        IN DWORD dwOption,
        OUT LPVOID lpBuffer,
        IN OUT LPDWORD lpdwBufferLength
    );参数的意义是:hInternet:查询对象的 Internet 句柄(全局查询时为 nil),
    dwOption:查询的项目;
    lpBuffer:返回的查询结果;
    lpdwBufferLength:查询结果的字节长度(包括 IN 和 OUT);查询成功返回 True,否则返回 False;我们要查询当前计算机的 Internet 连接状态时可以使用查询项目 INTERNET_OPTION_CONNECTED_STATE,得到的 ConnectState 返回值可能是以下值的一个或几个值之和:INTERNET_STATE_CONNECTED               :$00000001 连接状态;
    INTERNET_STATE_DISCONNECTED            :$00000002 非连接状态(和 INTERNET_STATE_CONNECTED 对应);
    INTERNET_STATE_DISCONNECTED_BY_USER    :$00000010 用户请求的非连接状态
    INTERNET_STATE_IDLE                    :$00000100 连接状态,并且空闲
    INTERNET_STATE_BUSY                    :$00000200 连接状态,正在响应连接请求以下是一个判断当前计算机是否可以获得 Internet 服务的例子:function TForm1.CheckOffline: boolean;
    var
        ConnectState: DWORD;
        StateSize: DWORD;
    begin
        ConnectState:= 0;
        StateSize:= SizeOf(ConnectState);
        result:= false;
        if InternetQueryOption(nil, INTERNET_OPTION_CONNECTED_STATE, @ConnectState, StateSize) then
            if (ConnectState and INTERNET_STATE_DISCONNECTED) <> 2 then result:= true;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
        if CheckOffline then
            edit1.text:= 'Connect To ISP'
        else
            edit1.text:= 'Disconnect To ISP';
    end;需要说明的是 InternetQueryOption 函数的检测结果只能表明当前的 Internet 设置是可用的,
    并不能表示计算机一定能访问 Internet,例如网线掉了,网卡突然坏了之类的错误就没法检测出来,
    要想检测当前计算机是否能够获得 Internet 服务了必须两个函数结合起来使用。以上程序在 Win2000, Delphi5.0 下调试通过。最后要提醒大家注意的是在 uses 中要加上 WinInet。
      

  5.   

    Kingron(WinAPI)兄你太夸张了吧!说那么长.呵呵....
      

  6.   

    呵呵,转贴的资料!要是真的要我来敲这么多.........//faint
      

  7.   

    似乎  InetIsOffline(0) 永远返回False
      

  8.   

    用WINAPI函数:internetgetconnectstatus(*,*),具体参数记不清了,自己查msdn好了
      

  9.   

    哦,想起来了:
    VAR  
      dwConnectionTypes : DWORD;
    BEGIN
      dwConnectionTypes :=
       INTERNET_CONNECTION_MODEM +
    INTERNET_CONNECTION_LAN +
    INTERNET_CONNECTION_PROXY;
      if InternetGetConnectedState(@dwConnectionTypes,0) then showmessage('yes');
    END;
    给分吧!
      

  10.   

    来自:Another_eYes 时间:99-8-29 21:55:08 ID:129673 
    用个ICMP控件直接在程序里ping吧 
    不用控件也简单. 用api一样 
    uses winsock; p_ip_options=^t_ip_options; 
    t_ip_options=packed record 
    Ttl: byte; 
    Tos: byte; 
    flags: byte; 
    optionssize: byte; 
    optionsdata: pointer; 
    end; p_icmp_echo_reply=^t_icmp_echo_reply; 
    t_icmp_echo_reply=packed record 
    address: u_long; 
    status: u_long; 
    rttime: u_long; 
    datasize: word; 
    reserved: word; 
    data: pointer; ip_options:t_ip_options; 
    end; function icmpcreatefile: THandle; stdcall; external 'icmp.dll' name 'ICMPCreateFile'; function ICMPSendEcho:function(ICMPHandle: THandle; 
    DestAddress:longint; 
    Requestdata:pointer; 
    requestsize:word; 
    RequestOptns: p_ip_options; 
    ReplyBuffer:pointer; 
    Replysize:dword; 
    Timeout: DWord ):dword; stdcall; external 'icmp.dll' name 'ICMPSendEcho'; function ICMPCloseHandle:function(ICMPHandle:THandle):Boolean; stdcall; external 'icmp.dll' name 'ICMPCloseHandle'; function Ping(Ip: string): boolean; 
    var 
    f_ICMP_Handle: THandle; 
    f_address: LongInt; 
    f_timeout: Integer; 
    f_blocksize: Integer; 
    f_replysize: Integer; 
    requestoptions: T_ip_options; 
    reqyestdatam replybuffer: Pointer; 
    begin 
    f_ICMP_Handle:=icmpcreatefile; 
    f_timeout:=1000; f_blocksize:=64; 
    f_replysize:=16+sizeof(t_icmp_echo_reply)+f_blocksize; 
    GetMem(requestdata,f_blocksize); 
    fillchar(requestdata^,64,#$32); 
    GetMem(replybuffer,f_replysize); 
    requestoptions.ttl:=255; 
    requestoptions.tos:=0; 
    requestoptions.flags:=0; 
    requestoptions.optionssize:=0; 
    requestoptions.optionsdata:=NIL; 
    f_address := Inet_Addr(PChar(Ip)); if f_address = SOCKET_ERROR then 
    begin 
    result := false; 
    ICMPCloseHandle(f_icmp_handle); 
    exit; 
    end; if ICMPSendEcho(f_icmp_handle,f_address, 
    requestdata,f_blocksize, @requestoptions, 
    replybuffer,f_replysize, 
    f_timeout)<>1 then 
    result := false 
    else 
    result := true; 
    icmpclosehandle(f_icmp_handle); 
    end; 来自:曹晓钢 时间:99-8-31 02:35:06 ID:130028 
    这个问题不是刚讨论过吗? 
    pega说, 
    监视本机IP的变化,如果挂上了internet,就会多出一个新的IP来. 
    (是局域网就歇菜了.) 
    还有老外由这个思路写了控件了.不长,我就直接贴出来了. { ****************************************************************** } 
    { } 
    { VCL component TKZInternetDetect } { } 
    { Internet detector component } 
    { } 
    { Code generated by Component Create for Delphi } 
    { } 
    { Generated from source file d:\compon~1\intern~1\d3\kzintdet.cd } 
    { on 7 July 1999 at 21:52 } 
    { } 
    { Copyright ?1999 by Kire Zvezdakoski } 
    { } 
    { ****************************************************************** } unit KZIntDet; interface uses Messages, SysUtils, Classes, Controls, Forms, Graphics, 
    Windows, Extctrls, RAS; type 
    TKZInternetDetect = class(TComponent) 
    private 
    { Private fields of TKZInternetDetect } 
    FCheckInterval : Integer; 
    FConnected : Boolean; 
    FIP : String; 
    { Pointer to application's OnConnect handler, if any } 
    FOnConnect : TNotifyEvent; 
    { Pointer to application's OnDisconnect handler, if any } 
    FOnDisconnect : TNotifyEvent; 
    Timer : TTimer; { Private methods of TKZInternetDetect } 
    { Method to set variable and property values and create objects } 
    procedure AutoInitialize; 
    { Method to free any objects created by AutoInitialize } 
    procedure AutoDestroy; procedure SetConnected(Value : Boolean); 
    procedure SetIP(Value : String); 
    procedure Check(Sender:TObject); protected 
    { Protected fields of TKZInternetDetect } { Protected methods of TKZInternetDetect } 
    { Method to generate OnConnect event } 
    procedure Connect(Sender : TObject); virtual; 
    { Method to generate OnDisconnect event } 
    procedure Disconnect(Sender : TObject); virtual; 
    procedure Loaded; override; public 
    { Public fields and properties of TKZInternetDetect } { Public methods of TKZInternetDetect } constructor Create(AOwner: TComponent); override; 
    destructor Destroy; override; 
    function Execute : Boolean; published 
    { Published properties of TKZInternetDetect } 
    property OnConnect : TNotifyEvent read FOnConnect write FOnConnect; 
    property OnDisconnect : TNotifyEvent read FOnDisconnect write FOnDisconnect; 
    property CheckInterval : Integer 
    read FCheckInterval write FCheckInterval 
    default 1000; 
    property Connected : Boolean 
    read FConnected write SetConnected 
    default False; 
    property IP : String read FIP write SetIP; end; procedure Register; implementation procedure Register; 
    begin 
    { Register TKZInternetDetect with KireZ as its 
    default page on the Delphi component palette } 
    RegisterComponents('KireZ', [TKZInternetDetect]); 
    end; { Method to set variable and property values and create objects } 
    procedure TKZInternetDetect.AutoInitialize; 
    begin 
    Timer := TTimer.Create(Self); 
    FCheckInterval := 1000; 
    FConnected := False; 
    FIP := '127.0.0.1'; 
    end; { of AutoInitialize } { Method to free any objects created by AutoInitialize } 
    procedure TKZInternetDetect.AutoDestroy; 
    begin 
    Timer.Free; 
    end; { of AutoDestroy } procedure TKZInternetDetect.SetConnected(Value : Boolean); begin 
    FConnected := Value; 
    end; procedure TKZInternetDetect.SetIP(Value : String); 
    begin 
    FIP := Value; 
    end; { Method to generate OnConnect event } 
    procedure TKZInternetDetect.Connect(Sender : TObject); 
    begin 
    { Has the application assigned a method to the event, whether 
    via the Object Inspector or a run-time assignment? If so, 
    execute that method } 
    if Assigned(FOnConnect) then 
    FOnConnect(Sender); 
    end; { Method to generate OnDisconnect event } 
    procedure TKZInternetDetect.Disconnect(Sender : TObject); 
    begin 
    { Has the application assigned a method to the event, whether 
    via the Object Inspector or a run-time assignment? If so, 
    execute that method } 
    if Assigned(FOnDisconnect) then 
    FOnDisconnect(Sender); 
    end; procedure TKZInternetDetect.Check(Sender:TObject); var 
    RASConns:TRasConn; 
    dwSize:dword; 
    dwCount:dword; 
    RASpppIP:TRASpppIP; 
    T:Boolean; 
    r:string; 
    begin 
    r:=''; 
    RASConns.dwSize:=SizeOf(TRASConn); 
    RASpppIP.dwSize:=SizeOf(RASpppIP); 
    dwSize:=SizeOf(RASConns); 
    If RASEnumConnections(@RASConns, dwSize, dwCount)=0 Then If dwCount > 0 Then begin 
    dwSize := SizeOf(RASpppIP); 
    If RASGetProjectionInfo(RASConns.hRasConn, RASP_PPPIP, @RasPPPIP, dwSize)=0 Then 
    r:=strPas(RASpppIP.szIPAddress); 
    End; 
    t:=Connected; 
    Connected:=(r<>'127.0.0.1') and (r<>'') and (r<>' '); 
    FIP:=r; 
    if ((not t) and Connected) then Connect(Sender); 
    if (t and (not Connected)) then Disconnect(Sender); 
    end; constructor TKZInternetDetect.Create(AOwner: TComponent); begin 
    inherited Create(AOwner); 
    AutoInitialize; { Code to perform other tasks when the component is created } 
    Timer.OnTimer:=Check; 
    CheckInterval:=1000; 
    end; destructor TKZInternetDetect.Destroy; 
    begin 
    AutoDestroy; 
    inherited Destroy; 
    end; function TKZInternetDetect.Execute : Boolean; 
    begin 
    { Perform the component operation } { Return True if the operation was successful, False otherwise } 
    Result := True 
    end; procedure TKZInternetDetect.Loaded; 
    begin 
    inherited Loaded; { Perform any component setup that depends on the property values having been set } end; 
    end. 
    来自:DNChen 时间:99-8-31 07:30:18 ID:130040 
    >2. windows登陆internet后,使用什麽消息变量传递的.我可否使用 
    没有消息可以响应,只有你自己的程序拨号时,才可以提供一个回调函数给ras api, 
    这个时候才可以获得拨号成功/失败信息。 来自:cytown 时间:99-8-31 17:53:46 ID:130196 
    可以通过ping www.netscape.com是否通来判断, 不过, 如果你的局域网 
    有一个机器或服务器是WWW.NETSCAPE.COM, 那么...... 
    话说回来了, 谁会设这么一台机器呢? 可以用ics构件里的tping构件. 来自:Kim Wong 时间:99-8-31 22:50:32 ID:130290 
    使用 idle 是否会比用 timer 节约资源呢? 
    不过我以前就是用 timer 来实现的一个记费器,并没感觉事件响应有迟缓。 
    其实 timer 也不是太占资源啦!