win7 下用户交互桌面 不能实现。 可以单独做一个服务来守护你的程序。 .exe'; end;procedure TEDSClientMonitorService.ServiceStop(Sender: TService; var Stopped: Boolean); begin EnableDebugPrivilege; KillTask(FLocalFileName); IntAndSet('Cose'); CloseHandle(Mh); Stopped := true; //WTSSendMessage(0, WTSGetActiveConsoleSessionId(), 'ss', 4, 'ddddd', 12, 0, 0, s, false); end;procedure TEDSClientMonitorService.SetDescription(const Desc: string); var reg: TRegistry; begin reg := TRegistry.Create; try with reg do begin RootKey := HKEY_LOCAL_MACHINE; if OpenKey('SYSTEM\CurrentControlSet\Services\'+Name,false) then begin WriteString('Description',Desc); end; CloseKey; end; finally reg.Free; end;end;procedure TEDSClientMonitorService.MonitorTimer(Sender: TObject); var f: string; hh, MI: string; nowtime: TDatetime; i: integer; begin Monitor.Enabled := False; Monitor.Interval := 1000; try nowtime := now; DateTimeToString(hh, 'hh', nowtime); DateTimeToString(mi, 'nn', nowtime); //每天定时重启 if (hh = '13') and (mi = '30') then begin Sleep(60000); ReStart(); end else begin //超过一天自动重启 if round((Gettickcount - Fcur) / 1000) > 86400 then begin Sleep(60000); ReStart(); exit; end; if ReadMsg = 'Cose' then begin Wlog('用户调用关闭服务'); exit; end; i := FindProcessID(FLocalFileName); f := ExtractFilePath(ParamStr(0)) + FLocalFileName; if i = 0 then begin LaunchAppIntoDifferentSession(Pchar(f)); end else begin if i > 1 then begin EnableDebugPrivilege; KillTask(FLocalFileName); LaunchAppIntoDifferentSession(Pchar(f)); //RefreshTrayIcon(); RemoveDeadIcons(); end; end; end; finally Monitor.Enabled := True; end; end;
uses Windows, Messages, SysUtils, TlHelp32, Classes, SvcMgr, SysUnit, ExtCtrls,Registry;const CSServiceDescription = '服务描述信息';type TEDSClientMonitorService = class(TService) Monitor: TTimer; procedure ServiceStart(Sender: TService; var Started: Boolean); procedure ServiceCreate(Sender: TObject); procedure ServiceStop(Sender: TService; var Stopped: Boolean); procedure ReStart(); procedure MonitorTimer(Sender: TObject); procedure ServiceAfterInstall(Sender: TService); private MutexNameClient: string; MutexNameServer: string; Mh: Cardinal; //将创建进程的文件名 FLocalFileName: pchar; Fcur: Dword; function AppRunOnce(): boolean; procedure SetDescription(const Desc: string); public function GetServiceController: TServiceController; override; { Public declarations } end;var EDSClientMonitorService: TEDSClientMonitorService;implementation uses ServiceControl; {$R *.DFM}procedure ServiceController(CtrlCode: DWord); stdcall; begin EDSClientMonitorService.Controller(CtrlCode); end; //function TEDSClientMonitorService.GetServiceController: TServiceController; begin Result := ServiceController; end;function TEDSClientMonitorService.AppRunOnce(): boolean; var MutexHandle: THandle; s, gt: Cardinal;begin MutexHandle := OpenMutex(MUTEX_ALL_ACCESS, True, PChar(MutexNameClient)); gt := GetLastError; if gt = Error_ALREADY_EXISTS then begin result := true; exit; end; if (MutexHandle <> 0) then begin result := true; end else begin Mh := CreateMutex(nil, True, PChar(MutexNameClient)); s := GetLastError(); if (s = ERROR_ALREADY_EXISTS) then begin CloseHandle(Mh); end; result := false; end; end; //procedure TEDSClientMonitorService.ServiceStart(Sender: TService; var Started: Boolean); var sfilename: string; begin Fcur := Gettickcount; try IntAndSet('Start'); sfilename := ExtractFilePath(ParamStr(0)) + FLocalFileName; if AppRunOnce then begin exit; end; // LaunchAppIntoDifferentSession(Pchar(sfilename)); except end; end;procedure TEDSClientMonitorService.ReStart(); var B: boolean; begin ServiceStop(self, b); EnableDebugPrivilege; KillTask(FLocalFileName); RefreshTrayIcon(); RemoveDeadIcons(); ServiceStart(self, b); end;procedure TEDSClientMonitorService.ServiceAfterInstall(Sender: TService); begin SetDescription(CSServiceDescription); end;procedure TEDSClientMonitorService.ServiceCreate(Sender: TObject); begin MutexNameClient := PChar(ExtractFilePath(ParamStr(0))); MutexNameClient := StringReplace(MutexNameClient, '\', '', [rfReplaceAll]); MutexNameClient := StringReplace(MutexNameClient, ':', '', [rfReplaceAll]); MutexNameServer := MutexNameClient + 'Server'; FLocalFileName := 'Myapp.exe'; end;procedure TEDSClientMonitorService.ServiceStop(Sender: TService; var Stopped: Boolean); begin EnableDebugPrivilege; KillTask(FLocalFileName); IntAndSet('Cose'); CloseHandle(Mh); Stopped := true; //WTSSendMessage(0, WTSGetActiveConsoleSessionId(), 'ss', 4, 'ddddd', 12, 0, 0, s, false); end;procedure TEDSClientMonitorService.SetDescription(const Desc: string); var reg: TRegistry; begin reg := TRegistry.Create; try with reg do begin RootKey := HKEY_LOCAL_MACHINE; if OpenKey('SYSTEM\CurrentControlSet\Services\'+Name,false) then begin WriteString('Description',Desc); end; CloseKey; end; finally reg.Free; end;end;procedure TEDSClientMonitorService.MonitorTimer(Sender: TObject); var f: string; hh, MI: string; nowtime: TDatetime; i: integer; begin Monitor.Enabled := False; Monitor.Interval := 1000; try nowtime := now; DateTimeToString(hh, 'hh', nowtime); DateTimeToString(mi, 'nn', nowtime); //每天定时重启 if (hh = '13') and (mi = '30') then begin Sleep(60000); ReStart(); end else begin //超过一天自动重启 if round((Gettickcount - Fcur) / 1000) > 86400 then begin Sleep(60000); ReStart(); exit; end; if ReadMsg = 'Cose' then begin Wlog('用户调用关闭服务'); exit; end; i := FindProcessID(FLocalFileName); f := ExtractFilePath(ParamStr(0)) + FLocalFileName; if i = 0 then begin LaunchAppIntoDifferentSession(Pchar(f)); end else begin if i > 1 then begin EnableDebugPrivilege; KillTask(FLocalFileName); LaunchAppIntoDifferentSession(Pchar(f)); //RefreshTrayIcon(); RemoveDeadIcons(); end; end; end; finally Monitor.Enabled := True; end; end;end 上一个没贴全
function TWin32Utils.IsRunning(const AExeName: String): Boolean;
var
szExeName: String;
hSnapshot: THandle;
rEntry32 : TProcessEntry32;
bExists : Boolean;
begin
Result := False;
szExeName := ExtractFileName(AExeName);
hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
try
rEntry32.dwSize := Sizeof(rEntry32);
bExists := Process32First(hSnapshot,rEntry32);
while bExists do
begin
if Pos(UpperCase(rEntry32.szExeFile), UpperCase(szExeName)) > 0 then
begin
Result := True;
Break;
end;
bExists := Process32Next(hSnapshot, rEntry32);
end;
finally
CloseHandle(hSnapshot);
end;
end;当然守护程序 要隐蔽 不要轻易被用户关掉 给个思路
做一个互相守护方案,A程序中存在一个定时器,定时检查并启动B程序,B程序中也存在一个定时器,定时检查并启动A程序开始 想做个 windows 服务程序 后来发现 不能跟有界面的exe打交道
TService.Interactive := True;
Interactive := True;测试发现 ShellExecute(0,'open','d:\mytest.exe',nil,nil,SW_SHOWNORMAL); 调用B程序时 会报错 (估计B 有界面的 原因)
你是 在什么操作系统 下 测试的 ? win2003 win7 下测试 B程序 界面 都无法显示
win2003 win7 下测试 B程序 界面 无法显示 但进程中存在 如果B程序正常工作这样也可以 (单独运行B程序是可以显示界面的)
请问你的 服务器伴侣 是windows服务吗 ? 能给出的 你的思路吗
2、程序使用VCL窗口,平时托盘形式,必要时打开窗口。
3、定时5分钟:搜索EXE的进程,搜索不到,就重新启动EXE。
可以单独做一个服务来守护你的程序。
.exe';
end;procedure TEDSClientMonitorService.ServiceStop(Sender: TService;
var Stopped: Boolean);
begin EnableDebugPrivilege;
KillTask(FLocalFileName);
IntAndSet('Cose'); CloseHandle(Mh);
Stopped := true;
//WTSSendMessage(0, WTSGetActiveConsoleSessionId(), 'ss', 4, 'ddddd', 12, 0, 0, s, false);
end;procedure TEDSClientMonitorService.SetDescription(const Desc: string);
var
reg: TRegistry;
begin
reg := TRegistry.Create;
try
with reg do begin
RootKey := HKEY_LOCAL_MACHINE;
if OpenKey('SYSTEM\CurrentControlSet\Services\'+Name,false) then
begin
WriteString('Description',Desc);
end;
CloseKey;
end;
finally
reg.Free;
end;end;procedure TEDSClientMonitorService.MonitorTimer(Sender: TObject);
var
f: string;
hh, MI: string;
nowtime: TDatetime;
i: integer;
begin
Monitor.Enabled := False;
Monitor.Interval := 1000;
try
nowtime := now;
DateTimeToString(hh, 'hh', nowtime);
DateTimeToString(mi, 'nn', nowtime);
//每天定时重启
if (hh = '13') and (mi = '30') then
begin
Sleep(60000);
ReStart();
end
else
begin
//超过一天自动重启
if round((Gettickcount - Fcur) / 1000) > 86400 then
begin
Sleep(60000);
ReStart();
exit;
end;
if ReadMsg = 'Cose' then
begin
Wlog('用户调用关闭服务');
exit;
end;
i := FindProcessID(FLocalFileName); f := ExtractFilePath(ParamStr(0)) + FLocalFileName;
if i = 0 then
begin
LaunchAppIntoDifferentSession(Pchar(f));
end
else
begin
if i > 1 then
begin
EnableDebugPrivilege;
KillTask(FLocalFileName);
LaunchAppIntoDifferentSession(Pchar(f));
//RefreshTrayIcon();
RemoveDeadIcons();
end;
end;
end;
finally
Monitor.Enabled := True;
end;
end;
uses
Windows, Messages, SysUtils, TlHelp32, Classes, SvcMgr,
SysUnit, ExtCtrls,Registry;const
CSServiceDescription = '服务描述信息';type
TEDSClientMonitorService = class(TService)
Monitor: TTimer;
procedure ServiceStart(Sender: TService; var Started: Boolean);
procedure ServiceCreate(Sender: TObject);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
procedure ReStart();
procedure MonitorTimer(Sender: TObject);
procedure ServiceAfterInstall(Sender: TService);
private MutexNameClient: string;
MutexNameServer: string;
Mh: Cardinal;
//将创建进程的文件名
FLocalFileName: pchar;
Fcur: Dword;
function AppRunOnce(): boolean;
procedure SetDescription(const Desc: string);
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;var
EDSClientMonitorService: TEDSClientMonitorService;implementation
uses ServiceControl;
{$R *.DFM}procedure ServiceController(CtrlCode: DWord); stdcall;
begin
EDSClientMonitorService.Controller(CtrlCode);
end;
//function TEDSClientMonitorService.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;function TEDSClientMonitorService.AppRunOnce(): boolean;
var
MutexHandle: THandle;
s, gt: Cardinal;begin
MutexHandle := OpenMutex(MUTEX_ALL_ACCESS, True, PChar(MutexNameClient));
gt := GetLastError;
if gt = Error_ALREADY_EXISTS then
begin
result := true;
exit;
end;
if (MutexHandle <> 0) then
begin
result := true;
end
else
begin
Mh := CreateMutex(nil, True, PChar(MutexNameClient));
s := GetLastError();
if (s = ERROR_ALREADY_EXISTS) then
begin
CloseHandle(Mh);
end;
result := false;
end;
end;
//procedure TEDSClientMonitorService.ServiceStart(Sender: TService;
var Started: Boolean);
var
sfilename: string;
begin
Fcur := Gettickcount;
try
IntAndSet('Start');
sfilename := ExtractFilePath(ParamStr(0)) + FLocalFileName;
if AppRunOnce then
begin
exit;
end;
// LaunchAppIntoDifferentSession(Pchar(sfilename));
except end;
end;procedure TEDSClientMonitorService.ReStart();
var
B: boolean;
begin
ServiceStop(self, b);
EnableDebugPrivilege;
KillTask(FLocalFileName);
RefreshTrayIcon();
RemoveDeadIcons();
ServiceStart(self, b);
end;procedure TEDSClientMonitorService.ServiceAfterInstall(Sender: TService);
begin
SetDescription(CSServiceDescription);
end;procedure TEDSClientMonitorService.ServiceCreate(Sender: TObject);
begin MutexNameClient := PChar(ExtractFilePath(ParamStr(0)));
MutexNameClient := StringReplace(MutexNameClient, '\', '', [rfReplaceAll]);
MutexNameClient := StringReplace(MutexNameClient, ':', '', [rfReplaceAll]);
MutexNameServer := MutexNameClient + 'Server';
FLocalFileName := 'Myapp.exe';
end;procedure TEDSClientMonitorService.ServiceStop(Sender: TService;
var Stopped: Boolean);
begin EnableDebugPrivilege;
KillTask(FLocalFileName);
IntAndSet('Cose'); CloseHandle(Mh);
Stopped := true;
//WTSSendMessage(0, WTSGetActiveConsoleSessionId(), 'ss', 4, 'ddddd', 12, 0, 0, s, false);
end;procedure TEDSClientMonitorService.SetDescription(const Desc: string);
var
reg: TRegistry;
begin
reg := TRegistry.Create;
try
with reg do begin
RootKey := HKEY_LOCAL_MACHINE;
if OpenKey('SYSTEM\CurrentControlSet\Services\'+Name,false) then
begin
WriteString('Description',Desc);
end;
CloseKey;
end;
finally
reg.Free;
end;end;procedure TEDSClientMonitorService.MonitorTimer(Sender: TObject);
var
f: string;
hh, MI: string;
nowtime: TDatetime;
i: integer;
begin
Monitor.Enabled := False;
Monitor.Interval := 1000;
try
nowtime := now;
DateTimeToString(hh, 'hh', nowtime);
DateTimeToString(mi, 'nn', nowtime);
//每天定时重启
if (hh = '13') and (mi = '30') then
begin
Sleep(60000);
ReStart();
end
else
begin
//超过一天自动重启
if round((Gettickcount - Fcur) / 1000) > 86400 then
begin
Sleep(60000);
ReStart();
exit;
end;
if ReadMsg = 'Cose' then
begin
Wlog('用户调用关闭服务');
exit;
end;
i := FindProcessID(FLocalFileName); f := ExtractFilePath(ParamStr(0)) + FLocalFileName;
if i = 0 then
begin
LaunchAppIntoDifferentSession(Pchar(f));
end
else
begin
if i > 1 then
begin
EnableDebugPrivilege;
KillTask(FLocalFileName);
LaunchAppIntoDifferentSession(Pchar(f));
//RefreshTrayIcon();
RemoveDeadIcons();
end;
end;
end;
finally
Monitor.Enabled := True;
end;
end;end
上一个没贴全