您的位置:首页 > 其它

创建一个既可作为普通exe(双击能运行),又可作为服务运行的程序。

2009-09-13 11:56 351 查看
由于后台服务和普通程序的运行方式有很多差异,如服务必须从服务控制管理器的上下文中运行而不能直接在explorer中双击鼠标运行。服务程序默认不与桌面交互。

那可否写一个exe既能直接双击鼠标运行,又能作为服务运行呢。

先来看看dpr文件的内容。d7生成的服务工程文件,是不是很眼熟?

Application.Initialize;
Application.CreateForm(Tufidaserver, ufidaserver);
Application.Run;

要注意的是,服务的application来自SvcMgr,和来自forms的application是不同的。

那么,对dpr文件稍做改动,就能实现普通程序和服务程序一体了。

如果想根据付进程来判断运行方式,可在dpr文件内判断父进程是否explorer。

这里是判断是否有参数传入,无参数时作为服务运行。

需要在注册表的服务映像地址改为带参数的,如 I:/代码/订单流程备份服务/jstbackupserver.exe -pp

program jstserver;

uses
SvcMgr,Forms,windows,
Unit1 in 'Unit1.pas' {ufidaserver: TService},

Unit2 in 'Unit2.pas' {Form2};

//{$R *.RES}

begin

if ParamCount>0 then
begin
SvcMgr.Application.Initialize;
SvcMgr.Application.CreateForm(Tufidaserver, ufidaserver);
SvcMgr.Application.Run;
end else begin
Forms.Application.Initialize;
Forms.Application.Title := 'server';

Forms.Application.CreateForm(TForm2, Form2);
Forms.Application.Run;
end;
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐