问题描述:
1、我的一个常驻内存程序,想时时检查是否连接Internet。
2、我用winAPI InternetCheckConnection 做了一下检讨查,如查在线状态,检查速度特快;如果是离线状态,检查是否连网,速度特慢。
3、我用线程、定时器俩种方式,做在线状态检查,他们检查(离线状态)都是特慢,并且CPU一直是100%.能否像MSN那样,时时检查到网络连接状态,并且不能特吃系统资源。
1、我的一个常驻内存程序,想时时检查是否连接Internet。
2、我用winAPI InternetCheckConnection 做了一下检讨查,如查在线状态,检查速度特快;如果是离线状态,检查是否连网,速度特慢。
3、我用线程、定时器俩种方式,做在线状态检查,他们检查(离线状态)都是特慢,并且CPU一直是100%.能否像MSN那样,时时检查到网络连接状态,并且不能特吃系统资源。
InternetCheckConnection
Allows an application to check if a connection to the Internet can be established.BOOL InternetCheckConnection(
LPCTSTR lpszUrl,
DWORD dwFlags,
DWORD dwReserved
);Parameters
lpszUrl
[in] Pointer to a null-terminated string that specifies the URL to use to check the connection. This value can be NULL.
dwFlags
[in] Options. FLAG_ICC_FORCE_CONNECTION is the only flag that is currently available. If this flag is set, it forces a connection. A sockets connection is attempted in the following order:If lpszUrl is non-NULL, the host value is extracted from it and used to ping that specific host. //第一个参数不为nil,那个这个函数就去ping 那个主机
If lpszUrl is NULL and there is an entry in the internal server database for the nearest server, the host value is extracted from the entry and used to ping that server.
//第一个参数为空,人家就自己找主机ping。
dwReserved
[in] Reserved. Must be zero.
看吧,还是别用了,都是ping,
用indy的icm控件自己ping以下是个不错的选择。
当然自己写个ping也可以。
uses IdIcmpClient,classes,QForms,SysUtils,IdException,Dialogs;
type
TCustomIcmpClient = class(TIdIcmpClient)private
state:integer;
//state=1正常
//state=0主机未知
constructor create();reintroduce;
public
destructor destroy();override;
class function getInstance():TCustomIcmpClient;
procedure doProcOnReply(ASender: TComponent; const AReplyStatus: TReplyStatus);
function isHostExists(theHostName:string;thePort:integer):boolean;
end;
implementation
var myIcmpClient :TCustomIcmpClient;constructor TCustomIcmpClient.create();
begin
inherited create(nil);
end;destructor TCustomIcmpClient.destroy();
begin
freeandnil(myIcmpClient);
inherited;
end;class function TCustomIcmpClient.getInstance():TCustomIcmpClient;
begin
if myIcmpClient = nil then
myIcmpClient := create();
myIcmpClient.OnReply := myIcmpClient.doProcOnReply;
Result := myIcmpClient;
end;function TCustomIcmpClient.isHostExists(theHostName:string;thePort:integer):boolean;
begin
try
myIcmpClient.Host := theHostName;
myIcmpClient.Port := thePort;
myIcmpClient.ReceiveTimeout := 500;
self.Ping;
except
on Exception do
begin
Result := false;
exit;
end;
end;
Result := (state=1);
end;procedure TCustomIcmpClient.doProcOnReply(ASender: TComponent; const AReplyStatus: TReplyStatus);
begin
if AReplyStatus.BytesReceived <=0 then
state := 0
else
state := 1;
end;end.
Function CheckInterConnection:Boolean;//检查当前物理连接是否正常
Function CheckInternetQueryOption:Boolean;//检查是否能够获取INTERNET服务
以下是具体实现这//利用YAHOO网站,返回当前是否物理联网:即网卡是否正常、网线是否接好等
Function TIpFileCopy.CheckInterConnection:Boolean;
begin
Result:=InternetCheckConnection('http://www.yahoo.com/', 1, 0);
end;//利用InternetQueryOption函数,得到是否能够获取INTERNET的各种服务
Function TIpFileCopy.CheckInternetQueryOption:Boolean;
var
ConnectState: DWORD;
StateSize: DWORD;
begin
ConnectState:= 0;
StateSize:= SizeOf(ConnectState);
result:= false;
{InternetQueryOption函数的返回值
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 连接状态,正在响应连接请求}
if InternetQueryOption(nil, INTERNET_OPTION_CONNECTED_STATE, @ConnectState, StateSize) then
if (ConnectState and INTERNET_STATE_DISCONNECTED) <> 2 then result:= true;
end;