| 今天要做的任务,经理要我往ERP系统中增加一个功能,检测网络连接是否正常,还有是否与服务器中数据库通讯正常!
第二个很简单.用Timer定时用GetDate()发送select请求.
现在我说说第一个:第一个我一拿起来就使用了个WNetOpenEnum函数,来定时列举网络状态,这样来判断.但是我发现这样很浪费资源,有时候这个函数很不稳定.后来我在C#的论坛里面看到一个同样检测网络连接的例子看到了这个:
|
using System ; using System.Runtime ; using System.Runtime.InteropServices ; public class InternetCS { //Creating the extern function... [DllImport("wininet.dll")] private extern static bool InternetGetConnectedState( out int Description, int ReservedValue ) ; //Creating a function that uses the API function... public static bool IsConnectedToInternet( ) { int Desc ; return InternetGetConnectedState( out Desc, 0 ) ; } } |
哈哈这样终于OK了.于是我移植到我的delphi上;
我把我的delphi实现代码贴出来:
|
var Form1: TForm1; Flag:Integer; function InternetGetConnectedState(out Description :integer;ReservedValue :integer):boolean;stdcall external 'C:\WINDOWS\system32\wininet.dll'; implementation
{$R *.dfm}
procedure TForm1.Timer2Timer(Sender: TObject); begin edit1.Text:=datetimetostr(now); end;
procedure TForm1.FormCreate(Sender: TObject); begin Flag:=1; end;
procedure TForm1.Button1Click(Sender: TObject); var value:integer; begin if InternetGetConnectedState(value,0)=true then showMessage('连接正常') else showmessage('连接出错'); end; end. | 这样实现就很简单了.呵呵.一个函数搞定.....哈哈 |