📄 nt-service.shtml.htm
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Zafir Anjum">
<TITLE>A Class For Building An NT Service</TITLE>
</HEAD>
<body background="../fancyhome/back.gif" tppabs="http://www.codeguru.com/fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000" bgproperties="fixed">
<table WIDTH="100%">
<tr WIDTH="100%">
<td><td>
</tr>
</table>
<CENTER><H3><FONT COLOR="#AOAO99">A Class For Building An NT Service</FONT></H3></CENTER>
<HR>
This article was contributed by <A HREF="mailto:Joerg.Koenig@rhein-neckar.de">Joerg
Koenig</A>.
<P>Ever tried to write an NT service ? :->
<BR>One of my very latest projects was to develop a CORBA server running
as an NT service. Since one of my next projects will be an NT service too,
I let my brain work a little harder to get it ready for as many programs
as needed. The result of this was the CNTService class, that I want to
share with the codegurus :-)
<BR>With this class it is very simple to create an NT service. Just derive
your own class and override the "Run()" and "Stop()" pure virtual members.
All of the hard bits are done by the class.
<P>There are several virtuals to overload (multi-threading, pausable services
and more).
<P>The class accepts a set of command-line parameters. For instance you
can pass the -d switch to let it run as a console process. This makes debugging
much easier.
<BR>Furthermore the class has abilities to (de)install the service. This
is usually a command-line switch too (but one can customize this :-).
<P>CNTService should be able to run as a unicode service too, but this
is still untested (would be nice to hear of your experiences ... :-)
<P><B><FONT SIZE=+1>Building a Service</FONT></B>
<BR>Now lets have a closer look at how you can build your own service.
<BR>Follow these steps (assuming you're using VC++ 5.0):
<OL>
<LI>
create a new project workspace of type "console application"</LI>
<LI>
copy the files of the NTService package into the directory of your new
project</LI>
<LI>
insert "NTService.cpp" and "NTServiceEventLogMsg.mc" into your project</LI>
<LI>
open the <B>Project->Settings ...</B> dialog and fill it out. See the image
below. It says more than I could write :-)</LI>
<LI>
compile the file "NTServiceEventLogMsg.mc". This will produce the files
"NTServiceEventLogMsg.h" and "NTServiceEventLogMsg.rc".</LI>
<LI>
insert the generated RC file into your project.</LI>
<LI>
derive your own class from CNTService and override at least the two methods
"Run()" and "Stop()"</LI>
<LI>
write a simple "main()" to start up your program's functionality :) (You
can use the sample project for a good starting point)</LI>
<LI>
Once your project compiles without errors, you can immediately start it.
To do so, you should open the <B>Project->Settings ...</B> dialog once
again. Select <B>All Configurations</B> and than check the line with the
name of your project. Activate the tab <B>Debug</B> and insert the <B>-d
</B>switch in the field <B>Program arguments:</B></LI>
<BR>Now you should be able to run and debug your service as a console program.
You can use either Ctrl-C or Ctrl-Break to stop the service (that would
simulate a <I>Stop</I> request from the service control manager)</OL>
<IMG SRC="ntservice_settings.gif" tppabs="http://www.codeguru.com/misc/ntservice_settings.gif" HEIGHT=372 WIDTH=577>
<BR><FONT SIZE=-1>How to fill out the settings of the file NTServiceEventLogMsg.mc</FONT>
<P>Now lets try to start the service as a real NT service (assuming you
use the sample project):
<OL>
<LI>
Log on as an administrator, if your account isn't in the administrators
group (this will make life somewhat easier)</LI>
<LI>
Start the program with the switch <B>-i</B>. This will install your service.</LI>
<LI>
If the last step completes successfully, you can open the control panel
and start the <B>Services</B> applet. Search for the display name of your
service and select it (<I>Very Simple Service</I> if you try the sample).</LI>
<LI>
Press the <B>Startup ...</B> button. Have a look at the <B>Log On As:</B>
box. For now you can let the check at <B>System Account</B>. If you try
the sample, make sure you check the <B>Interact With The Desktop</B> box.
This is necessary, since the sample uses the MessageBox() function (which
interacts with the desktop). If you don't check this box, the sample will
not run properly and will hang up itself; thus you're unable to stop the
service again ! With a little bit of experience one can enhance the sample,
so that it will install the service with this box already checked, but
I wanted to keep the sample as simple as possible, so I leave it to you
to implement that. :-)</LI>
</OL>
The sample project implements a very simple service. The derived class
(declaration and implementation) and the main() function are all in the
same file (main.cpp). This file is less than 100 lines long (strip off
all the comments and it will fit on one page of printing !)
<BR>The service only pops up a message-box every 10 seconds.
<P>For more information have a look at CNTService.h and - of course - the
sample. I think it is well commented (let <A HREF="mailto:Joerg.Koenig@rhein-neckar.de">me</A>
know, if you don't think so)
<P>CNTService consists of three files:
<BR>NTService.h
<BR>NTService.cpp
<BR>NTServiceEventLogMsg.mc
<BR><A HREF="ntservice_source.zip" tppabs="http://www.codeguru.com/misc/ntservice_source.zip">Download Source</A> 11KB
<BR><A HREF="ntservice_sample_project.zip" tppabs="http://www.codeguru.com/misc/ntservice_sample_project.zip">Download Sample Project</A> 15KB
<P>Note that the file NTServiceEventLogMsg.mc is copyrighted by Telic
Software International B.V. Many thanks to Telic for providing it in the
public domain !
<P>
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="../index.htm" tppabs="http://www.codeguru.com/">Goto HomePage</A></FONT></TD>
<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>© 1997 Zafir Anjum</FONT> </CENTER>
</TD>
<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@home.com">zafir@home.com</A> </FONT></DIV>
</TD>
</TR>
</TABLE>
<CENTER><IMG SRC="../cgi/Count.cgi-ft=2&dd=E-df=misc_no_window_list.cnt" tppabs="http://www.codeguru.com/cgi/Count.cgi?ft=2&dd=E%7cdf=misc_no_window_list.cnt" ALIGN="BOTTOM" BORDER="0"></CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -