123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- // WatchService.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "WatchService.h"
- #include "WindowService.h"
- //////////////////////////////////////////////////////////////////////////
- // 包含主要处理类;
- #include "MainProcess.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 唯一的应用程序对象
- CWinApp theApp;
- using namespace std;
- // 工作启动回调函数;
- void CALLBACK WorkStart()
- {
- #ifdef _DEBUG
- Sleep(8000);
- #endif
- WindowsService::GetDebugPriv();
- CMainProcess::GetInstance()->StartWork();
- }
- // 工作结束回调函数;
- void CALLBACK WorkEndof()
- {
- CMainProcess::GetInstance()->EndofWork();
- }
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- int nRetCode = 0;
- // 直接获得前景窗口的句柄;
- HWND hwnd = GetForegroundWindow();
- SendMessage(hwnd , WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL,MAKEINTRESOURCE(IDI_MICON)));
- HMODULE hModule = ::GetModuleHandle(NULL);
- // 初始化 MFC 并在失败时显示错误
- if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
- {
- // TODO: 更改错误代码以符合您的需要
- _tprintf(_T("错误: MFC 初始化失败\n"));
- nRetCode = 1;
- }
- else
- {
- Global::GetIniInfo();
- // 进程存在,直接退出;
- TCHAR szExeName[MAX_PATH] = {0};
- TCHAR szDrive[_MAX_DRIVE] = { 0 };
- TCHAR szDir[_MAX_DIR] = { 0 };
- TCHAR szFna[_MAX_DIR] = { 0 };
- TCHAR szExt[_MAX_DIR] = { 0 };
- _tsplitpath_s(Global::g_szModuleFileName, szDrive, szDir, szFna, szExt);
- _stprintf_s(szExeName, _T("%s%s"), szFna, szExt);
- if ( Global::FindProcessCount(szExeName) > 1)
- {// 进程计数大于1,退出;
- return 0;
- }
- // 设置服务名和描述;
- _stprintf_s(WindowsService::g_szSvcName, _T("Watch Service"));
- _stprintf_s(WindowsService::g_szlpSvrDescription, _T("监控服务"));
- // 1.先判断服务是否存在;
- if ( WindowsService::IsSvcInstalled())
- {
- // 卸载服务;
- if ( argc > 1)
- {
- if (_tcscmp((TCHAR *)argv[1], _T("/uninstall")) == 0)
- WindowsService::DoDeleteSvc(WindowsService::g_szSvcName);
- else
- _tprintf_s(_T("未有卸载服务参数行\n"));
- return 0;
- }
- else
- {
- WindowsService::InitSvcVar();
- WindowsService::SetCallBack(WorkStart, WorkEndof);
- SERVICE_TABLE_ENTRY st[] =
- {
- { WindowsService::g_szSvcName, (LPSERVICE_MAIN_FUNCTION)WindowsService::ServiceMain },
- { NULL, NULL }
- };
- if (!::StartServiceCtrlDispatcher(st)) // 在直接双击.exe时,这步会运行失败;
- {
- //LOG4C((LOG_NOTICE,"服务已安装,须系统运行控制"));
- WindowsService::SvcReportEvent(_T("Register Service Main Function Error!"));
- return 0;
- }
- }
- }
- // 2.不存在,安装服务;
- WindowsService::InitSvcVar();
- WindowsService::SetCallBack(WorkStart, WorkEndof);
- SERVICE_TABLE_ENTRY st[] =
- {
- { WindowsService::g_szSvcName, (LPSERVICE_MAIN_FUNCTION)WindowsService::ServiceMain },
- { NULL, NULL }
- };
- WindowsService::SvcInstall();
- }
- return nRetCode;
- }
|