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;

相关阅读 >>

Delphi net.httpclient用最精简的代码获取网页数据

Delphi获取进程pid

线程与进程的区别

Delphi 自我拷贝复制

Delphi 关闭显示器

Delphi ifdef 另类用法

Delphi ttabcontrol

Delphi取得文件图标并在tlistview中显示

Delphi图片格式判断

Delphi httpencode编码

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...