SERVICE.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:6k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /******************************************************************************
  2. *       This is a part of the Microsoft Source Code Samples.
  3. *       Copyright (C) 1992-1997 Microsoft Corporation.
  4. *       All rights reserved. 
  5. *       This source code is only intended as a supplement to 
  6. *       Microsoft Development Tools and/or WinHelp documentation.
  7. *       See these sources for detailed information regarding the 
  8. *       Microsoft samples programs.
  9. ******************************************************************************/
  10. //+---------------------------------------------------------------------------
  11. //
  12. //  File:       service.c
  13. //
  14. //  Contents:
  15. //
  16. //  Classes:
  17. //
  18. //  Functions:
  19. //
  20. //----------------------------------------------------------------------------
  21. #include "pop3srvp.h"
  22. #pragma hdrstop
  23. SERVICE_STATUS_HANDLE   hService = 0;
  24. WCHAR                   wszServiceName[] = APPLICATION_NAME;
  25. SERVICE_STATUS          ServiceStatus;
  26. extern  BOOLEAN fService;
  27. extern  BOOLEAN fTestMode;
  28. #if DBG
  29. extern  void TestLoop(void);
  30. #endif
  31. SOCKET sListener;
  32. HANDLE  hCompletionPort;
  33. WSADATA WsaData;
  34. BOOL bServiceTerminating = FALSE;
  35. //+---------------------------------------------------------------------------
  36. //
  37. //  Function:   ServiceControlHandler
  38. //
  39. //  Synopsis:   Handles requests from the service controller.
  40. //
  41. //  Arguments:  [fdwControl] -- Request code
  42. //
  43. //  History:    1-11-95   RichardW   Created
  44. //
  45. //  Notes:
  46. //
  47. //----------------------------------------------------------------------------
  48. VOID
  49. WINAPI
  50. ServiceControlHandler(
  51.     DWORD           fdwControl)
  52. {
  53.     switch (fdwControl)
  54.     {
  55.         case SERVICE_CONTROL_STOP:
  56.             UpdateServiceStatus(SERVICE_STOP_PENDING);
  57.             //
  58.             // Remember that the service is terminating.
  59.             //
  60.             bServiceTerminating = TRUE;
  61.             //
  62.             // Close the completion port and the listening socket.
  63.             // These actions will cause the other threads to exit.
  64.             //
  65.             closesocket( sListener );
  66.             CloseHandle( hCompletionPort );
  67.             UpdateServiceStatus(SERVICE_STOPPED);
  68.             return;
  69.         case SERVICE_CONTROL_INTERROGATE:
  70.             UpdateServiceStatus(ServiceStatus.dwCurrentState);
  71.             return;
  72.         default:
  73.             return;
  74.     }
  75. }
  76. //+---------------------------------------------------------------------------
  77. //
  78. //  Function:   NotifyServiceController
  79. //
  80. //  Synopsis:   Notifies the service controller of our control entry point,
  81. //              and tells it that we're trying to start up.
  82. //
  83. //  Arguments:  (none)
  84. //
  85. //  Algorithm:
  86. //
  87. //  History:    1-11-95   RichardW   Created
  88. //
  89. //  Notes:
  90. //
  91. //----------------------------------------------------------------------------
  92. BOOL
  93. NotifyServiceController(
  94.             VOID)
  95. {
  96.     if (!fService)
  97.     {
  98.         return(TRUE);
  99.     }
  100.     ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  101.     ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  102.     ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  103.     ServiceStatus.dwWin32ExitCode = 0;
  104.     ServiceStatus.dwServiceSpecificExitCode = 0;
  105.     ServiceStatus.dwCheckPoint = 0;
  106.     ServiceStatus.dwWaitHint = 0;
  107.     hService = RegisterServiceCtrlHandler(wszServiceName, ServiceControlHandler);
  108.     if (hService)
  109.     {
  110.         UpdateServiceStatus(SERVICE_START_PENDING);
  111.         return(TRUE);
  112.     }
  113.     else
  114.         return(FALSE);
  115. }
  116. BOOL
  117. UpdateServiceStatus(DWORD   Status)
  118. {
  119.     if (hService)
  120.     {
  121.         ServiceStatus.dwCurrentState = Status;
  122.         if ((Status == SERVICE_START_PENDING) || (Status == SERVICE_STOP_PENDING))
  123.         {
  124.             ServiceStatus.dwCheckPoint ++;
  125.             ServiceStatus.dwWaitHint = 5000;    // 5 sec.
  126.         }
  127.         else
  128.         {
  129.             ServiceStatus.dwCheckPoint = 0;
  130.             ServiceStatus.dwWaitHint = 0;
  131.         }
  132.         return(SetServiceStatus(hService, &ServiceStatus));
  133.     }
  134.     return(FALSE);
  135. }
  136. void
  137. FailServiceStart(
  138.     DWORD           Win32Code,
  139.     DWORD           PrivateCode)
  140. {
  141.     ServiceStatus.dwWin32ExitCode = Win32Code;
  142.     ServiceStatus.dwServiceSpecificExitCode = PrivateCode;
  143.     UpdateServiceStatus(SERVICE_STOPPED);
  144. }
  145. void
  146. WINAPI
  147. Pop3SrvMain(
  148.     DWORD       argc,
  149.     LPTSTR      argv[])
  150. {
  151.     int error;
  152.     if (!NotifyServiceController())
  153.     {
  154.         DebugLog((DEB_ERROR, "Could not contact service controllern"));
  155.         return;
  156.     }
  157.     if (!InitializeEvents())
  158.     {
  159.         DebugLog((DEB_ERROR, "Could not initialize eventsn"));
  160.         FailServiceStart(GetLastError(), 0);
  161.         return;
  162.     }
  163.     UpdateServiceStatus(SERVICE_START_PENDING);
  164.     if (!ReadParameters())
  165.     {
  166.         DebugLog((DEB_ERROR, "Could not read parameters!n"));
  167.         FailServiceStart(GetLastError(), 0);
  168.         return;
  169.     }
  170.     UpdateServiceStatus(SERVICE_START_PENDING);
  171.     ReportServiceEvent(
  172.         EVENTLOG_INFORMATION_TYPE,
  173.         POP3EVENT_SERVICE_STARTED,
  174.         0, NULL, 0);
  175.     if (fTestMode)
  176.     {
  177. #if DBG
  178.         TestLoop();
  179. #endif
  180.     }
  181.     else
  182.     {
  183.         error = WSAStartup( 0x0101, &WsaData );
  184.         if ( error == SOCKET_ERROR ) {
  185.             printf( "WSAStartup failed.n" );
  186.         }
  187.         UpdateServiceStatus(SERVICE_START_PENDING);
  188.         //
  189.         // Initialize the POP3SRV worker threads.
  190.         //
  191.         hCompletionPort = InitializeThreads( );
  192.         if ( hCompletionPort == NULL ) {
  193.             printf( "it failed.n" );
  194.         }
  195.         UpdateServiceStatus(SERVICE_RUNNING);
  196.         //
  197.         // Start accepting and processing clients.
  198.         //
  199.         AcceptClients( hCompletionPort );
  200.     }
  201. }