怎样判断计算机是否已经连接网络?(delphi5编程)(200分)

A

atty

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样判断计算机是否已经连接网络?(delphi5编程)。本人初学编程,请指教,用什么控件或函数能
判断计算机是否已经连接上网?请给出详细代码,谢谢!!
 
转载一篇文章:
检测计算机的 Internet 连接状态
检测计算机是否联网比较简单的做法可以通过一个 Win32 Internet(WinInet)
函数 InternetCheckConnection来实现;
这个函数的功能是检查是否能够建立 Internet 连接。
它的实现是在 %SystemRoot%/System32/wininet.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);
begin

if InternetCheckConnection('http://www.yahoo.com/', 1, 0) then

edit1.text:= 'Connected'
else

edit1.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;
var
ConnectState: DWORD;
StateSize: DWORD;
begin

ConnectState:= 0;
StateSize:= SizeOf(ConnectState);
result:= false;
if InternetQueryOption(nil, INTERNET_OPTION_CONNECTED_STATE, @ConnectState, StateSize) then

if (ConnectState and INTERNET_STATE_DISCONNECTED) <> 2 then
result:= true;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin

if CheckOffline then

edit1.text:= 'Connect To ISP'
else

edit1.text:= 'Disconnect To ISP';
end;

需要说明的是 InternetQueryOption 函数的检测结果只能表明当前的 Internet 设置是可用的,
并不能表示计算机一定能访问 Internet,例如网线掉了,网卡突然坏了之类的错误就没法检测出来,
要想检测当前计算机是否能够获得 Internet 服务了必须两个函数结合起来使用。
以上程序在 Win2000, Delphi5.0 下调试通过。
最后要注意的是在 uses 中要加上 WinInet。
 
贴一段代码给你,检测你是否连在网上.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, Psock, NMsmtp;
const
WM_LOGIN =WM_USER+100;
Type
TWM = record
Msg: WORD;
wParam:Word;
lParam:DWORD;
end;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label4: TLabel;
Edit4: TEdit;
Label5: TLabel;
Label3: TLabel;
ListBox1: TListBox;
Label6: TLabel;
Edit5: TEdit;
Label7: TLabel;
Memo1: TMemo;
StatusBar1: TStatusBar;
btnSend: TButton;
Edit6: TEdit;
Button1: TButton;
NMSMTP1: TNMSMTP;
procedure btnSendClick(Sender: TObject);
procedure NMSMTP1Connect(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure NMSMTP1Disconnect(Sender: TObject);
procedure NMSMTP1ConnectionFailed(Sender: TObject);
procedure NMSMTP1HostResolved(Sender: TComponent);
procedure NMSMTP1InvalidHost(var Handled: Boolean);
procedure NMSMTP1SendStart(Sender: TObject);
procedure NMSMTP1Success(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ListBox1KeyUp(Sender: TObject;
var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
procedure WMLOGIN(var Msg:TWM);Message WM_LOGIN;
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
{ TForm1 }
procedure TForm1.WMLOGIN(var Msg: TWM);
var
i:integer;
begin
//Send mail
//fix part of mail
NMSMTP1.PostMessage.FromAddress := Edit4.Text;
NMSMTP1.PostMessage.FromName := Edit3.Text;
NMSMTP1.PostMessage.Subject := Edit5.Text;
//clear old info
NMSMTP1.PostMessage.ToAddress.Clear;
//send a mail back
//NMSMTP1.PostMessage.ToAddress.Add(Edit4.Text);
NMSMTP1.PostMessage.Body.Assign(Memo1.Lines);
//set mail list
//NMSMTP1.PostMessage.ToBlindCarbonCopy.Clear;
for i:=0 to ListBox1.Items.Count -1do
begin
NMSMTP1.PostMessage.ToAddress.Add(
ListBox1.Items.Strings);
end;
NMSMTP1.SendMail;
NMSMTP1.Disconnect;
end;

procedure TForm1.btnSendClick(Sender: TObject);
begin
//set server info
NMSMTP1.Host := Edit1.Text;
NMSMTP1.Port := StrToInt(Edit2.Text);
NMSMTP1.UserID:=Edit3.Text;
//update view
StatusBar1.SimpleText:='Connecting...';
StatusBar1.Update;
//connect
NMSMTP1.Connect;
end;

procedure TForm1.NMSMTP1Connect(Sender: TObject);
begin
//update view
StatusBar1.SimpleText:='Connected';
StatusBar1.Update;

SendMessage(Handle,WM_LOGIN,0,0);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
//server info
Edit1.Text:='10.13.101.10';
Edit2.Text:='25';
//User info
Edit3.Text:='Bowman';
Edit4.Text:='MatthewBowman@21cn.com';
//Empty Other
Edit5.Text:='';
Edit6.Text:='';
Memo1.Text:='';
end;

procedure TForm1.NMSMTP1Disconnect(Sender: TObject);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := 'Disconnected from server';

end;

procedure TForm1.NMSMTP1ConnectionFailed(Sender: TObject);
begin
ShowMessage('Connection Failed');
end;

procedure TForm1.NMSMTP1HostResolved(Sender: TComponent);
begin
StatusBar1.SimpleText := 'HostResolved';
end;

procedure TForm1.NMSMTP1InvalidHost(var Handled: Boolean);
begin
StatusBar1.SimpleText := 'InvalidHost';
end;

procedure TForm1.NMSMTP1SendStart(Sender: TObject);
begin
StatusBar1.SimpleText := 'Sending Envelop...';
end;

procedure TForm1.NMSMTP1Success(Sender: TObject);
begin
StatusBar1.SimpleText := 'Success';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
//add list
ListBox1.Items.Add(Edit6.Text);
end;

procedure TForm1.ListBox1KeyUp(Sender: TObject;
var Key: Word;
Shift: TShiftState);
var
idx:integer;
begin
if ListBox1.ItemIndex=-1 then
exit;
if Key=VK_DELETE then
begin
idx:=ListBox1.ItemIndex;
ListBox1.Items.Delete(idx);
end;
end;

end.
 
如果你事先检索一下论坛,就不用花冤枉分了,这个问题在论坛上讨论过N次了。
uses WinInet;
procedure TForm1.Button1Click(Sender: TObject);
function GetOnlineStatus : Boolean;
var ConTypes : Integer;
begin
ConTypes := INTERNET_CONNECTION_MODEM + INTERNET_CONNECTION_LAN + INTERNET_CONNECTION_PROXY;
if (InternetGetConnectedState(@ConTypes, 0) = False)
then
Result := False
else
Result := True;
end;

begin
if GetOnlineStatus then
ShowMessage(' Connected')
else
ShowMessage(' not Connected');
end;
 
可以用ics中ping控件来解决的,
只要能ping得通当地ISP的dns服务器,即可判断出是否连接互联网。
我ping的是yahoo的dns为61.235.128.76,只要ping得通就八九不离十了

我是5秒钟判断一次
procedure TForm1.Ping1EchoReply(Sender, Icmp: TObject;
Error: Integer);
var code:integer;
begin
code:=ping1.reply.status;
if code=0 then
... //0表示ping得通。
end;

我已经用了能通过sysget代理,但不能通过winget代理。先下载个ics控件吧!
 
顶部