📄 mosmgpservice.cpp
字号:
// MOSmgpService.cpp : 定义应用程序的类行为。
//
#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 构造
CMOSmgpServiceApp::CMOSmgpServiceApp()
{
// TODO: 在此处添加构造代码,
// 将所有重要的初始化放置在 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 上的应用程序清单指定要
//// 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
////则需要 InitCommonControls()。否则,将无法创建窗口。
//InitCommonControls();
//CWinApp::InitInstance();
//AfxEnableControlContainer();
//CMOSmgpServiceDlg dlg;
//m_pMainWnd = &dlg;
//INT_PTR nResponse = dlg.DoModal();
//if (nResponse == IDOK)
//{
// // TODO:在此放置处理何时用“确定”来关闭
// //对话框的代码
//}
//else if (nResponse == IDCANCEL)
//{
// // TODO:在此放置处理何时用“取消”来关闭
// //对话框的代码
//}
//// 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序,
//// 而不是启动应用程序的消息泵。
//return FALSE;
// 如果一个运行在 Windows XP 上的应用程序清单指定要
// 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
//则需要 InitCommonControls()。否则,将无法创建窗口。
//MOTrace("START MOSMGP SERVICE>>>>>>>>>>>>>>>>>>>>");
InitCommonControls();
CWinApp::InitInstance();
AfxEnableControlContainer();
CNTServiceParam param;//程序运行参数
param.Parse();
CString strServiceName="TELEMOService";//定义MO的服务名称
if(!param.HadParam()){//没有运行参数,启动服务
CMOSmgpService service;
int iCode = CNTService::Run(&service,strServiceName);//运行该服务,得到返回值
if(iCode!=ERROR_OK){
AfxMessageBox("运行失败\n");
}
}
else{
//得到参数,判断参数的类型,执行安装或卸载服务。
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;//设置为手动启动服务
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("安装成功,服务名称: %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("参数错误,只支持-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();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -