我已经做了一个解决方法,我有所有代码不直接与服务控制管理器封装到单独的单元中进行交互。然后我为每个实例编写一个单独的Delphi项目,我想要的服务只有足够的代码启动自己并开始运行主要代码。
这个方法是,在我看来,丑陋肯定是低效的。对于两个实例,它可以正常工作,但是我们需要第三个和第四个...
有什么办法可以修改我的代码,以便我有一个Delphi项目可以通过一些简单的运行时输入(例如命令行标志)安装和运行自己作为多个服务实例?
或者更广泛的问题是:是否有一个正确的方法来完成目标?
解决方案你还没有清楚你在TService中尝试改变的内容
您是否添加了BeforeInstall处理程序?
某些东西:
procedure TServiceMain.ServiceLoadInfo(Sender:TObject); //新方法,而不是覆盖 begin 名称:= ParamStr 2); DisplayName:= ParamStr(3); 结束 程序TServiceMain.ServiceBeforeInstall(Sender:TService); begin ServiceLoadInfo(Self); 结束 procedure TServiceMain.ServiceCreate(Sender:TObject); begin ServiceLoadInfo(Self); 结束如果你经常这样做,子类TService在构造函数中执行thie。
您也应该在BeforeUninstall中执行相同的操作 - 同样的方法将两个事件相结合。
C:\> servicename / install MyService我的服务说明
I have a service application built in Delphi that works great. It does exactly what I want it to do and all is happy. All is fine until I want to run two (or more) instances of that service on a single machine. Since the service name is hard coded into the program (via the Name property of the service), I can only install the service once on any given computer. If I try to modify the Name property at run-time, the service does not respond unless the Name property is set to the same thing that was set during design time.
I have done a workaround for this where I have all of the code that is not interacting directly with the service control manager encapsulated out into separate unit(s). Then I write a separate Delphi project for each instance that I want of the service that has just enough code to launch itself and start running the main code.
This method is, in my opinion, ugly and is certainly inefficient. It works okay for two instances, but then we need a third and a fourth and ...
Is there any way that I can modify my code so that I have just one Delphi project that can install and run itself as multiple service instances with some simple run-time input (e.g. command line flag)?
Or perhaps a broader question: Is there a "right way" to accomplish goal?
解决方案You haven't made it clear what you have tried to change in the TService subclass.
Have you added a "BeforeInstall" handler?
Something like:
procedure TServiceMain.ServiceLoadInfo(Sender : TObject);// new method, not an override begin Name := ParamStr(2); DisplayName := ParamStr(3); end; procedure TServiceMain.ServiceBeforeInstall(Sender: TService); begin ServiceLoadInfo(Self); end; procedure TServiceMain.ServiceCreate(Sender: TObject); begin ServiceLoadInfo(Self); end;If you do this regularly, subclass TService to do thie in the Constructor instead.
You should do the same in the BeforeUninstall as well - point both events at the same method.
C:\>servicename /install MyService "My Service Description"
更多推荐
是否可以安装同一个delphi服务应用程序的多个实例?
发布评论