?? mosmgpservice.cpp
字號:
// MOSmgpService.cpp : 定義應(yīng)用程序的類行為。
//
#include "stdafx.h"
#include "MOSmgpService.h"
//#include "MOSmgpServiceDlg.h"
#include "MOSmgpThread.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMOSmgpServiceApp
BEGIN_MESSAGE_MAP(CMOSmgpServiceApp, CWinApp)
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
// CMOSmgpServiceApp 構(gòu)造
CMOSmgpServiceApp::CMOSmgpServiceApp()
{
// TODO: 在此處添加構(gòu)造代碼,
// 將所有重要的初始化放置在 InitInstance 中
}
void MOTrace(CString str){
#ifdef _DEBUG
str+= "\r\n";
CString strFilename = "c:\\TELEMOService.txt";
CFileStatus status;
CTime time = CTime::GetCurrentTime();
CString strTime;
strTime.Format("*******%04d-%02d-%02d %02d:%02d:%02d ****",
time.GetYear(),time.GetMonth(),time.GetDay(),
time.GetHour(),time.GetMinute(),time.GetSecond());
if(CFile::GetStatus(strFilename,status)){
CFile f(strFilename,CFile::modeReadWrite|CFile::modeNoTruncate);
f.SeekToEnd();
f.Write(strTime.GetBuffer(0),strTime.GetLength());
f.Write(str.GetBuffer(0),str.GetLength());
f.Close();
}
else{
CFile f(strFilename,CFile::modeCreate|CFile::modeWrite);
f.Write(strTime.GetBuffer(0),strTime.GetLength());
f.Write(str.GetBuffer(0),str.GetLength());
f.Close();
}
#endif
}
// 唯一的一個 CMOSmgpServiceApp 對象
CMOSmgpServiceApp theApp;
// CMOSmgpServiceApp 初始化
BOOL CMOSmgpServiceApp::InitInstance()
{
//// 如果一個運行在 Windows XP 上的應(yīng)用程序清單指定要
//// 使用 ComCtl32.dll 版本 6 或更高版本來啟用可視化方式,
////則需要 InitCommonControls()。否則,將無法創(chuàng)建窗口。
//InitCommonControls();
//CWinApp::InitInstance();
//AfxEnableControlContainer();
//CMOSmgpServiceDlg dlg;
//m_pMainWnd = &dlg;
//INT_PTR nResponse = dlg.DoModal();
//if (nResponse == IDOK)
//{
// // TODO:在此放置處理何時用“確定”來關(guān)閉
// //對話框的代碼
//}
//else if (nResponse == IDCANCEL)
//{
// // TODO:在此放置處理何時用“取消”來關(guān)閉
// //對話框的代碼
//}
//// 由于對話框已關(guān)閉,所以將返回 FALSE 以便退出應(yīng)用程序,
//// 而不是啟動應(yīng)用程序的消息泵。
//return FALSE;
// 如果一個運行在 Windows XP 上的應(yīng)用程序清單指定要
// 使用 ComCtl32.dll 版本 6 或更高版本來啟用可視化方式,
//則需要 InitCommonControls()。否則,將無法創(chuàng)建窗口。
//MOTrace("START MOSMGP SERVICE>>>>>>>>>>>>>>>>>>>>");
InitCommonControls();
CWinApp::InitInstance();
AfxEnableControlContainer();
CNTServiceParam param;//程序運行參數(shù)
param.Parse();
CString strServiceName="TELEMOService";//定義MO的服務(wù)名稱
if(!param.HadParam()){//沒有運行參數(shù),啟動服務(wù)
CMOSmgpService service;
int iCode = CNTService::Run(&service,strServiceName);//運行該服務(wù),得到返回值
if(iCode!=ERROR_OK){
AfxMessageBox("運行失敗\n");
}
}
else{
//得到參數(shù),判斷參數(shù)的類型,執(zhí)行安裝或卸載服務(wù)。
CString strParam = param.GetAt(0);
strParam.MakeLower();
if(strParam=="install"){
TCHAR path[MAX_PATH];
::GetModuleFileName(NULL,path,MAX_PATH);
SERV_INFO info;
ZeroMemory(&info,sizeof(info));
info.enStartType = Manual;//設(shè)置為手動啟動服務(wù)
strcat(info.tcServiceDesc,"接收中國電信的上行短信息,信息存放到MO表和日志表中。作者:一笑,阿杜");
strcat(info.tcServiceDisplayName,"TELEMOService");//"SMS CMPP MO Service"
strcat(info.tcServicePath,path);
int iCode = ::ServiceInstall(strServiceName,info);
CString strMsg;
if(iCode!=ERROR_OK)
{
strMsg.Format("安裝失敗,錯誤代碼:%d\n",iCode);
}
else
{
SetServieStartType(strServiceName,Auto);
strMsg.Format("安裝成功,服務(wù)名稱: %s\n",info.tcServiceDisplayName);
//MOTrace(strMsg+">>>>>>>>>>>>>>>>>>>>>>>>>");
}
AfxMessageBox(strMsg);
}
else if(strParam=="uninstall"){
int iCode=::ServiceUninstall(strServiceName);
CString strMsg;
if(iCode!=ERROR_OK)
strMsg.Format("卸載失敗,錯誤代碼:%d\n",iCode);
else
strMsg.Format("卸載成功\n");
AfxMessageBox(strMsg);
}
else
AfxMessageBox(_T("參數(shù)錯誤,只支持-install,-uninstall\n"));
}
return FALSE;
}
void CMOSmgpService::OnStart(){
m_pThread = ::AfxBeginThread(RUNTIME_CLASS(CMOSmgpThread),THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
m_pThread->ResumeThread();
}
void CMOSmgpService::OnStop(){
//TerminateThread(m_pThread->m_hThread,0xdead);
DWORD ExitCode;
GetExitCodeThread(m_pThread->m_hThread,&ExitCode);
TerminateThread(m_pThread->m_hThread,ExitCode);
}
void CMOSmgpService::OnPause(){
if(this->m_pThread)this->m_pThread->SuspendThread();
}
void CMOSmgpService::OnContinue(){
if(this->m_pThread)this->m_pThread->ResumeThread();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -