MMORPG Server-api

Source: Internet
Author: User
StartServiceCtrlDispatcher
In 2000/xp, such as the NT-based operating system, there is a service manager, it manages the background process is called service. A service is an application type that runs in the background and is similar to a UNIX background application.

    Service applications can typically provide users with functionality, such as client/server applications, WEB servers, database servers, and other server-based applications, locally and over the network. The background service program is quietly running in the background.

     

     By registering our program as a service, we can make our program not present in Task Manager, and run it first with system startup, and finally stop with system shutdown.

     The Service Control Manager is an RPC server that exposes a set of application programming interfaces that programmers can easily write to configure services and control service programs on remote servers. Service programs are usually written into console-type applications, and in general, a program that adheres to the interface requirements of the service Control management program contains the following three functions: 1.

Service Program main function (main): Calls the system function StartServiceCtrlDispatcher the connector main thread to the Service control management program. 2.

Service entry point function (ServiceMain): Performs service initialization tasks, and service processes that perform multiple services have multiple service entry functions. 3. Control Service handler function (Handler): referenced by the control distribution thread when the service program receives a control request.

 (Here is Service_ctrl). Additionally, you need to install the enrollment service before the system runs this service: the Installservice function.

 

Deleting a service requires that you first remove the service installation registration: RemoveService function.
 
Service Type: Type description service_file_system_driver=2 file system driver service.
 
Service_kernel_driver=1 Drive Service.
 
Service_win32_own_process=16 a service that exclusively processes.
 

SERVICE_WIN32_SHARE_PROCESS=32 A service that shares a process with other services. Create a new WIN32 console program with the source file named Service.cpp.

 

The development tool I use is vc++.net. 1. After the Service Program main function service control management program launches the service program, waits for the service program main function to call the system letter StartServiceCtrlDispatcher. A service_win32_own_procesS-type services should immediately call the StartServiceCtrlDispatcher function, which allows the service entry point function to complete initialization after the service starts. For service_win32_own_process type of service and all services in the program common initialization work can be done in the main function, but not more than 30 seconds.

 

 

Otherwise, additional threads must be created to perform these common initialization tasks, thus ensuring that the service program main function can call the StartServiceCtrlDispatcher function in a timely manner. The main function handles three command-line arguments:-install,-remove,-Debug, for installation, deletion, and debugging of service programs. If run without parameters, it is assumed that the service control management appears to start the service program.

 

If the argument is not correct, give the hint information. The StartServiceCtrlDispatcher function is responsible for Chenglian the program main line to the Service control management program.

The specific description is as follows: BOOL startservicectrldispatcher (const lpservice_table_entry lpservicetable); Lpservicestarttable points to an array of service_table_entry struct types that contains the entry function and string name for each service provided by the calling process. The last element in the table must be NULL to indicate the end of the Entry table.  The service_table_entry structure is described as follows: typedef struct _SERVICE_TABLE_ENTRY {LPTSTR lpservicename;

Lpservice_main_function Lpserviceproc;

 

} service_table_entry, *lpservice_table_entry; Lpservicename is a NULL-terminated string that identifies the service name.

If it is a service_win32_own_process type of service, this string will be ignored.

 

 

Lpserviceproc points to the service entry point function.

The service program main function. #include "stdafx.h" #include "Windows.h" #define SZAPPNAME "serversample"//Service Program name #define SZSERVICENAME "Servicesample "//Logo service internal//internal variable bool Bdebugserver=false;

Service_status Ssstatus;

Service_status_handle Sshstatushandle;

DWORD dwerr=0;

 

TCHAR szerr[256];

The following function is implemented by the program void WINAPI Service_main (DWORD dwargc, LPTSTR *lpszargv);

void WINAPI Service_ctrl (DWORD dwctrlcode);

void Installservice ();

void RemoveService ();

void Debugservice (int argc,char** argv);

BOOL Reportstatustoscmgr (DWORD dwcurrentstate,dword dwwin32exitcode,dword dwwaithint);

 

void Addtomessagelog (LPTSTR lpszmsg); int _tmain (int argc, _tchar* argv[]) {service_table_entry dispatchtable[]= {TEXT (Szservicena

     ME), (lpservice_main_function) Service_main}, {null,null}}; if ((argc>1) && ((*argv[1]== '-') | |

     (argv[1]== "/"))

         {if (_stricmp ("Install", argv[1]+1) ==0) {installservice (); else if (_stricmp ("Remove", argv[1]+1) ==0) {RemoveService ();

              else if (_stricmp ("Debug", argv[1]+1) ==0) {bdebugserver=true;

         Debugservice (ARGC,ARGV); else {//If the above argument cannot be matched, it may be the Service Control Manager to start the program.

              Call the//startservicectrldispatcher function now.

              printf ("%s-install to install the service \ n", szappname);

              printf ("%s-remove to remove the service \ n", szappname);

              printf ("%s-debug to debug the service \ n", szappname);

              printf ("\ n startservicectrldispatcher being called.\n"); printf ("This may take several seconds.

              Please wait.\n "); if (! StartServiceCtrlDispatcher (dispatchtable)) Addtomessagelog (TEXT ("StartServiceCtrlDispatcher failed.")

              );

         Else Addtomessagelog (TEXT ("StartServiceCtrlDispatcher OK)");

     } exit (0); } return0; }

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.