procedure TService1.ServiceStart(Sender: TService; var Started: Boolean);
var
lppe: TProcessEntry32;
found: boolean;
Hand: THandle;
hh, dd: hwnd;
v_found, vs_found: boolean;
begin
found := false;
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
found := Process32First(Hand, lppe);
while found do
begin
if uppercase(StrPas(lppe.szExeFile)) = uppercase('Project_rlzy.ex') then
begin
v_found := true;
break;
end
else
v_found := false;
found := Process32Next(Hand, lppe);
end;
found := Process32First(Hand, lppe);
while found do
begin
if uppercase(StrPas(lppe.szExeFile)) = 'SCKTSRVR.EXE' then
begin
vs_found := true;
break;
end
else
vs_found := false;
found := Process32Next(Hand, lppe);
end;
if v_found = false then
begin
shellExecute(dd, nil, 'C:\Program Files\butone\HR_Server服务器\Project_rlzy.exe', nil, nil, sw_hide);
end;
if vs_found = false then
begin
shellExecute(dd, nil, 'C:\Program Files\butone\HR_Server服务器\SCKTSRVR.EXE', nil, nil, sw_hide);
end;
Started := True;
end;
以上是服务在启动时,调用可执行文件的代码,以下是在服务终止时,结束应用程序的代码:
procedure TService1.ServiceStop(Sender: TService; var Stopped: Boolean);
var
lppe: tprocessentry32;
sshandle: thandle;
hh: hwnd;
Hand: THandle;
found: boolean;
begin
found := false;
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
found := Process32First(Hand, lppe);
while found do
begin
if uppercase(StrPas(lppe.szExeFile)) = uppercase('Project_rlzy.ex') then
begin
hh := OpenProcess(PROCESS_ALL_ACCESS, true, lppe.th32ProcessID);
TerminateProcess(hh, 0);
end;
if uppercase(StrPas(lppe.szExeFile)) = 'SCKTSRVR.EXE' then
begin
hh := OpenProcess(PROCESS_ALL_ACCESS, true, lppe.th32ProcessID);
TerminateProcess(hh, 0);
end;
found := Process32Next(Hand, lppe);
end;
Stopped := True;
end;