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从全路径中分离路径,有'\'

Delphi删除目录及其子目录

Delphi 检验指定字符串网址是否输入正确

Delphi 内存截取字符串

Delphi 利用阿里大于接口发短信(Delphi版)

Delphi 进程隐藏的若干方法

Delphi 自动把combobox控件的内容里没有的内容加入列表中

Delphi获取系统电源状态的信息

Delphi 建立access数据库

Delphi xe增强的rtti妙用--动态创建包中的窗口类

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



打赏

取消

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

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

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

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

评论

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