?? writing nt service using vb6-vb5.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0037)http://www.smsoft.ru/en/ntservice.htm -->
<HTML><HEAD><TITLE>Writing NT service using VB6/VB5</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1"><LINK
href="Writing NT service using VB6-VB5.files/common.css" type=text/css
rel=stylesheet>
<STYLE type=text/css>SPAN.Keyword {
FONT-WEIGHT: bold; COLOR: #000080
}
SPAN.FunctionName {
FONT-WEIGHT: bold; COLOR: #800000
}
SPAN.QA {
FONT-SIZE: 16pt
}
SPAN.Q {
FONT-SIZE: 16pt; COLOR: #800000
}
SPAN.A {
FONT-SIZE: 16pt; COLOR: #008000
}
</STYLE>
<META content="MSHTML 6.00.2800.1458" name=GENERATOR></HEAD>
<BODY>
<TABLE class=HeaderTable summary="">
<TBODY>
<TR>
<TD class=ToHome>
<TABLE class=Button summary="">
<TBODY>
<TR>
<TD class=ButtonL></TD>
<TD class=ButtonM><A href="http://www.smsoft.ru/en/index.htm"><IMG
height=24 alt="" hspace=4
src="Writing NT service using VB6-VB5.files/home.gif" width=24
<TD class=ToHomeCaption noWrap><A
href="http://www.smsoft.ru/en/index.htm">To Main Page</A></TD>
<TD class=ButtonR></TD></TR></TBODY></TABLE></TD>
<TD width="100%"></TD>
<TD class=ViewIn noWrap>
<P class=Small><A href="http://www.smsoft.ru/ru/ntservice.htm"
target=_top><IMG height=20 alt=""
src="Writing NT service using VB6-VB5.files/russian.gif" width=32
align=top vspace=6 border=0></A><BR><A
href="http://www.smsoft.ru/ru/ntservice.htm" target=_top>View in
Russian</A></P></TD></TR></TBODY></TABLE><BR>
<TABLE class=ContentTable summary="">
<TBODY>
<TR>
<TD class=CaptionLL></TD>
<TD class=Caption>
<TABLE class=ContentTable summary="">
<TBODY>
<TR>
<TD class=CaptionImage><IMG height=32 alt="" hspace=0
src="Writing NT service using VB6-VB5.files/bas.gif" width=32
border=0></TD>
<TD class=CaptionText noWrap>Writing NT service using VB6/VB5</TD>
<TD class=CaptionNote></TD>
<TD class=CaptionR></TD></TR></TBODY></TABLE></TD>
<TD class=CaptionRR></TD></TR>
<TR>
<TD class=FrameUUL></TD>
<TD class=FrameUU></TD>
<TD class=FrameUUR></TD></TR>
<TR>
<TD class=FrameL></TD>
<TD class=Window>
<TABLE class=Contents summary="">
<TBODY>
<TR>
<TD class=WindowText>
<P>It is known, that Visual Basic isn't most appropriate tool for
developing of Windows NT/2000/XP services. The problem is, for
service development is necessary to use API function <SPAN
class=FunctionName>CreateThread</SPAN>, which is not supported
nether in VB5, nor in VB6. VB allows creation of multi-thread
programs, but not using <SPAN class=FunctionName>CreateThread</SPAN>
function. VB5 utilizes <SPAN class=Keyword>Err</SPAN> object, common
for all threads created by <SPAN
class=FunctionName>CreateThread</SPAN> function. It is inadmissible,
because values of this object may be changed from different threads
in unpredictable order, and ever simultaneously on multi-processor
computers. On the contrary, VB6 uses Thread Local Storage (TLS) for
<SPAN class=Keyword>Err</SPAN> object (and not only for it), but TLS
isn't initialized when <SPAN class=FunctionName>CreateThread</SPAN>
function creates new thread, and therefore, after simple
recompilation of the program in VB6 it doesn't work at all (rather,
it works only being compiled in p-code with common <SPAN
class=Keyword>Err</SPAN> object).</P>
<P>Matthew Curland suggested rather complicated method of threads
creation in VB (see links), but for purpose of service development
more simple solution may be applied. The program will work without
TLS, if in new thread used only arithmetic operators and API
functions, declared in type library, and <SPAN
class=Keyword>Err</SPAN> object isn't used at all. It is well known
that the properties of <SPAN class=Keyword>Err</SPAN> object became
changed in two cases: when occurs Run-Time error (<SPAN
class=Keyword>Err.Number</SPAN>) and after every API call (<SPAN
class=Keyword>Err.LastDllError</SPAN>). The first may be avoided by
writing error-free code, the second - by declaring API calls in Type
Library (API functions declarations must not contain <SPAN
class=FunctionName>usesgetlasterror</SPAN> attributes, therefore
type libraries from the popular Bruce McKinney's book are not
suitable). Functional part of the service must work in main thread,
and above limitations doesn't apply to it. This approach must work
both in VB6 and VB5.</P>
<P>Of course, you can use free NTSVC.OCX control from Microsoft to
create your service, and this way is simple and reliable, but it has
one disadvantage: you can't set "Unattended execution" option for
it. You can't use any OCX controls when "Unattended execution" is
set while this mode is preferable for service. In addition, because
this control distributed in form of source code, there are some
incompatible compiled versions of it.</P>
<P>This sample is written using VB6 without any external components.
As service is compiled for unattended execution, it has no visual
interface. Use Event Viewer to read messages written by the service
to the Application Log.</P>
<P>The functional part of service (which is absent in this sample)
must be driven by the events from Service Dispatcher. All events
must be processed during few seconds, otherwise the service will be
unable to respond on requests from Service Dispatcher.</P>
<P>See also:</P>
<P>Microsoft Knowledge Base <A
href="http://support.microsoft.com/support/kb/articles/Q137/8/90.ASP"
target=_blank>Q137890</A>, <A
href="http://support.microsoft.com/support/kb/articles/Q170/8/83.ASP"
target=_blank>Q170883</A>, <A
href="http://support.microsoft.com/support/kb/articles/Q175/9/48.ASP"
target=_blank>Q175948</A>,</P>
<P><A
href="http://msdn.microsoft.com/library/techart/msdn_ntsrvocx.htm"
target=_blank>An OLE Control for Creating Win32 Services in Visual
Basic,</A></P>
<P><A href="http://www.microsoft.com/msj/0298/service.htm"
target=_blank>Manipulate Windows NT Services by Writing a Service
Control Program,</A></P>
<P><A
href="http://www.pinnaclepublishing.com/VB/VBmag.nsf/0/2496CE0A783BE738852568E000549E0B"
target=_blank>Creating an Agent NT Service with VB,</A></P>
<P><A href="http://vbwire.com/advanced/howto/service.asp"
target=_blank>How-To Run Your Application as a Service,</A></P>
<P><A href="http://vbwire.com/advanced/howto/service2.asp"
target=_blank>How-To Run Your Application as a Service - Part
II,</A></P>
<P>Matthew Curland's article <A
href="http://www.fawcette.com/archives/premier/mgznarch/vbpj/1999/06jun99/bb0699.pdf"
target=_blank>"Create Worker Threads in DLLs"</A> in June/1999 issue
of <A href="http://www.fawcette.com/vsm" target=_blank>"Visual Basic
Programmer's Journal"</A>.</P></TD></TR>
<TR>
<TD class=WindowText
style="BORDER-TOP: #9d9da1 1px solid; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; TEXT-ALIGN: left; BORDER-RIGHT-WIDTH: 0px"><BR><SPAN
class=QA>Updates of source code:</SPAN><BR><BR><U>06 June
2004</U><BR><BR>1. All API functions calls except GetVersionEx
changed to Unicode versions, in code and in type library. Added new
Enum members to type library to support new Windows 2000 control
codes.<BR><BR>2. Added MsgWaitObj function to prevent blocking of
messages processing. All calls of WaitForSingleObject and
WaitForMultipleObjects in Sub Main replaced with MsgWaitObj.</TD></TR>
<TR>
<TD class=WindowText
style="BORDER-TOP: #9d9da1 1px solid; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; TEXT-ALIGN: left; BORDER-RIGHT-WIDTH: 0px"><BR><SPAN
class=Q>Questions</SPAN> <SPAN class=QA>and</SPAN> <SPAN
class=A>Answers:</SPAN><BR><BR><SPAN class=Q>Q:</SPAN> Is there any
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -