• Main Page
  • Classes
  • Files
  • File List
  • File Members

Beesnest/Windows/DcDemon.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net)
00003 This file is part of Beesnest.
00004 
00005 Beesnest is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 Beesnest is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with Beesnest; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00020 
00021 /**
00022  * DcDemon.cpp: implementation of the CdcDemon class.
00023  * for documentation see the header file.
00024  *
00025  * The code here is based on:
00026  * http://www.codersource.net/win32_nt_service.html
00027  * http://www.devx.com/cplus/Article/9857
00028  */
00029 
00030 #include "DcDemon.h"
00031 
00032 /* Help function to handle calls from Windows. */
00033 void ControlHandler (DWORD request)
00034 {
00035     switch(request)
00036     {
00037         case SERVICE_CONTROL_STOP:
00038         case SERVICE_CONTROL_SHUTDOWN:
00039             CdcDemon::Stop (0);
00040             break;
00041     }
00042     return;
00043 }
00044 
00045 
00046 SERVICE_STATUS CdcDemon::ServiceStatus;
00047 SERVICE_STATUS_HANDLE CdcDemon::hStatus;
00048 char* CdcDemon::gsServiceName = "beesnest";
00049 
00050 void CdcDemon::Install ()
00051 {
00052     SC_HANDLE serviceControlManager = OpenSCManager (0, 0,
00053         SC_MANAGER_CREATE_SERVICE);
00054 
00055     if (serviceControlManager)
00056     {
00057         char path[ _MAX_PATH + 1 ];
00058         if (GetModuleFileName (0, path, sizeof (path) / sizeof (path[0])) > 0)
00059         {
00060             SC_HANDLE service = CreateService (serviceControlManager,
00061                 gsServiceName, gsServiceName,
00062                 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
00063                 SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, path,
00064                 0, 0, 0, 0, 0);
00065             if (service)
00066             {
00067                 CloseServiceHandle (service);
00068                 cout<<gsServiceName<<" Installed Successfully\n";
00069             }
00070             else
00071             {
00072                 if (GetLastError () == ERROR_SERVICE_EXISTS)
00073                     cout<<gsServiceName<<" Already Exists.\n";
00074                 else
00075                     cout<<gsServiceName
00076                         <<" Was not Installed Successfully. Error Code "
00077                         <<GetLastError()<<endl;
00078             }
00079         }
00080         CloseServiceHandle (serviceControlManager);
00081     }
00082 }
00083 
00084 void CdcDemon::Uninstall ()
00085 {
00086     SC_HANDLE serviceControlManager = OpenSCManager (0, 0,
00087         SC_MANAGER_CONNECT);
00088 
00089     if (serviceControlManager)
00090     {
00091         SC_HANDLE service = OpenService (serviceControlManager,
00092             gsServiceName, SERVICE_QUERY_STATUS | DELETE);
00093         if (service)
00094         {
00095             SERVICE_STATUS serviceStatus;
00096             if ( QueryServiceStatus (service, &serviceStatus))
00097                 if (serviceStatus.dwCurrentState == SERVICE_STOPPED)
00098                 {
00099                     if (DeleteService (service))
00100                         cout<<gsServiceName<<" Removed Successfully\n";
00101                     else
00102                     {
00103                         DWORD dwError;
00104                         dwError = GetLastError ();
00105                         if (dwError == ERROR_ACCESS_DENIED)
00106                             cout<<"Access Denied While trying to Remove "
00107                                 <<gsServiceName<<endl;
00108                         else if (dwError == ERROR_INVALID_HANDLE)
00109                             cout<<"Handle invalid while trying to Remove "
00110                                 <<gsServiceName<<endl;
00111                         else if (dwError == ERROR_SERVICE_MARKED_FOR_DELETE)
00112                             cout<<gsServiceName
00113                                 <<" already marked for deletion\n";
00114                     }
00115                 }
00116                 else
00117                     cout<<gsServiceName<<" is still Running.\n";
00118             CloseServiceHandle (service);
00119         }
00120         CloseServiceHandle (serviceControlManager);
00121     }
00122 }
00123 
00124 bool CdcDemon::StartPending ()
00125 {
00126     ServiceStatus.dwServiceType = SERVICE_WIN32;
00127     ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
00128     ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
00129         SERVICE_ACCEPT_SHUTDOWN;
00130     ServiceStatus.dwWin32ExitCode = 0;
00131     ServiceStatus.dwServiceSpecificExitCode = 0;
00132     ServiceStatus.dwCheckPoint = 0;
00133     ServiceStatus.dwWaitHint = 0;
00134 
00135     hStatus = RegisterServiceCtrlHandler (gsServiceName,
00136         (LPHANDLER_FUNCTION)ControlHandler);
00137     if (hStatus == (SERVICE_STATUS_HANDLE)0)
00138         return false;
00139 
00140     SetServiceStatus(hStatus, &ServiceStatus);
00141     return true;
00142 }
00143 
00144 bool CdcDemon::MarkRun ()
00145 {
00146     ServiceStatus.dwCurrentState = SERVICE_RUNNING;
00147     SetServiceStatus(hStatus, &ServiceStatus);
00148     return true;
00149 }
00150 
00151 bool CdcDemon::Stop (int code)
00152 {
00153     ServiceStatus.dwCurrentState = SERVICE_STOPPED;
00154     ServiceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP |
00155             SERVICE_ACCEPT_SHUTDOWN);
00156     ServiceStatus.dwWin32ExitCode = code;
00157     SetServiceStatus(hStatus, &ServiceStatus);
00158     return true;
00159 }
00160 
00161 
00162 void CdcDemon::RunMain ()
00163 {
00164     SERVICE_TABLE_ENTRY ServiceTable[2];
00165     ServiceTable[0].lpServiceName = gsServiceName;
00166     ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ProgramMain;
00167     ServiceTable[1].lpServiceName = NULL;
00168     ServiceTable[1].lpServiceProc = NULL;
00169     /* Start the control dispatcher thread for the service. */
00170     StartServiceCtrlDispatcher (ServiceTable);
00171 }
00172 

Generated on Mon Oct 11 2010 16:23:25 for Beesnest by  doxygen 1.7.2