The following describes the problems related to C ++ code programming, how to compile C ++ code, and how to better compile C ++ code, having mastered the issue of C ++ code compilation, it means that the entire software development cycle is less detours, reducing the complexity of the work.
- Void main ()
- {
- SERVICE_TABLE_ENTRY ServiceTable [2];
- ServiceTable [0]. LpServiceName="Name"; // Thread name
- ServiceTable [0]. LpServiceProc= (LPSERVICE_MAIN_FUNCTION) ServiceMain; // thread entry address
- ServiceTable [1]. LpServiceName=NULL;
- ServiceTable [1]. LpServiceProc=NULL; // The Last One must be NULL
- StartServiceCtrlDispatcher (ServiceTable); // control the dispatch thread of the startup Service
- }
For a Service program, it generally consists of the following four parts: main (), ServiceMain) and Handler). Of course, there are also the relationships between our function implementation functions, such as MyWork, you can use a simple graph to explain.
The basic process of its service program is to call ServiceMain () by main (), while ServiceMain () calls Handler () and the execution function MyWork () until now, you have learned about the process of the service program and implemented the backdoor program step by step according to the above process.
1. main () function of the program entry
The entry of the service program starts from main (), but the difference is that the main of the service program is extremely simple, because it is only responsible for creating the dispatch table and starting the control dispatcher, the C ++ code is as follows:
- Void main ()
- {
- SERVICE_TABLE_ENTRY ServiceTable [2];
- ServiceTable [0]. LpServiceName="Name"; // Thread name
- ServiceTable [0]. LpServiceProc= (LPSERVICE_MAIN_FUNCTION) ServiceMain; // thread entry address
- ServiceTable [1]. LpServiceName=NULL;
- ServiceTable [1]. LpServiceProc=NULL; // The Last One must be NULL
- StartServiceCtrlDispatcher (ServiceTable); // control the dispatch thread of the startup Service
- }
2. Real service entry ServiceMain ()
ServiceMain () is the real entry point of the Service program. It mainly performs the following functions. First, register a Handler to process the control program or control panel. Service control requirements, such as start and stop, pause and restart, followed by implementing our function operations. The C ++ code is as follows:
- Void winapi ServiceMain (DWORD dwArgc, LPTSTR * lpszArgv)
- {
- DWORDStatus=0;
- DWORDSpecificError=0 xfffffff;
- ServiceStatus. dwServiceType=SERVICE_WIN32;
- ServiceStatus. dwCurrentState=SERVICE_START_PENDING;
- ServiceStatus. dwControlsAccepted=SERVICE_ACCEPT_STOP| SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE;
- ServiceStatus. dwWin32ExitCode=0;
- ServiceStatus. dwServiceSpecificExitCode=0;
- ServiceStatus. dwCheckPoint=0;
- ServiceStatus. dwWaitHint=0;
- // Call RegisterServiceCtrlHandler () to register a ServiceHandler function to handle the Service control requirements of the program.
- HStatus=RegisterServiceCtrlHandler("ServiceName", (LPHANDLER_FUNCTION) ServiceHandler );
- If (HStatus= 0)
- Return;
-
- // Handle error condition
- Status=GetLastError();
- If (status! = NO_ERROR)
- {
- ServiceStatus. dwCurrentState=SERVICE_STOPPED;
- ServiceStatus. dwCheckPoint=0;
- ServiceStatus. dwWaitHint=0;
- ServiceStatus. dwWin32ExitCode=Status;
- ServiceStatus. dwServiceSpecificExitCode=SpecificError;
- SetServiceStatus (hStatus, & ServiceStatus );
- Return;
- }
-
- // Initialization complete-report running status
- ServiceStatus. dwCurrentState=SERVICE_RUNNING;
- ServiceStatus. dwCheckPoint=0;
- ServiceStatus. dwWaitHint=0;
- SetServiceStatus (hStatus, & ServiceStatus );
-
- // Start your own working thread
- HANDLEHThread=CreateThread(NULL, 0, mainfun, NULL, 0, NULL );
- If (HThread= NULL)
- Return;
- }
- Introduction to C ++
- Summary Notes on learning and exploring C ++ library functions
- Basic Conception and method of C ++ Class Library Design
- Does C ++ really have market value?
- Basic Conception and method of C ++ Class Library Design