Is C ++ code in C ++ really so complicated?

Source: Internet
Author: User

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.

 
 
  1. Void main ()
  2. {
  3. SERVICE_TABLE_ENTRY ServiceTable [2];
  4. ServiceTable [0]. LpServiceName="Name"; // Thread name
  5. ServiceTable [0]. LpServiceProc= (LPSERVICE_MAIN_FUNCTION) ServiceMain; // thread entry address
  6. ServiceTable [1]. LpServiceName=NULL;
  7. ServiceTable [1]. LpServiceProc=NULL; // The Last One must be NULL
  8. StartServiceCtrlDispatcher (ServiceTable); // control the dispatch thread of the startup Service
  9. }

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:

 
 
  1. Void main ()
  2. {
  3. SERVICE_TABLE_ENTRY ServiceTable [2];
  4. ServiceTable [0]. LpServiceName="Name"; // Thread name
  5. ServiceTable [0]. LpServiceProc= (LPSERVICE_MAIN_FUNCTION) ServiceMain; // thread entry address
  6. ServiceTable [1]. LpServiceName=NULL;
  7. ServiceTable [1]. LpServiceProc=NULL; // The Last One must be NULL
  8. StartServiceCtrlDispatcher (ServiceTable); // control the dispatch thread of the startup Service
  9. }

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:

 
 
  1. Void winapi ServiceMain (DWORD dwArgc, LPTSTR * lpszArgv)
  2. {
  3. DWORDStatus=0;
  4. DWORDSpecificError=0 xfffffff;
  5. ServiceStatus. dwServiceType=SERVICE_WIN32;
  6. ServiceStatus. dwCurrentState=SERVICE_START_PENDING;
  7. ServiceStatus. dwControlsAccepted=SERVICE_ACCEPT_STOP| SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE;
  8. ServiceStatus. dwWin32ExitCode=0;
  9. ServiceStatus. dwServiceSpecificExitCode=0;
  10. ServiceStatus. dwCheckPoint=0;
  11. ServiceStatus. dwWaitHint=0;
  12. // Call RegisterServiceCtrlHandler () to register a ServiceHandler function to handle the Service control requirements of the program.
  13. HStatus=RegisterServiceCtrlHandler("ServiceName", (LPHANDLER_FUNCTION) ServiceHandler );
  14. If (HStatus= 0)
  15. Return;
  16.  
  17. // Handle error condition
  18. Status=GetLastError();
  19. If (status! = NO_ERROR)
  20. {
  21. ServiceStatus. dwCurrentState=SERVICE_STOPPED;
  22. ServiceStatus. dwCheckPoint=0;
  23. ServiceStatus. dwWaitHint=0;
  24. ServiceStatus. dwWin32ExitCode=Status;
  25. ServiceStatus. dwServiceSpecificExitCode=SpecificError;
  26. SetServiceStatus (hStatus, & ServiceStatus );
  27. Return;
  28. }
  29.  
  30. // Initialization complete-report running status
  31. ServiceStatus. dwCurrentState=SERVICE_RUNNING;
  32. ServiceStatus. dwCheckPoint=0;
  33. ServiceStatus. dwWaitHint=0;
  34. SetServiceStatus (hStatus, & ServiceStatus );
  35.  
  36. // Start your own working thread
  37. HANDLEHThread=CreateThread(NULL, 0, mainfun, NULL, 0, NULL );
  38. If (HThread= NULL)
  39. Return;
  40. }
  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

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.