How to Use vc6.0 to compile Windows service programs

Source: Internet
Author: User

How to Use vc6.0 to compile Windows service programs

2005/10/11

Yang Dengfeng (MSN: dengfengyang@hotmail.com)

I need to create a service application for project development. However, it seems that there are few articles on the Internet. Most of them use the basic class in the. NET architecture to write Windows service programs. After reading it, I feel that I cannot describe the characteristics of the Windows service application.

After referring to msdn, I think it is necessary to describe the architecture of the Windows service interface, so that later users may have to look at msdn from the beginning. For more information, see.

It mainly involves Service Control Manager (SCM ). SCM is mainly used to manage service objects. Because the service is a system-hosted application, each service corresponds to a kernel object. Use the createservice and openservice functions to create and delete instances. The system has a service ing table between service objects and service processing functions. In this way, the system can host the service program. The service processing function is the critical callback function provided to the system by the service application. In the service program, the main task is to call the startservicectrldispatcher function at the beginning so that the SCM can know the message Response Processing Function of the service program.

(1) create a service

Call openscmanager () to connect to the Service Control Manager of the computer operating system, and open the database of the Service Control Manager of the system.
Schscmanager = openscmanager (
Null, // machine (null = Local)
Null, // database (null = default)
SC _manager_all_access // access required
);

Call createservice () to open a service object. Add the service object to the Service Control Manager Database of the system.
Schservice = createservice (
Schscmanager, // scmanager Database
Text (szservicename), // name of service
Text (szservicedisplayname), // name to display
Service_all_access, // desired access
Service_win32_own_process | service_interactive_process, // service type
Service_auto_start, // start Type
Service_error_normal, // Error Control Type
Szpath, // Service's binary
Null, // No load ordering Group
Null, // No tag identifier
Text (szdependencies), // Dependencies
Null, // LocalSystem account
Null); // No Password

Define the main entry point function of the service application. When the created service runs, this function is the embodiment of the main functions of the Service.
Service_table_entry dispatchtable [] // service dispatchtable
=
{
{Text (lptstr) (lpctstr) szservicename), (lpservice_main_function) service_main}, // here service_main is the main entry point function of the service application.
{Null, null }//
};

Call the startservicectrldispatcher () function to create a callback function for the service and link the callback function to the Service Control Manager. In this way, the service can implement message delivery through the callback function.
Void winapi service_main (DWORD dwargc, lptstr * lpszargv)
{
// ×¢ 2áservice _ CTRL ←? (SCM-> service ???? O ˉ ê y)
Sshstatushandle = registerservicectrlhandler (text (szservicename), service_ctrl );
If (! Sshstatushandle)
Goto cleanup;

// Service functional code.

Return;
}
In the service_main function, first assign a callback function pointer to the request interface of the Service Control Manager. Then, the user clicks start and pause in the Service Control Manager ", "Continue running", "stop", and other message responses.
Sshstatushandle = registerservicectrlhandler (text (szservicename), service_ctrl); // hook up the callback function interface of the System Service Control Manager's response.
Void winapi service_ctrl (DWORD dwctrlcode)
{
// Handle the requested control code.
//
Switch (dwctrlcode)
{
// Stop the service.
//
// Service_stop_pending shoshould be reported before
// Setting the stop event-hserverstopevent-in
// Servicestop (). This avoids a Race Condition
// Which may result in a 1053-the service did not respond...
// Error.
Case service_control_stop:
Reportstatustoscmgr (service_stop_pending, no_error, 0 );
Stopservice ();
Return;

// Update the service status.
//
Case service_control_interrogate:
Break;

// Invalid control code
//
Default:
Break;

}

Reportstatustoscmgr (ssstatus. dwcurrentstate, no_error, 0 );
}

(2) uninstall the service
Open openscmanager () Service Control Manager
Call openservice () to open the corresponding service according to the service name, and return the corresponding handle.
First, disable the service to be uninstalled.
Controlservice (schservice, service_control_stop, & ssstatus );

Use the queryservicestatus () function to confirm that the service has been disabled.
While (queryservicestatus (schservice, & ssstatus ))
{
If (ssstatus. dwcurrentstate = service_stop_pending)
{
_ Tprintf (text ("."));
Sleep (1000 );
}
Else
Break;
}

You can use deleteservice to delete this service.
If (deleteservice (schservice ))

Demo program (http://www.metalsoft.cn/article/WindowService.rar)

Related Article

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.