?? writing nt service using vb6-vb5.htm
字號:
way to make the events in Event Viewer show up as the APP name
instead of VBRuntime?<BR><SPAN class=A>A:</SPAN> Of course, yes. For
example, see the solution in <A
href="http://www.fawcette.com/archives/premier/mgznarch/vbpj/2001/09sep01/lj0109/lj0109-1.asp"
target=_blank>this article</A>. The code for the article you can
download <A
href="http://mvps.org/st-software/Code/WriteEventLogsCode_Mod1.zip"
target=_blank>here</A>.<BR><BR><SPAN class=Q>Q:</SPAN> I have run
the ServiceSampleControl.vbp and installed the service, and then
tried starting the service but it didn't not start. When running the
ServiceSample.vbp the error states that it must be started as a
service.<BR><SPAN class=A>A:</SPAN> Did you try starting
SvSampleControl.exe? By this sample design, the service program
(SvSample.exe) must be in the same directory as the control program
(SvSampleControl.exe) or its project (ServiceSampleControl.vbp), if
you prefer starting it in IDE. Because the project of the control
program located in the separate directory, the service executable
was not found. See System event log - this fact must be reflected
there. Of course, this behaviour may be changed by rewriting some
bytes in the control program.<BR><BR><SPAN class=Q>Q:</SPAN> Is it
possible in to turn on "Allow service to interact with
Desktop"?.<BR><SPAN class=A>A:</SPAN> Yes, it is possible. When you
call CreateService or ChangeServiceConfig functions, pass this value
to dwServiceType parameter: <BR><PRE>SERVICE_WIN32_OWN_PROCESS Or SERVICE_INTERACTIVE_PROCESS
Const SERVICE_WIN32_OWN_PROCESS = &h10&
Const SERVICE_INTERACTIVE_PROCESS = &h100&
</PRE><SPAN class=Q>Q:</SPAN> Is it possible to start a service as
another user (administrator) with "Interact with Desktop"?<BR><SPAN
class=A>A:</SPAN> No, it is impossible, only LocalSystem services
can interact with desktop. It is security feature.<BR>Usually in
this situation programmers divide an application into two parts: the
service running under some user account with sufficient rights
provides main work and network communication; and ordinary
auto-start executable provides interaction with current user. These
two parts interact each other using one of known methods: COM, named
pipes, shared memory etc. Using any of these methods is not simple
in conformity with services because they must be modified to
overcome security barriers.<BR><BR><SPAN class=Q>Q:</SPAN> Have you
noticed any problems with using your service with local DDE
application? When that service is running, local DDE application
(and some installs written with InstallShield) hang. The process
exists on the machine, but the exe does not run. As soon as I issue
a STOP to the serivce, the local DDE application begins.<BR><SPAN
class=A>A:</SPAN> Try to replace all wait functions
(WaitForSingleObject and others) with my non-blocking equivalent
function <A
href="http://www.smsoft.ru/en/vbwait.htm">MsgWaitObj</A>. Using
blocking versions of wait functions in VB program is not correct
even if the program have no visible interface (don't touch
WaitForSingleObject in NTService.bas: using this function in service
thread is lawful, but DoEvents from MsgWaitObj may be not
thread-safe, and used there API functions are not declared in type
library).<BR><BR>Note: updated source code of service sample is
available for downloading, it takes into account above-mentioned
considerations.<BR><BR><SPAN class=Q>Q:</SPAN> I want to customize
your "NT Service module" application for running my .exe as a NT
service. Is there anything I should worry about during
customization?<BR><SPAN class=A>A:</SPAN> The first rule for
service: it must respond to Stop request as soon as possible. If
your program performs long operations, insert intermediate checks
for hStopPendingEvent event: <PRE>If WaitForSingleObject(hStopPendingEvent, 0&) = 0& Then ... cancel operation
</PRE>The next rule: initialization code must be brief. That is the
program must execute <PRE>SetServiceState SERVICE_RUNNING
</PRE>line within 1-2 seconds.<BR><BR>The sample introduces two
different approaches in service design.<BR>1. The program uses
objects which respond to external events (for example, you are
writing Internet server, which responds to data arrival in socket).
You have initialization code, termination code and no code in main
loop. The interval for main loop you may set to INFINITE.<BR>2. Your
program performs periodic tasks (for example, you are writing time
scheduler). You have no initialization code (or very brief
assignment initial values for variables at that place), no
termination code and all functional code in main loop.<BR>Of course,
you may combine these approaches.<BR><BR>The next thing is security.
If the service runs in LocalSystem account, it can't access network
shares at all, if it runs in some User account, it can access secure
objects in accordance with User rights. If your service is COM
server, you must use DCOM to access it from user's application even
from the same computer. You must add and modify some registry
settings to make COM service work properly.<BR><BR><SPAN
class=Q>Q:</SPAN> I try to write some value to the registry
(HKCU\...) from the service, but registry editor states that nothing
was written.<BR><SPAN class=A>A:</SPAN> Your problem is, the service
runs in context of another account (typically, "LocalSystem"). That
is, HKCU for your interactive session and for service point to
different registry hives. Use "HKLM\Software\Your Company\Your
Program" key to store data instead. Or start your service in context
of your account (it is adjustable in Control
Panel\Services).<BR><BR></TD></TR>
<TR>
<TD class=WindowBackground>
<TABLE class=Combo summary="">
<TBODY>
<TR>
<TD class=ComboPart>
<TABLE class=ComboPart1 summary="">
<TBODY>
<TR>
<TD class=ComboBorderH colSpan=5></TD></TR>
<TR>
<TD class=ComboBorderV></TD>
<TD class=ComboImage><IMG height=16 alt="" hspace=2
src="Writing NT service using VB6-VB5.files/save.gif"
width=16 align=top vspace=1 border=0></TD>
<TD class=ComboText><A name=Download>Download source
code</A></TD>
<TD class=ComboImage><IMG height=19 alt="" hspace=0
src="Writing NT service using VB6-VB5.files/comboarrow.gif"
width=17 align=right border=0></TD>
<TD class=ComboBorderV></TD></TR>
<TR>
<TD class=ComboBorderH
colSpan=5></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD class=ComboPart>
<TABLE class=ComboPart1 summary="">
<TBODY>
<TR>
<TD class=ComboBorderHD colSpan=4></TD></TR>
<TR>
<TD class=ComboBorderVD></TD>
<TD class=ComboImage><A
href="http://www.smsoft.ru/files/vb6svc.zip"><IMG
height=16 alt="" hspace=2
src="Writing NT service using VB6-VB5.files/save.gif"
width=16 align=top vspace=1 border=0></A></TD>
<TD class=ComboText><A
href="http://www.smsoft.ru/files/vb6svc.zip">vb6svc.zip</A>
(34 kB, English interface)</TD>
<TD class=ComboBorderVD></TD></TR>
<TR>
<TD class=ComboBorderHD
colSpan=4></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD>
<TD class=FrameR></TD></TR>
<TR>
<TD class=FrameDL></TD>
<TD class=FrameD></TD>
<TD class=FrameDR></TD></TR></TBODY></TABLE>
<P class=Centered><IMG height=20 alt=""
src="Writing NT service using VB6-VB5.files/bar2.gif" width=500></P>
<TABLE class=CaptionTable align=center summary="">
<TBODY>
<TR>
<TD class=FrameUL></TD>
<TD class=FrameU></TD>
<TD class=FrameUR></TD></TR>
<TR>
<TD class=FrameL></TD>
<TD>
<TABLE class=Footnote summary="">
<TBODY>
<TR>
<TD class=FootnoteImage width=24><IMG height=24 alt=""
src="Writing NT service using VB6-VB5.files/refresh.gif" width=24
align=top></TD>
<TD class=FootnoteText noWrap colSpan=6>This page has been updated
last time on</TD>
<TD class=Datestamp noWrap><!-- datestamp:begin -->06-Jun-2004<!-- datestamp:end --></TD></TR><!-- <tr> <td class="FootnoteImage" colspan="6"><img src="/cgi-bin/Count.cgi?df=smsofte.dat&dd=B" align="top" alt=""></td> <td class="FootnoteText">visitors since</td> <td class="Datestamp">30-Nov-1999</td> </tr> -->
<TR>
<TD class=CopyrightText noWrap colSpan=8>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -