/** Copyright (C) 2005-2011 MaNGOS <http://getmangos.com/>* Copyright (C) 2009-2011 MaNGOSZero <https://github.com/mangos/zero>* Copyright (C) 2011-2016 Nostalrius <https://nostalrius.org>* Copyright (C) 2016-2017 Elysium Project <https://github.com/elysium-project>** This program is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of the License, or* (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#ifdef WIN32#include "Common.h"#include "Log.h"#include <cstring>#include <windows.h>#include <winsvc.h>#if !defined(WINADVAPI)#if !defined(_ADVAPI32_)#define WINADVAPI DECLSPEC_IMPORT#else#define WINADVAPI#endif#endif#ifdef main#undef main#endifextern int main(int argc, char ** argv);extern char serviceLongName[];extern char serviceName[];extern char serviceDescription[];extern int m_ServiceStatus;SERVICE_STATUS serviceStatus;SERVICE_STATUS_HANDLE serviceStatusHandle = 0;typedef WINADVAPI BOOL (WINAPI *CSD_T)(SC_HANDLE, DWORD, LPCVOID);bool WinServiceInstall(){CSD_T ChangeService_Config2;HMODULE advapi32;SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);if (!serviceControlManager){sLog.outError("SERVICE: No access to service control manager.");return false;}char path[_MAX_PATH + 10];if (!GetModuleFileName( 0, path, sizeof(path)/sizeof(path[0]))){CloseServiceHandle(serviceControlManager);sLog.outError("SERVICE: Can't get service binary filename.");return false;}std::strcat(path, " -s run");SC_HANDLE service = CreateService(serviceControlManager,serviceName, // name of serviceserviceLongName, // service name to displaySERVICE_ALL_ACCESS, // desired access// service typeSERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,SERVICE_AUTO_START, // start typeSERVICE_ERROR_IGNORE, // error control typepath, // service's binary0, // no load ordering group0, // no tag identifier0, // no dependencies0, // LocalSystem account0); // no passwordif (!service){CloseServiceHandle(serviceControlManager);sLog.outError("SERVICE: Can't register service for: %s", path);return false;}advapi32 = GetModuleHandle("ADVAPI32.DLL");if(!advapi32){sLog.outError("SERVICE: Can't access ADVAPI32.DLL");CloseServiceHandle(service);CloseServiceHandle(serviceControlManager);return false;}ChangeService_Config2 = (CSD_T) GetProcAddress(advapi32, "ChangeServiceConfig2A");if (!ChangeService_Config2){sLog.outError("SERVICE: Can't access ChangeServiceConfig2A from ADVAPI32.DLL");CloseServiceHandle(service);CloseServiceHandle(serviceControlManager);return false;}SERVICE_DESCRIPTION sdBuf;sdBuf.lpDescription = serviceDescription;ChangeService_Config2(service, // handle to serviceSERVICE_CONFIG_DESCRIPTION, // change: description&sdBuf); // new dataSC_ACTION _action[1];_action[0].Type = SC_ACTION_RESTART;_action[0].Delay = 10000;SERVICE_FAILURE_ACTIONS sfa;ZeroMemory(&sfa, sizeof(SERVICE_FAILURE_ACTIONS));sfa.lpsaActions = _action;sfa.cActions = 1;sfa.dwResetPeriod =INFINITE;ChangeService_Config2(service, // handle to serviceSERVICE_CONFIG_FAILURE_ACTIONS, // information level&sfa); // new dataCloseServiceHandle(service);CloseServiceHandle(serviceControlManager);return true;}bool WinServiceUninstall(){SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT);if (!serviceControlManager){sLog.outError("SERVICE: No access to service control manager.");return false;}SC_HANDLE service = OpenService(serviceControlManager,serviceName, SERVICE_QUERY_STATUS | DELETE);if (!service){CloseServiceHandle(serviceControlManager);sLog.outError("SERVICE: Service not found: %s", serviceName);return false;}SERVICE_STATUS serviceStatus2;if (QueryServiceStatus(service, &serviceStatus2)){if (serviceStatus2.dwCurrentState == SERVICE_STOPPED)DeleteService(service);}CloseServiceHandle(service);CloseServiceHandle(serviceControlManager);return true;}void WINAPI ServiceControlHandler(DWORD controlCode){switch (controlCode){case SERVICE_CONTROL_INTERROGATE:break;case SERVICE_CONTROL_SHUTDOWN:case SERVICE_CONTROL_STOP:serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;SetServiceStatus(serviceStatusHandle, &serviceStatus);m_ServiceStatus = 0;return;case SERVICE_CONTROL_PAUSE:m_ServiceStatus = 2;serviceStatus.dwCurrentState = SERVICE_PAUSED;SetServiceStatus(serviceStatusHandle, &serviceStatus);break;case SERVICE_CONTROL_CONTINUE:serviceStatus.dwCurrentState = SERVICE_RUNNING;SetServiceStatus(serviceStatusHandle, &serviceStatus);m_ServiceStatus = 1;break;default:if ( controlCode >= 128 && controlCode <= 255 )// user defined control codebreak;else// unrecognized control codebreak;}SetServiceStatus(serviceStatusHandle, &serviceStatus);}void WINAPI ServiceMain(DWORD argc, char *argv[]){// initialise service statusserviceStatus.dwServiceType = SERVICE_WIN32;serviceStatus.dwCurrentState = SERVICE_START_PENDING;serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;serviceStatus.dwWin32ExitCode = NO_ERROR;serviceStatus.dwServiceSpecificExitCode = NO_ERROR;serviceStatus.dwCheckPoint = 0;serviceStatus.dwWaitHint = 0;serviceStatusHandle = RegisterServiceCtrlHandler(serviceName, ServiceControlHandler);if ( serviceStatusHandle ){char path[_MAX_PATH + 1];unsigned int i, last_slash = 0;GetModuleFileName(0, path, sizeof(path)/sizeof(path[0]));for (i = 0; i < std::strlen(path); i++){if (path[i] == '\\') last_slash = i;}path[last_slash] = 0;// service is startingserviceStatus.dwCurrentState = SERVICE_START_PENDING;SetServiceStatus(serviceStatusHandle, &serviceStatus);// do initialisation hereSetCurrentDirectory(path);// runningserviceStatus.dwControlsAccepted |= (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);serviceStatus.dwCurrentState = SERVICE_RUNNING;SetServiceStatus( serviceStatusHandle, &serviceStatus );////////////////////////// service main cycle //////////////////////////m_ServiceStatus = 1;argc = 1;main(argc , argv);// service was stoppedserviceStatus.dwCurrentState = SERVICE_STOP_PENDING;SetServiceStatus(serviceStatusHandle, &serviceStatus);// do cleanup here// service is now stoppedserviceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);serviceStatus.dwCurrentState = SERVICE_STOPPED;SetServiceStatus(serviceStatusHandle, &serviceStatus);}}bool WinServiceRun(){SERVICE_TABLE_ENTRY serviceTable[] ={{ serviceName, ServiceMain },{ 0, 0 }};if (!StartServiceCtrlDispatcher(serviceTable)){sLog.outError("StartService Failed. Error [%u]", ::GetLastError());return false;}return true;}#endif
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。