关于NT的SERVICE(30分)

Q

QQ

Unregistered / Unconfirmed
GUEST, unregistred user!
TO CJ:现在把分数还给你!
以下为CJ的题目:
如何制作NT的SERVICE?
 
Hehe, So little points, :)

In general, a NT service is a program that can communicate with Service
Control Manager.
Delphi 4 provide simple steps to build a NT service.

I'll give an example soon. Please wait for me. :)
 
我有一个这样的例子,如果需要可以e-mail给斑竹。
NT Service

The NT Service for Delphi 3 download contains a component which encapsulates NT
services, and a demo service. The component produces neat multithreaded, self
installing and uninstalling services. The source code is heavily commented.


(*========================================================================*
| NT Service unit |
| |
| Encapsulates an NT service that supports the following interface. |
| |
| Copyright (c) Colin Wilson 1997. All rights reserved. |
| |
| Command line parameters: |
| -i Install the service |
| -d De-install the service |
| -l Turn on logging (must be before '-n') |
| -n Run in non-service mode (for debugging) |
| |
| Service Startup Parameters: |
| -l Turn on logging |
| |
| The following protected functions can be overridden. |
| |
| function PreRegisterInitialize : boolean; |
| |
| Override this to perform pre-registration processing. This must not |
| take more than a couple of milliseconds. Return false to prevent |
| the service from running. |
| |
| function PostRegisterInitialize : boolean; |
| |
| Override this to perform processing that should happen before the |
| main thread starts. |
| |
| function Execute : Integer; |
| |
| Override this to perform processing for the service. |
| This main processing must check the Terminated flag, or the return |
| value of ExecuteDelay, often, and finish when asked. The function |
| can return an Exit Code for the service. |
| |
| procedure Installed; |
| |
| Override this to perform post installation processing. |
| |
| procedure Deleted; |
| |
| Override this to perform post deletion processing. |
| |
| The following functions and properties can be used from within the |
| above overridden functions: |
| |
| property RegistryRoot : string read GetRegistryRoot; |
| |
| Returns the root key for the service entry in the registry. |
| |
| function ExecuteDelay (nMilliseconds : Integer) : boolean; |
| |
| Delay for the specified time in milliseconds, or until the service |
| is asked to shut down. The function returns False if the service |
| should terminate. |
| |
| property TerminateEvent : THandle read fTerminatEvent; |
| |
| This event gets signalled when the service should terminate. It can |
| be used in more advanced services (With a WaitForMultipleObjects |
| call, for instance). |
*==============================================================
 
不好意思,这个问题是QQ还给我的,所以请MAIL给我!多谢!!多谢!!!
cjcjc@online.sh.cn
To pegasus: If you do not give the example soon, Though just 30 points
but perhaps it will belong jiangtao!
To jiangtao: Please mail me this unit, Thank you!
 
不好意思,这个问题是QQ还给我的,所以请MAIL给我!多谢!!多谢!!!
cjcjc@online.sh.cn
To pegasus: If you do not give the example soon, Though just 30 points
but perhaps it will belong jiangtao!
To jiangtao: Please mail me this unit, Thank you!
 
请下载来自jiangtao的 <a href=/delphi/attachments/ntservice.zip>NTService.zip</a>.
 
不好意思:
如果立刻就要的话, 我可以提供给您一个极简单的例子:
CuteClock: 每当正点或者半点的时候, 会鸣响报时, 就像家里的
老挂钟, :)
非常简单地演示了NTService的原理, 我是用Visual C++编写的,
但是可以马上改成Delphi4的代码, 不知道.EXE会不会变得很大, :)
 
本来我是想借此机会介绍一下NT Service的原理的, 但是既然
jiangtao大侠提供了这么方便的控件, 大家如果不想知其所以然的话,
还是使用这个控件得了, :)
 
过去曾用VC做过NT Service,其目的是为了做一个Chat Server;
过程非常繁琐.而且我认为实现的方法也比较死板。

通常一个Server都有这样三个函数:
1.一个Main函数,在Main函数中主要工作就是执行一个函数StartServiceCtrlDispatcher,其目的是为了把你的ServiceMain函数的指针
传递给NT的SCM(Service Control Manager,你可以在NT的控制面板中
找到它)。这样NT就可以通过SCM来启动、停止、暂停你的程序了。
2、一个ServiceMain函数,主要工作就是登记你的Service控制句柄和做一些
初始化的工作,并且一般就是在这个函数中启动你的服务。例如启动一个定时
报时的线程
3、一个Control Handler函数,简单的讲,就是实现NT Service的启动、停止、
暂停这些动作

要想对NT Service编程了解得多一些,最好是看一下VC的帮助,这部分内容在VC
帮助的位置是:Platform SDK下的Windows Base Services下的Executables下的
Services中。

上面说了这么多废话,最重要的差点忘了说:Delphi4下可以用比较简单的方法来实现Service了!哈哈,这是Delphi4的新功能。
Delphi4中有个TService类,你可以直接New一个Service Application。不需要
使用什么控件。当然,真正做起来也不是那么简单的。
 
pegasus大虾抱歉!我以为你有现成的,没想到...
我当然想知道原理!谢谢!!
 
那太好啦, 我做的例子还含有一个控制您自己的Service的程序:
启动, 暂停, 或者停止. 大概星期天或者下个星期一交卷, 因为周末我们同学聚会出去玩, :)
 
pegasus 玩的开不开心?
pegasus大侠,到时候MAIL给我,因为是我的分数!
最好程序有详细注释!(30分要求高了点)
 
>pegasus 玩的开不开心?
It's a rainy day yesterday, we canceled the trip, :(.
>pegasus大侠,到时候MAIL给我,因为是我的分数!
>最好程序有详细注释!(30分要求高了点)
Sorry, I had not finished it in time. Just done half of it. (Start and stop the service, :)
But I'm sure I can provide it the day after tomorrow.,I'll give you the program with
comments in detail
You can give the score to JiangTao and dubhe. 我无所谓, "就到这里, 就到这里... " 知足常乐嘛! *-^
 
pegasus:
Do you know why the quession still here?
Be hurry please! Thanks!
 
去vcl.vclxx.com有范例程序
 
I not find it!!
Now I will end the quession:
pegasus: 15 //I beleive you so i give the score if you want more
please let me know! I will be right here waiting for you!
jiangtao: 15 //for your NTService.zip
 
Hehe, I do not want too much score.
I'm only shame that I have not written the example in time.

So you do not need give me any point. But I'll write the example
for you this week, :)
 
THANKS pegasus
WHERE IS QQ
THE SCORE BELONG TO pegasus
 
多人接受答案了。
 
顶部