哪位大侠指点一下在win2k如何下如何编写服务程序?请举个例子说明。可以加分。谢谢!

Tod 2001-06-27 10:18:18
...全文
169 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
david_ye 2001-07-12
  • 打赏
  • 举报
回复
To:Tod(菜鸟) 
我前一个月就发过这个贴子了,还被版主臭骂一顿,
到一前去找找。
Crob讲的很明确了。
Tod 2001-07-12
  • 打赏
  • 举报
回复
我回头试试,行的话马上给大虾加分,并表示万分感激!!
copy_paste 2001-07-12
  • 打赏
  • 举报
回复
看看先。

//工程文件
program Project1;

uses
Windows, SvcMgr, WinSvc, Forms, SysUtils,
Unit1 in 'Unit1.pas' {Service1: TService},
Unit2 in 'Unit2.pas' {Form2};

{$R *.RES}

const
SServiceName = 'MyApplication';
{是否install, or uninstall}
function Installing: Boolean;
begin
Result := FindCmdLineSwitch('INSTALL',['-','\','/'], True) or
FindCmdLineSwitch('UNINSTALL',['-','\','/'], True);
end;

function StartService: Boolean;
var
Mgr, Svc: Integer;
begin
Result := False;
Mgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if Mgr <> 0 then
begin //这函数就像在“服务”中启动某个服务一样的作用。
Svc := OpenService(Mgr, PChar(SServiceName), SERVICE_ALL_ACCESS);
Result := Svc <> 0;
CloseServiceHandle(Mgr);
end;
end;

begin
if not Installing then
begin{如果有本程序实例,退出}
CreateMutex(nil, True, 'MyService');
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, 'Already Running', 'Error', MB_ICONERROR);
Halt;
end;
end;
//如果是出现在“服务”中而且是自动启动,则是下面情况
if Installing and StartService then
begin
SvcMgr.Application.Initialize;
Service1 := TService1.CreateNew(SvcMgr.Application, 0);
SvcMgr.Application.CreateForm(TForm2, Form2);
SvcMgr.Application.Run;
end
else{否则是以Explorer, 双击来启动本程序,则是这种情况}
begin
Forms.Application.Initialize;
Application.CreateForm(TForm2, Form2);
Form2.Initialize;
Application.Run;
end;
end.

//TService服务单元(menus->new->Service Application)
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs;

type
TService1 = class(TService)
procedure ServiceStart(Sender: TService; var Started: Boolean);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;

var
Service1: TService1;

implementation

uses Unit2;
{$R *.DFM}

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

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

procedure TService1.ServiceStart(Sender: TService; var Started: Boolean);
begin {Tell Form2 进行初始工作}
PostMessage(Form2.Handle, WM_Initialize, 0, 0);
end;

procedure TService1.ServiceStop(Sender: TService; var Stopped: Boolean);
begin //退出
PostMessage(Form2.Handle, WM_Quit, 0, 0);
end;

end.

//TService只是做启动用,真正作用的在你的Form实例中,在你的Form中作你的服务。
//TForm2
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

const
WM_Initialize = WM_User + 100;

type
TForm2 = class(TForm)
private
{ Private declarations }
procedure WMStart(var Message: TMessage); message WM_Initialize;
public
procedure Initialize;
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.DFM}

{ TForm2 }

procedure TForm2.Initialize;
begin
//Initialize Form all object
end;

//如果是启动“服务”中启动的,这里会有用的。
procedure TForm2.WMStart(var Message: TMessage);
begin
Initialize;
end;

//然后再写其它的服务类或什么的,写在Initialize中
end.

写的比较简单,都是从Scktsrvr.dpr copy过来,你怎么不去看看。
Tod 2001-07-11
  • 打赏
  • 举报
回复
半天还是没说清楚具体怎么写,与写通常的程序有什么差别!都只说一些框框,只是框框是不够的,我不是专家!
Crob 2001-07-01
  • 打赏
  • 举报
回复
File菜单->New下选择Service或Service Application

建议选择使用Service Application建立服务程序。

编译后的执行文件在开始菜单的"运行"中使用以下方法加载和卸载服务:
d:\service_name.exe /install //加载服务程序
d:\service_name.exe /uninstall //卸载服务程序

加载服务后可以在控制面板->管理工具的"服务"中找到该服务。

delphi5编写出的服务程序在winNt和win2k中通用。另外在uses WinSvc;, 在WinSvc中有很多关于控制服务的函数。

很详细了,给分吧
copy_paste 2001-06-30
  • 打赏
  • 举报
回复
Delphi有个VCL程序,那里面你可参考一下。
\delphi5\source\vcl\scktsrvr.dpr
也好像就是TSocketService类中的OnStart, OnShutDown这两个事件,其它的事就像Delphi程序一样用吧
猛禽 2001-06-30
  • 打赏
  • 举报
回复
最完整的例子是那个SCKTSVRV的源程序
Tod 2001-06-30
  • 打赏
  • 举报
回复
我真的泄气了
artgolf 2001-06-29
  • 打赏
  • 举报
回复
我做过,但用vc做的,delphi中的那个太简单,缺少用户自定义控制方法,对空闲触发事件和紧急触发事件也没有例子,而vc中有许多完善的例子,你可以把它改写成delphi的。比如,要实现空闲时每十分钟执行一次任务,当用户定义的紧急事件到来时,立即执行任务,怎么办?记得在msdn中有几个例子。
Tod 2001-06-29
  • 打赏
  • 举报
回复
大家说说啊,怎么没人??
airhorse 2001-06-28
  • 打赏
  • 举报
回复


不是,大家不会,只是做一个服务,而且要调试成功,比较麻烦,其实delphi作的服务已经够简单了,自己试试吧,不要光问不炼,
只等现成的,
你只要注意,(onstart,onshutdown....等)几个事件的处理,以及服务线程的可调度设计,就行了,

还有,可以有界面的,大概是在onCreate事件里处理。多动动手,delphi里有简单的例子的!

Tod 2001-06-28
  • 打赏
  • 举报
回复
提前一次,怎么还没有人回答?????????????????????
Crob 2001-06-27
  • 打赏
  • 举报
回复
在winNt下怎么做在win2k就怎么做,一样的。

guo 2001-06-27
  • 打赏
  • 举报
回复
有界面吗? 是什么服务?
一般做成服务的是一些通信程序.
Tod 2001-06-27
  • 打赏
  • 举报
回复
为什么不能有窗口界面呢?我见过有写服务程序有窗口界面啊?

它的编程方法和普通的程序有什么差别吗?如果有,是什么呢?
guo 2001-06-27
  • 打赏
  • 举报
回复
delphi->new->service开始一个服务程序框架,设置service属性,对afterinstall,beforeinstall,on creaet,ondestory,onpause,onstart,onstop进行编程.
注意:不要有窗口界面
Tod 2001-06-27
  • 打赏
  • 举报
回复
怎么没人?
Tod 2001-06-27
  • 打赏
  • 举报
回复
很难吗?怎么没多少回复??
Tod 2001-06-27
  • 打赏
  • 举报
回复
我NT下没做过,所以不知道
有界面的

5,928

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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