Ntsvc. ocx instructions for use

Source: Internet
Author: User
Account string, account attribute, that is, the NT domain account under which the NT Service runs. The default is the LocalSystem account. In fact, most NT services can run safely and satisfactorily under this account.
Controlsaccepted long, which is controlled by SCM and has the following values:
0. Allow start and stop.

2. allow pause and continue.

4. shutdown is allowed.

Other values, user-defined events.
With this attribute, you can decide whether to allow the SCM to stop, pause, start, and other operations at a certain time point of the NT service process (such as an operation that cannot be interrupted.
Dependencies string. if the service you write depends on one or more services for normal operation, you must specify the list of services that you depend on when registering the service. Dependencies separate multiple services by CHR (0) according to the dependency order, and end with two CHR (0. (We can see that this is a trace of C/C ++)
Displayname string, display name. The name of the NT Service displayed to the viewer.
Interactive Boolean: whether to allow interaction with desktop users.
Loadordergroup string, which is related to dependencies. It is determined that the services must be started before the service is started. The format is similar. It is also separated by CHR (0) and ends with two consecutive CHR (0.
Password string indicates the password for starting the service. If you use the default account, you do not need to set the account for starting the service.
Servicename string, service name. If net.exe is used to control the service, the service parameter specified by net.exe is the string in this attribute.
Startmode Enumeration type, specifically:
Vcstartautomatic 2 service can be started by yourself

Svcstartmanual 3 Service Manual start

Svcstartdisabled 4 service cannot be started by itself
There is also a debug attribute, which is not discussed.

We wantProgramAs an NT Service, some "Applications" must be made to the system, and the corresponding job VB cannot be completed well. Therefore, ntsvc. ocx provides the corresponding method for us to transfer relevant information to the system.
Install to install the current VB program into the NT Service. Before that, you must set at least displayname, servicename, controlsaccepted, startmode, and other attributes. In addition, you may also need to set the account, password, loadordergroup, dependencies, and so on. Whether the information is correctly set determines whether your service program can start and run properly.
Uninstall: Delete the service specified by ntsvc. ocx from the system registry. The NT service depends on the settings of the system service registry, which is a well-known secret.
Startservice: starts the specified service if it is registered.
Stopservice: stop the service if the service is running.
Logevent, which records service events. Errors and unexpected events may occur during service running. You can use this method to record these events, so that the administrator can use the "Event Viewer" to view relevant information to optimize the service. This method has three parameters: event, ID, and message. event, which indicate the event type and can be set to the following values:
Svceventerror 1 error event
Svceventwarning 2 warning event.
Svceventinformation 4 provides reference information.

Svceventauditsuccess 8 Audit successful.

Svceventauditfailure 10 audit failed
In addition to the above methods, you may also need to read and write the Registry. This control also provides the Registry access method:
Deletesetting (Section [, key])
Getallsettings (Section)
Getsetting (section, key [, default])
Savesetting (section, key, setting ).

'================================================= ==============

Note when registering a service with this control. If we do not use the/I or/u parameter, the Service created cannot be started because of timeout. Therefore, when registering a service, the/I or/U parameters must be included.

1. Reference controls
Select "project"-"Reference"-"Microsoft NT Service Control". If not, set ntsvc first. copy OCX to % system32 %/, and then select Browse in the reference dialog box to add the control.

2. Main Code
Private sub form_load ()
On Error goto serviceerror
'Install the service
If command = "/I" then
Ntservice. Interactive = true
If ntservice. Install then
Ntservice. savesetting "Parameters", "timerinterval", "300"
Msgbox ntservice. displayname & ": installed successfully"
Else
Msgbox ntservice. displayname & ": failed to install"
End if
End
'Delete Service
Elseif command = "/u" then
If ntservice. Uninstall then
Msgbox ntservice. displayname & ": uninstalled successfully"
Else
Msgbox ntservice. displayname & ": failed to uninstall"
End if
End
End if
Timer. interval = CINT (ntservice. getsetting ("Parameters", "timerinterval", "300 "))
Ntservice. controlsaccepted = svcctrlpausecontinue
Ntservice. startservice
Exit sub
Serviceerror:
Call ntservice. logevent (svcmessageerror, svceventerror, "[" & err. Number & "]" & err. description)
End sub

'Unload the service
Private sub form_unload (cancel as integer)
If not stopservice then
If msgbox ("are you sure you want to unload the service ?... "& Vbcrlf &" the service will be stopped ", vbquestion + vbyesno," Stop Service ") = vbyes then
Ntservice. stopservice
Label1.caption = "Stopping"
Cancel = true
Else
Cancel = true
End if
End if
End sub

Private sub ntservice_continue (success as Boolean)
On Error goto serviceerror
Timer. Enabled = true
Success = true
Ntservice. logevent svceventinformation, svcmessageinfo, "service continued"
Exit sub
Serviceerror:
Ntservice. logevent svcmessageerror, svceventerror, "[" & err. Number & "]" & err. Description
End sub

private sub ntservice_control (byval mevent as long)
on error goto serviceerror
exit sub
serviceerror:
ntservice. logevent svcmessageerror, svceventerror, "[" & err. number & "]" & err. description
end sub

private sub ntservice_pause (success as Boolean)
on error goto serviceerror
timer. enabled = false
ntservice. logevent svceventerror, svcmessageerror, "service paused"
success = true
exit sub
serviceerror:
ntservice. logevent svcmessageerror, svceventerror, "[" & err. number & "]" & err. description
end sub

Private sub ntservice_start (success as Boolean)
On Error goto serviceerror
Success = true
Exit sub
Serviceerror:
Ntservice. logevent svcmessageerror, svceventerror, "[" & err. Number & "]" & err. Description
End sub

Private sub ntservice_stop ()
On Error goto serviceerror
Stopservice = true
Unload me
Serviceerror:
Ntservice. logevent svcmessageerror, svceventerror, "[" & err. Number & "]" & err. Description
End sub

3. If other controls trigger install and uninstall of the Service, shell or winexec can be used for processing.
Declare functions first
Public declare function winexec lib "Kernel32" (byval lpcmdline as string, byval ncmdshow as long) as long
Public const sw_hide = 0
For example, use checkbox to trigger
A. Install
Call winexec (App. exename & "/I", sw_hide)

B. Uninstall
Call winexec (App. exename & "/u", sw_hide)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.