delphi 检测网络是否连通  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 检测网络是否连通


uses WinInet;

////网络是否连通
function InternetConnected: Boolean;
const
  // local system uses a modem to connect to the Internet.
  INTERNET_CONNECTION_MODEM      = 1;
  // local system uses a local area network to connect to the Internet.
  INTERNET_CONNECTION_LAN        = 2;
  // local system uses a proxy server to connect to the Internet.
  INTERNET_CONNECTION_PROXY      = 4;
  // local system's modem is busy with a non-Internet connection.
  INTERNET_CONNECTION_MODEM_BUSY = 8;
var
  dwConnectionTypes : DWORD;
begin
  dwConnectionTypes := INTERNET_CONNECTION_MODEM+ INTERNET_CONNECTION_LAN
      + INTERNET_CONNECTION_PROXY;
  Result := InternetGetConnectedState(@dwConnectionTypes, 0);
end;

{=================================================================
  功  能: 检测机器是否登入网络
  参  数: 无
  返回值: 成功:  True  失败:  False
=================================================================}
Function NetCheckMacAttachNet: Boolean;
begin
  Result := False;
  if GetSystemMetrics(SM_NETWORK) <> 0 then  //所有连入网的
    Result := True;
end;


procedure SaveLog(const sMsg:string);
var
  sList:TStringList;
  fn:string;
begin
  sList := TStringList.Create;
  fn := ExtractFilePath(ParamStr(0))+'SysLog.txt';
  try
    if FileExists(fn) then
      sList.LoadFromFile(fn);
    if sList.Count>1000 then
      sList.Clear;
    sList.Add(FormatDateTime('yyyy-mm-dd hh:nn:ss',Now)+' '+sMsg);
    sList.SaveToFile(fn);
  finally
    sList.Free;
  end;
end;


调用:

 try
    if NetCheckMacAttachNet and InternetConnected then
     //
    else
    begin
    //  SaveLog('检测到网络连接中断!');
    end;
  except
    on e:Exception do
     // SaveLog(e.Message);
  end;


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.040394067764282 seconds