// OIMOBOXWatchDirService.cpp : implementation file // #include "stdafx.h" #include "OIMOBOXWatchDir.h" #include "OIMOBOXWatchDirService.h" COIMOBOXWatchDirService::COIMOBOXWatchDirService(): CNTService( _T( "OIMOBOXWatchDir" ), // ·þÎñÃû³Æ _T( "OpenInfo MOBOX BackupDir Service" ), // ·þÎñÏÔʾÃû³Æ SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE, _T( "OpenInfo MOBOX ±¸·Ý¼àÊÓ·þÎñ" ) ) // ·þÎñÃèÊö { ::CoInitialize( NULL ); m_hSvcStopEvent = NULL; m_hSvcThread = NULL; m_pMOBOXWatchDirDlg = NULL; } COIMOBOXWatchDirService::~COIMOBOXWatchDirService() { ::CoUninitialize(); m_EventLogSource.Report( EVENTLOG_INFORMATION_TYPE, MSG_SERVICE_INFO, _T( "~COIMOBOXWatchDirService¡£" ) ); } // COIMOBOXWatchDirService member functions void COIMOBOXWatchDirService::ServiceMain( DWORD /*dwArgc*/, LPTSTR* /*lpszArgv*/ ) { // register our control handler RegisterCtrlHandler(); // Pretend that starting up takes some time ReportStatusToSCM( SERVICE_START_PENDING, NO_ERROR, 0, 1, 0 ); // ´´½¨·þÎñֹͣʼþ m_hSvcStopEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); if( NULL == m_hSvcStopEvent ) { Report( _T("´´½¨·þÎñֹͣʼþʧ°Ü"), EVENTLOG_ERROR_TYPE ); ReportStatusToSCM( SERVICE_STOPPED, NO_ERROR, 0, 1, 0 ); return; } if ( !Start() ) { Report( _T("³õʼ»¯Ê§°Ü"), EVENTLOG_ERROR_TYPE ); ReportStatusToSCM( SERVICE_STOPPED, NO_ERROR, 0, 1, 0 ); return; } // ´´½¨·þÎñÏß³Ì /*m_hSvcThread = (HANDLE)_beginthreadex( NULL, 0, &ServiceThread, (LPVOID)this, 0, NULL ); if( NULL == m_hSvcThread ) { Report( _T("´´½¨·þÎñÏß³Ìʧ°Ü"), EVENTLOG_ERROR_TYPE ); ReportStatusToSCM( SERVICE_STOPPED, ERROR, 0, 1, 0 ); return; } CloseHandle( m_hSvcThread );*/ Report( _T("MOBOXWatchDirService start") ); ReportStatusToSCM( SERVICE_RUNNING, NO_ERROR, 0, 1, 0 ); /*while( 1 ) { WaitForSingleObject( m_hSvcStopEvent, INFINITE ); Report( _T("MOBOXWatchDirService end") ); ReportStatusToSCM( SERVICE_STOPPED, NO_ERROR, 0, 1, 0 ); return; }*/ } BOOL COIMOBOXWatchDirService::Start() { CString strError; CString strLog; Report(_T( "³õʼ»¯·þÎñÐÅÏ¢¡£" ) ); if(m_pMOBOXWatchDirDlg) { if( ::IsWindow(m_pMOBOXWatchDirDlg->GetSafeHwnd())) m_pMOBOXWatchDirDlg->DestroyWindow(); delete m_pMOBOXWatchDirDlg; m_pMOBOXWatchDirDlg = NULL; Report( _T( " Start 1" ) ); } m_pMOBOXWatchDirDlg = new COIMOBOXWatchDirDlg; m_pMOBOXWatchDirDlg->m_hSvcStopEvent = m_hSvcStopEvent; if ( !m_pMOBOXWatchDirDlg->Create( IDD_OIMOBOXWATCHDIR_DIALOG,m_pMOBOXWatchDirDlg ) ) { delete m_pMOBOXWatchDirDlg; m_pMOBOXWatchDirDlg = NULL; SetEvent( m_hSvcStopEvent ); strError = _T( "³õʼ»¯·þÎñÐÅϢʧ°Ü¡£" ); Report( strError,EVENTLOG_ERROR_TYPE, MSG_SERVICE_INFO ); ReportStatusToSCM( SERVICE_STOPPED, ERROR, 0, 1, 0 ); return FALSE; } //#ifndef _DEBUG // m_pMOBOXWatchDirDlg->ShowWindow( SW_HIDE ); //#else m_pMOBOXWatchDirDlg->ShowWindow( SW_SHOW ); //#endif AfxGetApp()->m_pMainWnd = m_pMOBOXWatchDirDlg; Report( _T( "³õʼ»¯·þÎñÐÅÏ¢³É¹¦¡£" ) ); // Report to the event log that the service has started successfully Report(m_sDisplayName, EVENTLOG_INFORMATION_TYPE, CNTS_MSG_SERVICE_STARTED ); ReportStatusToSCM( SERVICE_RUNNING, NO_ERROR, 0, 1, 0 ); // pool.WaitForShutdownToComplete( 15000, true ); return TRUE; } void COIMOBOXWatchDirService::OnStop() { Report( _T("OnStop") ); /*if(m_pMOBOXWatchDirDlg) { if( ::IsWindow(m_pMOBOXWatchDirDlg->GetSafeHwnd())) m_pMOBOXWatchDirDlg->DestroyWindow(); delete m_pMOBOXWatchDirDlg; m_pMOBOXWatchDirDlg = NULL; Report( _T( " OnStop 2" ) ); }*/ SetEvent( m_hSvcStopEvent ); // Report to the event log that the service has stopped successfully Report( m_sDisplayName,EVENTLOG_INFORMATION_TYPE, CNTS_MSG_SERVICE_STOPPED ); } void COIMOBOXWatchDirService::OnPause() { ReportStatusToSCM( SERVICE_PAUSED, ERROR, 0, 1, 0 ); // Report to the event log that the service has paused successfully Report(m_sDisplayName, EVENTLOG_INFORMATION_TYPE, CNTS_MSG_SERVICE_PAUSED ); } void COIMOBOXWatchDirService::OnContinue() { ReportStatusToSCM( SERVICE_RUNNING, ERROR, 0, 1, 0 ); Report( m_sDisplayName, EVENTLOG_INFORMATION_TYPE, CNTS_MSG_SERVICE_CONTINUED ); // Report to the event log that the service has continued continued } void COIMOBOXWatchDirService::Report( LPCTSTR lpszString, WORD wType/*=EVENTLOG_INFORMATION_TYPE*/ , DWORD dwEventID/*=MSG_SERVICE_INFO*/ ) { m_EventLogSource.Report( wType, dwEventID, lpszString ); }