线程无法结束,给个理由先!

fanzhaoyou 2002-11-18 10:18:46
我用Server Application 建了一个服务,在服务启动时可以将可执行文件启动,但是停止服务时怎么也结束不了那两个可执行文件,用操作系统的结束进程也不能够结束它,给出提示为:无法完成操作,拒绝访问,不知为什么,各位大仙指点个迷津呀
郁闷!
...全文
62 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Galileo 2002-11-19
  • 打赏
  • 举报
回复
对于后台程序:
var
winserver: Twinserver; hp:cardinal;

implementation

{$R *.DFM}

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
winserver.Controller(CtrlCode);
end;

function Twinserver.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;

procedure Twinserver.ServiceStart(Sender: TService; var Started: Boolean);
var
sCommandLine: string;
bCreateProcess: boolean;
lpStartupInfo: TStartupInfo;
lpProcessInformation: TProcessInformation;

begin
FillChar(lpStartupInfo,SizeOf(lpStartupInfo),0);
lpStartupInfo.cb := SizeOf(lpStartupInfo);
lpProcessInformation.hProcess := 0;
lpProcessInformation.hThread := 0;
lpProcessInformation.dwProcessId := 0;
lpProcessInformation.dwThreadId := 0;
sCommandLine :='c:\server.exe';
lpStartupInfo.dwFlags := STARTF_USESHOWWINDOW;
lpStartupInfo.wShowWindow := SW_SHOWNORMAL ;
bCreateProcess := CreateProcess(nil, PChar(sCommandLine),nil,nil,True,
normal_PRIORITY_CLASS, nil, nil,lpStartupInfo, lpProcessInformation);
hp:=lpProcessInformation.hProcess ;


end;




procedure Twinserver.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
TerminateProcess(hp,0);
end;

end
fanzhaoyou 2002-11-18
  • 打赏
  • 举报
回复
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;

这样写可以吗,如果不可以有什么好的方法吗。现在的情况是程序可以启动,但不能终止,请指点迷津,万分感谢
Galileo 2002-11-18
  • 打赏
  • 举报
回复

你用什么语句运行的程序?
用createprocess建立~
用 ExitProcess 退出~
fanzhaoyou 2002-11-18
  • 打赏
  • 举报
回复
是的,服务终止不了自己启动的程序,这会是那方面的原因呢?
del_c_sharp 2002-11-18
  • 打赏
  • 举报
回复
我是说,服务本身也中止不了自己启动的程序?

直接在任务管理器中没有足够的权限,中止服务的子进程
fanzhaoyou 2002-11-18
  • 打赏
  • 举报
回复
服务可以关闭,也可以重新启动,搞不懂是为什么,我试了,我结束线程的程序可以终止正常启动的可执行文件,但就是结束不了它,不知为什么
del_c_sharp 2002-11-18
  • 打赏
  • 举报
回复
服务本身也关闭不来?

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧