No, BCB has its own "service guide". The following code is found. Windows NT has a very important difference with Windows 9x, that is, Windows NT provides a lot of powerful service. These services can start from the start of NT, or allow users to start by the control panel, and can be stopped by the Win32 application. Even without the user login system, these services can also be executed. Many ftps, WWW servers and databases are in NT in the form of service, thereby achieving unattended. Even the latest version of the "hacker" program Back OrificE 2000 is also hidden on the service in the form of a service. Because Service is more complicated, many developers want to develop their own service but often. In view of this, then we will construct a new service from the head to the end, readers as long as they add their own code in the program, then you can easily have a self-service. Before writing service, first introduce several important functions: ---- 1. SC_Handle OpenScManager (LPCTSTSTR LPMABASENAME, DWORD DWDESIREDACCESS) --- OpenScManager function Opens the Service Control Manager Database on the specified computer. The parameter lpMachinename specifies the computer name, and if specified as the unit for the air. LPDATABASENAME is empty for the Service Control Manager Database name to be opened. Dwdesired Accept Specifies the permissions of the operation, one of the values below: ---- SC_MANAGER_ALL_ACCESS / / All permissions ---- SC_Manager_connect // Allow Connect to Service Control Manager Database ---- SC_Manager_Create_Service // Allow the creation of service objects and put It joins Database ---- SC_MANAGER_ENUMERATE_SERVICE / / Allow enumeration of service ---- sc_manager_lock // Allow locks Database ---- SC_Manager_Query_lock_status // Allow Query Database's blockade information ---- Function is successful, return A handle pointing to the Service Control Manager Database, returns NULL. Note: Winnt manages all services through a database called Service Control Manager Database, so this database should be opened for any operation of Service.
---- 2. SC_HANDLE CreateService (SC_HANDLE hSCManager, LPCTSTR lpServiceName, LPCTSTR lpDisplayName, DWORD dwDesiredAccess, DWORD dwServiceType, DWORD dwStartType, DWORD dwErrorControl, LPCTSTR lpBinaryPathName, LPCTSTR lpLoadOrderGroup, LPDWORD lpdwTagId, LPCTSTR lpDependencies, LPCTSTR lpServiceStartName, LPCTSTR lpPassword)
---- CreatService function generates a new service. The parameter hscmanager is the handle of the Service Control Manager Database, returned by OpenScManager. LPServiceName is the name of Service, LPDisplayName is a service display name, DwdesiredAccess is access, this program with service_all_access. WSERVICEPE, specify the service type, in this program with service_win32_oen_process | service_interactive_process. DWStartType is the service launch method, this program uses self-start, ie DWStartType is equal to service_auto_start. DWERRORCONTROL Note What actions take when the Service is in the launch, this program uses service_error_ignore, and readers can be changed to other. LPBINARYPATHNAME indicates the path name of the Service ontology program. The remaining five parameters can be typically set to NULL. If the function call is successful, return this new service handle, and the null is returned. Corresponding to this function is DELETSERVICE (HService), which deletes the specified service. ---- 3. SC_Handle OpenService (sc_handle hscmanager, lpctstr lpserviceename, dword dwdesiredAccess) ---- OpenService function Opens the specified service. The parameter hscmanager is the handle of the Service Control Manager Database, returned by OpenScManager. LPServiceName is the name of Service, dwdesiredAccess is access to access, and its optional value is more, readers can see the SDK Help. The function call successfully returns the open service handle, and returns NULL. ---- 4. Bool StartService (sc_handle hservice, dword dword dword dword dwnumservicegs) ---- StartService function launches the specified service. The parameter hservice is a handle pointing to the service, returned by OpenService. DwnumServiceA is the number of parameters required to start the service. LPSZServiceArgs is the parameters required to start the service. The function is successful, returns true, and false is returned. ---- 5. Bool ControlService (SC_Handle HService DWORD DWCONTROL, LPSERVICE_STATUS LPSERVICESTATUS) ---- Service program does not have a dedicated stop function, but with the controlService function to control the service's pause, continue, stop and other operations. DwControl parameter specifies the control command is issued, it is possible for the following values: SERVICE_CONTROL_STOP // stop ServiceSERVICE_CONTROL_PAUSE // pause ServiceSERVICE_CONTROL_CONTINUE // continue ServiceSERVICE_CONTROL_INTERROGATE // Query Service status SERVICE_CONTROL_SHUTDOWN // make ControlService call failure
---- Parameter LPServiceStatus is a pointer to Service_Status. Service_status is a more important structure that contains various information of the service, such as the current state, which control commands, and more. ---- 6. Bool queryServiceStatus (sc_handle hservice, lpservice_status lpservicestatus) ---- The queryServiceStatus function is relatively simple, it queries and returns the status of the current service. ---- Preparing a service usually requires two programs, one is a service body, one is a control program for controlling Service. Usually the service body is a console program, and the control program is a normal WIN32 application (of course, the user can be enabled by the control panel without the control program, stopping, but can not add, delete operation.) - First, let's write a service body. For the service body, it is generally composed of three parts: main (), servicemain (), handler (), the source code of Main (): (Note: Due to the relationship of the space, most of the program is not Error handling, readers can add themselves) int Main (int Argc, char ** argv) {service_table_entry ste [2]; // A service process can have multiple threads, this is the entry table for each // thread Stendum STE [0 ] .lpServiceName = "wzservice"; // thread name Ste [0] .lpserviceproc = serviceMain; // thread portal address Ste [1] .lpServiceName = null; // The last one must be null ste [1] .lpServiceProc = NULL; StartServiceCtrlDispatcher (ste); return 0;}
---- Main () is the main thread of the service. When Servie Control Manager starts a service process, it is always waiting for this service to call the StartServiceCtrldispatcher () function. Main () as the main thread of this process should call StartServiceCtrlDispatcher as soon as possible after the program. StartServiceCtrlDispatcher () is not immediately returned after being called, connects the host's main thread to Service Control Manager, so that Service Control Manager will start, stop control commands to the main thread through this connection. The main thread played the role of a command transponder, it or calls handle () to process the stop, continue the control requirements, or generate a new thread to execute ServiceMain. StartServiceCtrlDispatcher () returned at the end of the service. ---- ServiceMain () is a true entry point for Service, must be properly defined in main (). The two parameters of ServiceMain () are passed by StartService (). The following is the ServiceMain () of source code: void WINAPI ServiceMain (DWORD dwArgc, LPTSTR * lpszArgv) {ssh = RegisterServiceCtrlHandler ( "WZSERVICE", Handler); ss.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS; ss.dwCurrentState = SERVICE_START_PENDING; // The The code of the user program is more (execution time is more than 1 second), which is set to service_start_pending, and set to service_running after the user program is completed. Ss.dwcontrolsaccepted = service_accept_stop; // indicates that the command currently can accept is a stop command. Ss.dwwin32exitcode = no_error; ss.dwcheckpoint = 0; ss.dwwaithint = 0; setServiceStatus (SSH, & SS); // must update the status of the service in the database at any time. Mycode (); // here the code can be placed in the user's own ss.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS; ss.dwCurrentState = SERVICE_RUNNING; ss.dwControlsAccepted = SERVICE_ACCEPT_STOP; ss.dwWin32ExitCode = NO_ERROR; ss.dwCheckPoint = 0; ss.dwWaitHint = 0; SetServiceStatus (SSH, & SS); mycode (); // This can also be put into the user's own code} should immediately call RegisterServiceCtrlHandler () to register a Handler de-processing control program or control panel to service Claim.
Handler () is called to deal with the forwarder, the following is the source code of Handler (): Void WinApi Handler (DWORD OPCODE) {switch (opcode) {copy service_control_stop: // Stop Service mycode (); // can be put into it here User's own code ss.dwwin32exitcode = 0; s.dwcurrentState = service_stopped; // Put the current state of Service to stop ss.dwcheckpoint = 0; ss.dwaithint = 0; setServiceStatus (ssh, & ss); / must be updated at any time The Status of Service in the database; Case Service_Control_Interrogate: setServiceStatus (SSH, & SS); / Must update the status of the service in the database at any time;}}
---- Well, the service body program has been basically completed, let's take a look at the service control program: ---- Control program is a standard Window program, which mainly has four heads: Create Service, Delete Service START, STOP, used to generate, delete, start, and stop service. The following is a part of the source thereof: 1. Generate Servicevoid __fastcall TForm1 :: CreateBtnClick (TObject * Sender) {scm = OpenSCManager (NULL, NULL, SC_MANAGER_CREATE_SERVICE); if (scm = NULL!) {Svc = CreateService (scm, "WZ SERVICE "," WZSERVICE ", // Service name SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, SERVICE_AUTO_START, // an automated way to start SERVICE_ERROR_IGNORE," C: //ntservice.exe ", // Service program path body, must be consistent with the specific location NULL, NULL, NULL, NULL, NULL); if (svc = NULL) CloseServiceHandle (svc);! CloseServiceHandle (scm);.}} 2 deleted Servicevoid __fastcall TForm1 :: DeleteBtnClick (TObject * Sender) {scm = OpenSCManager (NULL , NULL, SC_MANAGER_CONNECT); if (scm = NULL) {svc = OpenService (scm, "WZSERVICE", SERVICE_ALL_ACCESS);!! if (svc = NULL) {QueryServiceStatus (svc, & ServiceStatus); if (ServiceStatus.dwCurrentState == Service_running) // Before delete, stop this service. ControlService; SVC, Service_Control_Stop, & ServiceStatus; deleteService (SVC); ClosESERVICEHANDE (SVC); // Remove the service, it is best to adjust Use closeServiceHandle} // to remove this entry immediately from the database.