function TTransparentForm.InstallService(ServiceName, DisplayName, FileName: string): boolean;
var
  SCManager , Service : THandle  ;
  Args  : pchar;
  TempServiceStatus      : TServiceStatus;
begin
  Result := False;
  SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
      if SCManager = 0 then Exit;
      try
          Service := CreateService(
                SCManager,                  
                PChar(ServiceName),         
                PChar(DisplayName),        
                SERVICE_ALL_ACCESS,   
                SERVICE_WIN32_OWN_PROCESS, 
                SERVICE_AUTO_START,     
                SERVICE_ERROR_IGNORE,      
                PChar(FileName),       
                nil,  nil,  nil,   nil,   nil                       
                                  );        Args := nil;
        //sleep(1000);
        Service := OpenService(SCManager, PChar(ServiceName), SERVICE_ALL_ACCESS);
        if  Service <> 0 then
        begin
        StartService(Service, 0, Args);
        CloseServiceHandle(Service);
        end ;
      finally
      CloseServiceHandle(SCManager);
      end;
      Result := True;
刚一开始在启动时要报1053错误!但是我用delphi自代的代码就没有问题!后来也不知道为什么可以启动了,现在又看不见主窗体了!
我在主窗体中加入了TrayIcon这个控件的,听说这样子服务启动时才看的见主窗体!
现在却看不见不知道是为什么!!!(我在SCM中启动服务)测试时:
begin
        StartService(Service, 0, Args);
        CloseServiceHandle(Service);
end ;
还是启动不了!
在  StartService(Service, 0, Args); 这一句停好久就是启动不了!

解决方案 »

  1.   

    我把 CreateService()中的
    SERVICE_WIN32_OWN_PROCESS 改为:SERVICE_INTERACTIVE_PROCESS就连服务也建不了的了!代码中加入了测试代码
    发现系统提示参数有错!
      

  2.   

    我在主窗体中加入了TrayIcon这个控件的,听说这样子服务启动时才看的见主窗体!
    不是吧,TrayIcon控件是用来最小化的
      

  3.   

    看不懂,可能是因为代码不全,楼主有实现ServiceMain吗?有实现RegisterServiceCtrlHandler吗?我觉得除非特殊情况,还是用Delphi的服务类算了,多简单啊:)
      

  4.   

    TrayIcon是个托盘控件  
    你如果写的服务的话要去服务里手动启动它啊  
    不然就得用一个服务类来管理你的服务
      

  5.   

    我用TService既实现过exe服务,也实现过dll服务,你说它通用不?如果要与桌面交互,需要SERVICE_WIN32_OWN_PROCESS or SERVICE_INTERACTIVE_PROCESS我不知道你有没有实现我上面说的,也看不到你的关键代码,所以没法说下去了。
      

  6.   

    楼主有实现ServiceMain吗?有实现RegisterServiceCtrlHandler吗?
    实现了这两个程序就可以做为服务启动了吗?