这段代码是检测.一掉线马上就知道的,对你可能有帮助 unit Link; interface function GateWay:string; procedure IfLink;implementation uses main,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,Registry, Grids; function GateWay:string; var Reg :TRegistry; MyStr : TStrings; buffer : array[0..1024] of byte; i:integer; info:TRegKeyInfo ; str:string; begin Result:='0'; Reg :=TRegistry.Create; Mystr:=Tstringlist.Create; try Reg.RootKey :=HKEY_LOCAL_MACHINE; if Reg.OpenKey('SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards',false) then begin if reg.GetKeyInfo(info) then begin reg.GetKeyNames(mystr); str:='SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\'+Mystr.Strings[0]; end; end; finally Reg.CloseKey; Mystr.Free; Reg.RootKey :=HKEY_LOCAL_MACHINE; if reg.OpenKey(str,false) then begin str:=reg.ReadString('ServiceName'); end; Reg.CloseKey; Reg.RootKey :=HKEY_LOCAL_MACHINE; if reg.OpenKey('SYSTEM\CurrentControlSet\Services\'+str+'\Parameters\Tcpip\',false) then begin str:=''; for i:= 1 to reg.ReadBinaryData('defaultGateway',buffer,sizeof(buffer)) do str:=str+chr(dword(buffer[i-1])); end; if str=chr(0) then begin str:=''; for i:= 1 to reg.ReadBinaryData('DhcpDefaultGateway',buffer,sizeof(buffer)) do str:=str+chr(dword(buffer[i-1])); end; Reg.CloseKey; reg.Free; if (str=char(0)) or (str='') then str:='66.218.71.88'; result:=str; end; end; procedure IfLink;begin with mainForm do begin ICMP.ReceiveTimeout:=10; try ICMP.Host := GateWay; ICMP.Ping; linklabel.caption:='正常'; linkLabel.Font.Color:=clGreen; except linklabel.caption:='断开'; mainform.linkLabel.Font.Color:=clred; end; end; end;end.
检测本机的可以用 FindWindow
本机可以findwindow,但是网络那就不知道了
HPrevInst := FindWindow('TForm1',nil); if HPrevInst = 0 then showmessage('运行') ELSE showmessage('未运行'); 你得知道所要检测程序的窗体名
unit Link;
interface
function GateWay:string;
procedure IfLink;implementation uses main,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,Registry,
Grids;
function GateWay:string;
var
Reg :TRegistry;
MyStr : TStrings;
buffer : array[0..1024] of byte;
i:integer;
info:TRegKeyInfo ;
str:string;
begin
Result:='0';
Reg :=TRegistry.Create;
Mystr:=Tstringlist.Create;
try
Reg.RootKey :=HKEY_LOCAL_MACHINE;
if Reg.OpenKey('SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards',false) then
begin
if reg.GetKeyInfo(info) then
begin reg.GetKeyNames(mystr);
str:='SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\'+Mystr.Strings[0];
end;
end;
finally
Reg.CloseKey;
Mystr.Free;
Reg.RootKey :=HKEY_LOCAL_MACHINE;
if reg.OpenKey(str,false) then
begin
str:=reg.ReadString('ServiceName');
end;
Reg.CloseKey;
Reg.RootKey :=HKEY_LOCAL_MACHINE;
if reg.OpenKey('SYSTEM\CurrentControlSet\Services\'+str+'\Parameters\Tcpip\',false) then
begin
str:='';
for i:= 1 to reg.ReadBinaryData('defaultGateway',buffer,sizeof(buffer)) do
str:=str+chr(dword(buffer[i-1]));
end;
if str=chr(0) then
begin
str:='';
for i:= 1 to reg.ReadBinaryData('DhcpDefaultGateway',buffer,sizeof(buffer)) do
str:=str+chr(dword(buffer[i-1]));
end; Reg.CloseKey;
reg.Free;
if (str=char(0)) or (str='') then
str:='66.218.71.88';
result:=str;
end;
end;
procedure IfLink;begin
with mainForm do begin ICMP.ReceiveTimeout:=10;
try
ICMP.Host := GateWay;
ICMP.Ping; linklabel.caption:='正常';
linkLabel.Font.Color:=clGreen;
except
linklabel.caption:='断开';
mainform.linkLabel.Font.Color:=clred;
end;
end;
end;end.
FindWindow
if HPrevInst = 0 then showmessage('运行')
ELSE showmessage('未运行');
你得知道所要检测程序的窗体名