一个上网计时的软件,主要用猫上网,不知如何判断用户是否已拨号接入网络?

解决方案 »

  1.   

    if GetSystemMetrics(SM_NETWORK) AND $01 = $01 then 
    ShowMessage('Machine is attached to network') elseShowMessage('Machine is not attached to network'); 
     
      

  2.   

    TO  hammer_shi(@农业专家@) :  不行呀,上不上网都显示"'Machine is attached to network" 呀,还是不能判断。
      

  3.   

    转:
     DWORD dwConnectState; 
    CString strConnectState;BOOL bOK = InternetGetConnectedState( &dwConnectState, 0);if ( bOK ){if ( dwConnectState & INTERNET_CONNECTION_LAN )strConnectState = "Local system uses a local area network to connect to the Internet. ";if ( dwConnectState & INTERNET_CONNECTION_MODEM )strConnectState = "Local system uses a modem to connect to the Internet. ";if ( dwConnectState & INTERNET_CONNECTION_MODEM_BUSY )strConnectState = "No longer used. ";if ( dwConnectState = INTERNET_CONNECTION_PROXY )strConnectState = "Local system uses a proxy server to connect to the Internet. ";}测试连接是否有效,可以用:InternetCheckConnection检测计算机是否联网比较简单的做法可以通过一个 Win32 Internet(WinInet) 函数 InternetCheckConnection来实现;这个函数的功能是检查是否能够建立 Internet 连接。它的实现是在 %SystemRoot%.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);beginif InternetCheckConnection('http://www.yahoo.com/', 1, 0) thenedit1.text:= 'Connected'elseedit1.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;varConnectState: DWORD;StateSize: DWORD;beginConnectState:= 0;StateSize:= SizeOf(ConnectState);result:= false;if InternetQueryOption(nil, INTERNET_OPTION_CONNECTED_STATE, @ConnectState, StateSize) thenif (ConnectState and INTERNET_STATE_DISCONNECTED) <> 2 then result:= true;end;procedure TForm1.Button1Click(Sender: TObject);beginif CheckOffline thenedit1.text:= 'Connect To ISP'elseedit1.text:= 'Disconnect To ISP';end;需要说明的是 InternetQueryOption 函数的检测结果只能表明当前的 Internet 设置是可用的,并不能表示计算机一定能访问 Internet,例如网线掉了,网卡突然坏了之类的错误就没法检测出来,要想检测当前计算机是否能够获得 Internet 服务了必须两个函数结合起来使用。以上程序在 Win2000, Delphi5.0 下调试通过。最后要提醒大家注意的是在 uses 中要加上 WinInet。*********************************************************check if I am connected to the internet?interfaceusesWindows, SysUtils, Registry, WinSock, WinInet;typeTConnectionType = (ctNone, ctProxy, ctDialup);function ConnectedToInternet: TConnectionType;function RasConnectionCount: Integer; implementation//For RasConnectionCount =======================constcERROR_BUFFER_TOO_SMALL = 603;cRAS_MaxEntryName = 256;cRAS_MaxDeviceName = 128;cRAS_MaxDeviceType = 16;typeERasError = class(Exception);HRASConn = DWORD;PRASConn = ^TRASConn;TRASConn = recorddwSize: DWORD;rasConn: HRASConn;szEntryName: array[0..cRAS_MaxEntryName] of Char;szDeviceType: array[0..cRAS_MaxDeviceType] of Char;szDeviceName: array [0..cRAS_MaxDeviceName] of Char;end;TRasEnumConnections =function(RASConn: PrasConn; { buffer to receive Connections data }var BufSize: DWORD; { size in bytes of buffer }var Connections: DWORD { number of Connections written to buffer }): Longint;stdcall;//End RasConnectionCount ======================= function ConnectedToInternet: TConnectionType;varReg: TRegistry;bUseProxy: Boolean;UseProxy: LongWord;beginResult := ctNone;Reg := TRegistry.Create;with REG dotrytryRootKey := HKEY_CURRENT_USER;if OpenKey('settings', False) thenbegin//I just try to read it, and trap an exceptionif GetDataType('ProxyEnable') = rdBinary thenReadBinaryData('ProxyEnable', UseProxy, SizeOf(Longword))elsebeginbUseProxy := ReadBool('ProxyEnable');if bUseProxy thenUseProxy := 1elseUseProxy := 0;end;if (UseProxy <> 0) and (ReadString('ProxyServer') <> '') thenResult := ctProxy;end;except//Obviously not connected through a proxyend;finallyFree;end;//We can check RasConnectionCount even if dialup networking is not installed//simply because it will return 0 if the DLL is not found.if Result = ctNone thenbeginif RasConnectionCount > 0 then Result := ctDialup;end;end;function RasConnectionCount: Integer;varRasDLL: HInst;Conns: array[1..4] of TRasConn;RasEnums: TRasEnumConnections;BufSize: DWORD;NumConns: DWORD;RasResult: Longint;beginResult := 0;//Load the RAS DLLRasDLL := LoadLibrary('rasapi32.dll');if RasDLL = 0 then Exit;tryRasEnums := GetProcAddress(RasDLL, 'RasEnumConnectionsA');if @RasEnums = nil thenraise ERasError.Create('RasEnumConnectionsA not found in rasapi32.dll');Conns[1].dwSize := SizeOf(Conns[1]);BufSize := SizeOf(Conns);RasResult := RasEnums(@Conns, BufSize, NumConns);if (RasResult = 0) or (Result = cERROR_BUFFER_TOO_SMALL) then Result := NumConns;finallyFreeLibrary(RasDLL);end;end; 
      

  4.   

    以上的代码都不是实时的。只有Ping才是实时的!
      

  5.   

    TO kiboisme(还是铁棒.....针): 用哪个函数取拨号IP,怎么样判定这个IP不是局域网IP呢?假如说主机有网卡连接局域网.
      

  6.   

    TO dancedog(猪熔鸡):
              如果你PIN的地址正好关了,或不通,那还有用吗?
      

  7.   

    To:silvermoon(quake)
    如果不是在主机上判断,就难了。如果用Sygate或者WinGate代理出去的,在普通电脑上想得到主机是怎么上网的,我不知道。
    如果在主机上想知道自己是怎么上网的,就可以用:
    用函数去取拨号IP,如果取到就是拨号,否则....
    函数嘛,如下,GetRasIP这个函数可以得到本机拨号上网的IP地址:
    unit GetIpCon;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
    const
       RAS_MaxDeviceType = 16;     //设备类型名称长度
       RAS_MaxEntryName = 256;     //连接名称最大长度
       RAS_MaxDeviceName = 128;    //设备名称最大长度
       RAS_MaxIpAddress = 15;      //IP地址的最大长度
       RASP_PppIp = $8021;         //拨号连接的协议类型,该数值表示PPP连接
    type
       HRASCONN = DWORD;              //拨号连接句柄的类型
       RASCONN = record               //活动的拨号连接的句柄和设置信息
          dwSize : DWORD;             //该结构所占内存的大小(Bytes),一般设置为SizeOf(RASCONN)
          hrasconn : HRASCONN;        //活动连接的句柄
          szEntryName : array[0..RAS_MaxEntryName] of char;      //活动连接的名称
          szDeviceType : array[0..RAS_MaxDeviceType] of char;    //活动连接的所用的设备类型
          szDeviceName : array[0..RAS_MaxDeviceName] of char;    //活动连接的所用的设备名称
       end;   TRASPPPIP = record                //活动的拨号连接的动态IP地址信息
          dwSize : DWORD;                //该结构所占内存的大小(Bytes),一般设置为SizeOf(TRASPPPIP)
          dwError : DWORD;               //错误类型标识符
          szIpAddress : array[ 0..RAS_MaxIpAddress ] of char;     //活动的拨号连接的IP地址
       end;
    //获取所有活动的拨号连接的信息(连接句柄和设置信息)
    Function GetRasIP(RasIndex:integer):String;
    function RasEnumConnections(
        var lprasconn      : RASCONN ;    //接收活动连接的缓冲区的指针
        var lpcb           : DWORD;       //缓冲区大小
        var lpcConnections : DWORD        //实际的活动连接数
        ) : DWORD; stdcall;function RasEnumConnections ; external 'Rasapi32.dll' name 'RasEnumConnectionsA';//获取指定活动的拨号连接的动态IP信息
    function RasGetProjectionInfo(
       hrasconn         : HRasConn;             //指定活动连接的句柄
       rasprojection    : DWORD;                //RAS连接类型
       var lpprojection : TRASPPPIP;            //接收动态IP信息的缓冲区
       var lpcb         : DWord                 //接收缓冲区的大小
       ) : DWORD;stdcall;
    function RasGetProjectionInfo ; external 'Rasapi32.dll' name 'RasGetProjectionInfoA';implementationFunction GetRasIP(RasIndex:integer):String;
    var
       connections     : array[0..9] of RASCONN ; //拨号连接数组
       longSize        : dword;
       intAvailabelConnections : dword;                          //活动的拨号连接的实际数目
       intIndex        : integer;
       dwResult        : DWORD;
       dwSize          : DWORD;
       RASpppIP        : TRASPPPIP ;                             //活动的拨号连接的动态IP地址信息
    begin
       try
         connections[ 0 ].dwSize := sizeof(RASCONN);
         longSize := 10 * connections[ 0 ].dwSize ;    //接收活动连接的缓冲区大小
         intAvailabelConnections := 0 ;                            //获取所有活动的拨号连接的信息(连接句柄和设置信息)
         dwResult := RasEnumConnections( connections[ 0 ], longSize,intAvailabelConnections );
         Result := '';
         if 0 <> dwResult then
            Result := 'Error'
         else begin
         for intIndex := 0 to intAvailabelConnections - 1 do  begin
            dwSize := SizeOf(RASpppIP);
            RASpppIP.dwSize := dwSize;
            dwResult := RASGetProjectionInfo(connections[intIndex].hRasConn,RASP_PppIp,RasPPPIP,dwSize);//获取动态IP地址
            if 0 <> dwResult then
               Result := 'Error'
            else
               Result := StrPas(RASpppIP.szIPAddress);
            end;
         end;
       except
       end;
    end;
    end.
      

  8.   

    to dancedog :
    你应该先监测一下我贴的那几个网络函数再发言~
      

  9.   

    我也很想知道这个问题的答案,但只是在局域网上,我知道目标机器的IP和机器名。
    有什么最快最好的办法,如果用PING的话程序改怎么写?
    谢谢各位!!!