MailService.cpp
资源名称:SmartMailSvr [点击查看]
上传用户:qdlutongda
上传日期:2007-01-14
资源大小:133k
文件大小:3k
源码类别:
Email客户端
开发平台:
Visual C++
- #include <stdafx.h>
- //#include <windows.h>
- #include "MailService.h"
- extern UINT main_mailsvr(LPVOID pParam);
- extern bool Ini_MailCom();
- extern void Release_MailCom();
- CMailService::CMailService()
- : CNTService(TEXT("SmartMailService"), TEXT("M-Office Smart Mail Server"))
- , m_hStop(0)
- , m_hPause(0)
- , m_hContinue(0)
- {
- m_dwControlsAccepted = 0;
- m_dwControlsAccepted |= SERVICE_ACCEPT_STOP;
- m_dwControlsAccepted |= SERVICE_ACCEPT_PAUSE_CONTINUE;
- m_dwControlsAccepted |= SERVICE_ACCEPT_SHUTDOWN;
- m_dwServiceType |= SERVICE_INTERACTIVE_PROCESS;
- }
- void CMailService::Run(DWORD dwArgc, LPTSTR * ppszArgv) {
- // report to the SCM that we're about to start
- ReportStatus(SERVICE_START_PENDING);
- m_hStop = ::CreateEvent(0, TRUE, FALSE, 0);
- m_hPause = ::CreateEvent(0, TRUE, FALSE, 0);
- m_hContinue = ::CreateEvent(0, TRUE, FALSE, 0);
- // TODO: You might do some initialization here.
- // Parameter processing for instance ...
- // If this initialization takes a long time,
- // don't forget to call "ReportStatus()"
- // frequently or adjust the number of milliseconds
- // in the "ReportStatus()" above.
- // report SERVICE_RUNNING immediately before you enter the main-loop
- // DON'T FORGET THIS!
- ReportStatus(SERVICE_RUNNING);
- //addtional code by Qian Xuezheng
- if(!Ini_MailCom())
- return ;
- DWORD dwThreadID;
- HANDLE hThread = CreateThread(NULL,
- 0,
- (LPTHREAD_START_ROUTINE)main_mailsvr,
- NULL,
- 0,
- &dwThreadID);
- if(hThread == NULL)
- return ;
- DWORD dExitCode=-1;
- // main-loop
- // If the Stop() method sets the event, then we will break out of
- // this loop.
- while( ::WaitForSingleObject(m_hStop, 10) != WAIT_OBJECT_0 ) {
- // if the service got a "pause" request, wait until the user
- // wants to "continue".
- if(dExitCode == -1)
- {
- if(::WaitForSingleObject(hThread,10) == WAIT_OBJECT_0 )
- {
- GetExitCodeThread(hThread,&dExitCode);
- if(dExitCode==0)
- break;
- }
- }
- if(::WaitForSingleObject(m_hPause, 5) == WAIT_OBJECT_0) {
- while(::WaitForSingleObject(m_hContinue, 50) != WAIT_OBJECT_0)
- if(::WaitForSingleObject(m_hPause, 50) == WAIT_OBJECT_0)
- goto Stop;
- ::ResetEvent(m_hPause);
- ::ResetEvent(m_hContinue);
- }
- // TODO: Enter your service's real functionality here
- }
- Stop:
- if( m_hStop )
- {
- ::CloseHandle(m_hStop);
- Release_MailCom();
- }
- if(m_hPause)
- ::CloseHandle(m_hPause);
- if(m_hContinue)
- ::CloseHandle(m_hContinue);
- }
- void CMailService::Stop() {
- // report to the SCM that we're about to stop
- // TODO: Adjust the number of milliseconds you think
- // the stop-operation may take.
- ReportStatus(SERVICE_STOP_PENDING, 5000);
- if( m_hStop )
- ::SetEvent(m_hStop);
- }
- void CMailService::Pause() {
- ReportStatus(SERVICE_PAUSE_PENDING);
- if(m_hPause)
- ::SetEvent(m_hPause);
- // TODO: add additional "pause" code here
- ReportStatus(SERVICE_PAUSED);
- }
- void CMailService::Continue() {
- ReportStatus(SERVICE_CONTINUE_PENDING);
- // TODO: add additional "continue" code here
- if(m_hContinue)
- ::SetEvent(m_hContinue);
- ReportStatus(SERVICE_RUNNING);
- }
- void CMailService::Shutdown() {
- if(m_hStop)
- ::SetEvent(m_hStop);
- // TODO: add code here that will be executed when the system shuts down
- }