First, the NT service program so-called NT service is actually a process that can be automatically started with a certain identity for a long time in a certain identity at a system startup. The FTP Server, HTTP Server, offline printing, etc. are provided in the form of NT services. This actually similar to the UNIX root daemon process. NT services are summarized, and NT services are also the following characteristics:
1. You can start from starting, do not need to be started. This is an important feature for the server. Of course, you can decide whether the service is started from start, and even shielding a service. 2, NT service does not have a user interface, basically similar to a DOS program, because the NT service must run for a long time, so you don't want to have your own interface as a normal Win32 process. But the NT service can interact with the user, which is a special service process. You can see the service process through the NT task manager. 3, NT service to manage, install, start, stop, and removal, etc., all of the SCM (Services Control Manager) interfaces require SCM interface functions. The service controller of the control panel is to use the SCM interface to manage all services in the system. In fact, there are some programs or commands that can control services, Net.exe, Server Manager, etc., SCM.exe, etc. 4, these processes run in a certain identity to facilitate access to server resources. Under normal circumstances, use Localsystem accounts in the domain, this account has complete access rights to most resources (unless special prohibited), which can ensure the "power" of the service program. However, some services use special accounts to run, you can also specifically set a service account. 5, the system is automatically run in a thread mode, in general, but more system resources, this is different from ordinary processes. If the thread mode is not used, the general process often consumes the entire CPU resource. It is generally necessary to exist, and the task that can not consume excessive resource is the most appropriate.
Second, the service control generally believes that writing NT services requires C / C to implement, and VC6 uses ATL wizard to provide a basic service framework. The specific implementation steps are: fileànew ... àatl com appwizardàserviceàfinish. But using VC to write NT services to write too much code, which means too much debugging, maintenance. In fact, NT services do not have to be written by C / C , which can actually be implemented by any language capable of implementing several features of the previous section, including VB.
VB writing services have those benefits, at least a few of the following:
1. Easy to coding, anyone who is familiar with VB grammar is written after this article. 2, means that the logic of modifying the service is simple, and the maintenance is simple. 3. You can use the component functions in the VB, write a powerful service, such as ADO, etc. If you use VC to implement, I believe that anyone will send it. 4, (awkward) can prove how powerful under the sky of Bill. So, how does VB implement NT service? According to I, there are at least two ways:
1. Use WinAPI according to the C / C ideas.
2. Use the components to be implemented in the traditional way of VB.
If you use the method 1, it is actually a routine of C / C . If there is a better path to be realized, I believe that anyone will not leave this "aircraft", because this programming is completely lost relative to other languages. It has no advantage in other languages. Here, I want to tell you that using OCX to implement a service. If you search for Samples / MSDN / TeChart / 4920 in MSDN, you can see an engineering file that has been written in VC5. Compiling this project to actually get NTSVC.OCX. If you are not familiar with C / C , you can download a ntsvc.ocx from http://www.mywebtech.net/download/ntsvc.zip, this OCX is I got from the BackOffice disc, copy it to / Winnt / Under System32 / Under the regsvr32 ntsvc.occ command to register. In this way, your VB can see the "Microsoft NT Service Control" item in the dialog list that can be taken from the Project / Components .... This component has the basic feature we created a service. If we want to write a NT service, we drag it into our form, then set its properties, call it with the system, registry, and SCM interaction, you can complete A service. We first understand the properties of this component and explain to everyone: Account string, account attribute, which NT service runs under this NT domain account, the default is the localsystem account, and most of the NT services are actually It can be successfully run safely under this account. ControlsAccepted long, this service accepts SCM control, for the following values: 0 allows START and STOP.
2 Allow PAUSE and Continue.
4 Allow Shutdown.
Other values, user-defined events. Use this property, you can determine whether the NT service process (such as an uninterruptible operation) is allowed to allow SCM to stop, pause, start and other operations. Dependencies String, if your written service relies on one or some service to run normally, you must specify a list of dependencies when registering services. DependenCIES divides a plurality of services in CHR (0) according to the dependent order, and finally must end with two CHR (0). (You can see this is a C / C existence trail) DisplayName String, display name, NT service What name is displayed to the viewer. Interactive Boolean, whether it is allowed to have a part of the desktop user interact. LoadOrderGroup string, associated with dependendencies, decide if the service must start, the format is similar, and the format is also similar, and two consecutive CHR (0) ends. Password string, the password that the service starts, if you use the default account, there is no need to set the service startup account. ServiceName String, Service Name, if using net.exe to control the service, Net.exe's specified parameter of that service is the string in this property. STARTMODE enumeration type, specific: vcstartautomatic 2 service can start yourself
SVCStartManual 3 service manual startup
The SVCStartDisabled 4 service cannot start another Debug property, not discussing.
We have to treat a VB program as a NT service, and we must do some "request" to the system, and the corresponding working VB cannot be done well. Therefore, NTSVC.OCX provides the corresponding method to leave the system to deliver information. Install, install the current VB program into NT service, before you have set at least DisplayName, ServiceName, ControlsAccepted, and StartMode and other properties. In addition to this, you may have to set up Account, Password, LoadOrderGroup, Dependencies, etc. The settings of this information are correct or not, determine whether your service program is running normally. Uninstall, delete the service specified by NTSVC.OCX from the system registry. The NT service depends on the setting of the system service registry, which is a well-known secret. StartService, launch the specified service if the service is registered. StopService, stop service if the service is running. Logevent, record service events. In the service run, an error may occur, and the unexpected events can be recorded by this method, and the administrator can view the relevant information through the "Event Outter" to optimize the service. This method has three parameters Event, ID, Message. Event refers to the event type that occurs, can be set to the following values: SVCEventWarning 2 Warning event. SVCEventInformation 4 provides reference information. SVCEventAuditSuccess 8 audit success.
SVCEventAuditFailure 10 Audit Failed In addition to the above method, the user may also need to read and write the registry, which also provides access to the registry: deletesetting (section) getallSettings (section) GetSetting (section, key [, default]) Savesetting (Section, Key, Setting).
Third, write a service to understand the above, let's start to design a service, through example, let everyone understand how to write services in VB. Before we decided to write one of the services. I refer to an example in C Build, write a service process that constantly alarm. After the process starts, I will send a BEEP at the back of 5 seconds. This can make everyone know more about the presence of this service, although some are annoying. The name of the service is VBBeepsVC, which is displayed as the VB NT SVC View in the SCM. Come with me! 1. Create an engineering, set the relevant use control. All VB controls must have a Form as a carrier, so we first create a standard project, select menu Project-> Components ..., then select (Microsoft NT Service Control), which will appear in Toolbar. NT service control. Drag a Timer Control to Form. Then save it. Basically, the creation process is complete. 2, set the control properties. Select the NTSVC.OCX instance, set in the properties bar: DisplayName: The VB NT SVC View, ServiceName: Vbbeepsvc, StartMode: 3 (Manually Start Service). Other is default. Since we want every 5 seconds, we must rely on a timed mechanism, so we set Timer's Interval to 5000 milliseconds. The setting of the above attributes will depend on your needs, I just said that this setting is enough in my VBBeepsvc. 3, write code, implement service logic, and service installation, and removal. Because the service program is actually an exe file, and you need to resolve the installation, remove the problem, you need to add the use of NT service controls to implement installation and removal issues in this program. So, when you should do it.
When the VB program is started, the FORM is loaded, so we need to add some code in the form's Load Event: On Error Goto Err_Load 'If an error occurs to refer to DIM STRDISPLAYNAME AS STRING STRDISPLAYNAME = NTSERVICE1.DISPLAYNAME IF COMMAND = "-install" THEN "Ntservice1.interactive = true if ntservice1.install the call ntservice1.install1," parameters "," Timerinterval "," 1000 ") 'system parameter storage msgbox strDisplayName & "Successful installation!" Else Msgbox strDisplayName & "Install failed" end if End 'termination installation Else if command = "-uninstall" "If startup with the withdrawal parameter if ntservice1.uninstall then msgbox strdisplayName &" Removed success "ELSE Msgbox strdisplayName & "Removement Fail" end if End 'Termination Remove Else End if End if' If it is not installed or removed, it is the launch service Timer1.interval = CINT (NTService1.GetSetting ("Parameters", "TimerInterVal", "2000 ")) 'Use the TIMER control to simulate the thread characteristic ntservice1.controlsaccepted = svcctrlpausecontinue' accept pause, stop operation, means the need to encode N TSERVICE1.StartService 'After setting the parameters Start service err_load: call ntservice1.logevent (svcMessageError, svceventEverror, "[" & err.Number & "& err.description)' SVCMessageError is the error value of NT service controls 4, add control Service code. Although the continuous thread of the service is dependent on Timer, the service control is issued by the SCM interface to each service, and the performance is captured in the VB service, we should These events respond in response to specific circumstances, deciding, how to control service logic. Of course, the specific logic can perform the logic implementation of the control service by changing the Global variables supported by the NT service control and Timer control.
DETAILED code demonstrates: Private Sub NTService1_Control (ByVal EventID As Long) On Error GoTo Err_Control 'The addition of some of their own processing logic, of course, the present embodiment may be the same as vacancies Err_Control: Call NTService1.LogEvent (svcMessageError, svcEventError, "[" & Err.Number & "]" & Err.Description) 'record End SubPrivate Sub NTService1_Pause (Success As Boolean) On Error GoTo Err_Pause Timer1.Enabled = False' ban Timer event, and therefore stop the occurrence service Call NTService1.LogEvent (svcEventError , svcMessageError, "service paused") success = True 'to return to the SCM command issuer, indicating the service was stopped successfully Err_Pause: Call NTService1.LogEvent (svcMessageError, svcEventError, "[" & Err.Number & "]" & Err.Description) End SubPrivate Sub NTService1_Start (Success As Boolean) On Error GoTo Err_Start Success = True Timer1.Enabled = True 'allows service logic Err_Start: Call NTService1.LogEvent (svcMessageError, svcEventError, "[" & Err.Number & "]" & Err .Description) End subprivate sub ntservice1_stop () on error Goto Err_stop unload me 'Removing FORM, natural Timer does not exist, the service logic stopped
Err_Stop: Call ntservice1.logevent (SVCMessageError, SVCEventEverror, "[" & Err.Number & "& Err.Description) End Sub 5, write service logic. Specifically, in the TIMER event, write some service details according to the contract. In this example, BEEN is issued, but considers the response to the SCM command, it needs to be encoded as: on error goto err_timer beep ', the specific service details Err_timer: call ntservice1.logevent (SvcMessageError, SVCEventError, "[" & err .Number & "]" & err.description) End Sub 6, compile installation, test If there is no error in the above, you can now compile the program. Suppose we get the file name of the service program: vbbeepsvc.exe, we need to install by the following command: d: / vbprog /> vbbeepsvc -install If you need to remove the installed service,: d: / vbprog /> vbbeepsvc - After Uninstall installed, the "service" of the control panel is turned on (Win2000 in "Administrative Tools"), well, see the service in the NT service list contains the service we join, "The VB NT SVC View", We can start, stop, stop, suspend this service, stop, stop and suspend this service. When the service is started, we will hear the annoying BEEP sound sent by the service. Our test is complete. four,
VB
Several descriptions of writing services:
1
First, declare:
VB
Writing services is an attempt, technology research, not advocating all services to use
VB
Write to the head. Similarly, the service is not
VC
Can't write.
2
,
VB
Written service is only suitable
Win32
Service, not suitable
NT
Bottom service.
3
,
VB
Advantage
ActiveX
Control,
NT
Services we can use most controls to complete our service logic, such as related database operations, we can use
ADO
Components, this
VC
compared to,
VB
It has natural advantages.
4
, Do a good job in the internal error event record, only this, can really meet the service preparation specification, and it is convenient for our unlock.
5
At the last point, this article is for reference only, if there is a mistake, it is not responsible for the consequences caused by the error.