// WxService.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "WxService.h" #include "MainProcess.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // 唯一的应用程序对象 CWinApp theApp; using namespace std; // 工作启动回调函数; void CALLBACK WorkStart() { if (CreatePoolInstance()) { // 尝试10次启动; INT i = 10; while (0 == g_pAdoPool->InitializePool(Global::g_szDBType, Global::g_szDBSource, Global::g_dwDBServerPort, Global::g_szDBAccount, Global::g_szDBPassWord, Global::g_szDBName, Global::g_dwDBPoolDef, Global::g_dwDBPoolMaxCount)) { Sleep(5000); if (i < 0) { break; } i--; } } #ifdef _DEBUG tb_customer customer; TCHAR szMax[MAX_PATH] = {0}; TString str; g_pAdoPool->IsUserExist(_T("15089231318"), _T("123456"), (LPVOID)&customer); #endif WinService::GetDebugPriv(); CMainProcess::GetInstance()->StartWork(); } // 工作结束回调函数; void CALLBACK WorkEndof() { FreelyfzodbcLibrary(); CMainProcess *pObj = CMainProcess::GetInstance(); if (pObj) { pObj->EndofWork(); delete pObj; pObj = NULL; } } int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; //_CrtSetBreakAlloc(582); // 直接获得前景窗口的句柄; 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; } #ifdef _DEBUG WorkStart(); system("pause"); WorkEndof(); #else // 设置服务名和描述; _stprintf_s(WinService::g_scVariant.scName, _T("WxService")); _stprintf_s(WinService::g_scVariant.scDisplayName, _T("WxService")); _stprintf_s(WinService::g_scVariant.scDescription, _T("微信服务")); // 1.先判断服务是否存在; if ( WinService::IsInstalled()) { // 卸载服务; if ( argc > 1) { if (_tcscmp((TCHAR *)argv[1], _T("/uninstall")) == 0) WinService::DoDeleteSvc(WinService::g_scVariant.scName); else printf(_T("未有卸载服务参数行\n")); system("pause"); return 0; } else { WinService::SetCallBack(WorkStart, WorkEndof); SERVICE_TABLE_ENTRY st[] = { { WinService::g_scVariant.scName, (LPSERVICE_MAIN_FUNCTION)WinService::ServiceMain }, { NULL, NULL } }; if (!::StartServiceCtrlDispatcher(st)) // 在直接双击.exe时,这步会运行失败; { WinService::SvcReportEvent(_T("Register Service Main Function Error!")); printf(_T("双击无法安装\n")); system("pause"); return 0; } } } // 2.不存在,安装服务; WinService::SetCallBack(WorkStart, WorkEndof); SERVICE_TABLE_ENTRY st[] = { { WinService::g_scVariant.scName, (LPSERVICE_MAIN_FUNCTION)WinService::ServiceMain }, { NULL, NULL } }; WinService::Install(); #endif } return nRetCode; }