我用Server Application 建了一个服务,在服务启动时可以将可执行文件启动,但是停止服务时怎么也结束不了那两个可执行文件,用操作系统的结束进程也不能够结束它,给出提示为:无法完成操作,拒绝访问,不知为什么,各位大仙指点个迷津呀
郁闷!

解决方案 »

  1.   


    你用什么语句运行的程序?
    用createprocess建立~
    用 ExitProcess 退出~
      

  2.   

    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;这样写可以吗,如果不可以有什么好的方法吗。现在的情况是程序可以启动,但不能终止,请指点迷津,万分感谢
      

  3.   

    对于后台程序:
    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