X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=win%2Fwin32service.cpp;h=81f8e7516e6f6becb85df2f1451900ba44bf8291;hb=a589577b68cb84bc877ecdd4c0f9cb84a1581ddd;hp=1ef27a4c7419dade95efbe121b1338387acba2ea;hpb=699a8b2fc82949bfd5a39acc5b00670a5c350b4d;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/win/win32service.cpp b/win/win32service.cpp index 1ef27a4c7..81f8e7516 100644 --- a/win/win32service.cpp +++ b/win/win32service.cpp @@ -1,15 +1,22 @@ -/* +------------------------------------+ - * | Inspire Internet Relay Chat Daemon | - * +------------------------------------+ +/* + * InspIRCd -- Internet Relay Chat Daemon * - * InspIRCd: (C) 2002-2011 InspIRCd Development Team - * See: http://wiki.inspircd.org/Credits + * Copyright (C) 2008 Craig Edwards * - * This program is free but copyrighted software; see - * the file COPYING for details. + * This file is part of InspIRCd. InspIRCd 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, version 2. * - * --------------------------------------------------- + * 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, see . */ + + #include "inspircd_config.h" #include "inspircd.h" #include "exitcodes.h" @@ -17,6 +24,7 @@ #include #include #include +#include static SERVICE_STATUS_HANDLE serviceStatusHandle; static HANDLE hThreadEvent; @@ -44,6 +52,14 @@ struct Commandline /* A function pointer for dynamic linking tricks */ SETSERVDESC ChangeServiceConf; +LPCSTR RetrieveLastError() +{ + static char err[100]; + FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)err, sizeof(err), 0); + SetLastError(ERROR_SUCCESS); + return err; +} + /* Returns true if this program is running as a service, false if it is running interactive */ bool IsAService() { @@ -69,7 +85,7 @@ void KillService() DWORD WINAPI WorkerThread(LPDWORD param) { char modname[MAX_PATH]; - GetModuleFileName(NULL, modname, sizeof(modname)); + GetModuleFileNameA(NULL, modname, sizeof(modname)); char* argv[] = { modname, "--nofork" }; smain(2, argv); KillService(); @@ -182,7 +198,7 @@ VOID ServiceMain(DWORD argc, LPTSTR *argv) { BOOL success; - serviceStatusHandle = RegisterServiceCtrlHandler("InspIRCd", (LPHANDLER_FUNCTION)ServiceCtrlHandler); + serviceStatusHandle = RegisterServiceCtrlHandler(TEXT("InspIRCd"), (LPHANDLER_FUNCTION)ServiceCtrlHandler); if (!serviceStatusHandle) { terminateService(EXIT_STATUS_RSCH_FAILED, GetLastError()); @@ -223,22 +239,22 @@ void InstallService() SERVICE_DESCRIPTION svDesc; HINSTANCE advapi32; - char modname[MAX_PATH]; + TCHAR modname[MAX_PATH]; GetModuleFileName(NULL, modname, sizeof(modname)); scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE); if (!scm) { - printf("Unable to open service control manager: %s\n", dlerror()); + std::cout << "Unable to open service control manager: " << RetrieveLastError() << std::endl; return; } - myService = CreateService(scm,"InspIRCd","Inspire IRC Daemon", SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, + myService = CreateService(scm,TEXT("InspIRCd"),TEXT("Inspire IRC Daemon"), SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, modname, 0, 0, 0, NULL, NULL); if (!myService) { - printf("Unable to create service: %s\n", dlerror()); + std::cout << "Unable to create service: " << RetrieveLastError() << std::endl; CloseServiceHandle(scm); return; } @@ -247,19 +263,19 @@ void InstallService() // this is supported from 5.0 (win2k) onwards only, so we can't link to the definition of // this function in advapi32.lib, otherwise the program will not run on windows NT 4. We // must use LoadLibrary and GetProcAddress to export the function name from advapi32.dll - advapi32 = LoadLibrary("advapi32.dll"); + advapi32 = LoadLibrary(TEXT("advapi32.dll")); if (advapi32) { ChangeServiceConf = (SETSERVDESC)GetProcAddress(advapi32,"ChangeServiceConfig2A"); if (ChangeServiceConf) { - char desc[] = "The Inspire Internet Relay Chat Daemon hosts IRC channels and conversations.\ - If this service is stopped, the IRC server will not run."; + TCHAR desc[] = TEXT("The Inspire Internet Relay Chat Daemon hosts IRC channels and conversations.\ + If this service is stopped, the IRC server will not run."); svDesc.lpDescription = desc; BOOL success = ChangeServiceConf(myService,SERVICE_CONFIG_DESCRIPTION, &svDesc); if (!success) { - printf("Unable to set service description: %s\n", dlerror()); + std::cout << "Unable to set service description: " << RetrieveLastError() << std::endl; CloseServiceHandle(myService); CloseServiceHandle(scm); return; @@ -268,7 +284,7 @@ void InstallService() FreeLibrary(advapi32); } - printf("Service installed.\n"); + std::cout << "Service installed." << std::endl; CloseServiceHandle(myService); CloseServiceHandle(scm); } @@ -281,27 +297,27 @@ void RemoveService() scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE); if (!scm) { - printf("Unable to open service control manager: %s\n", dlerror()); + std::cout << "Unable to open service control manager: " << RetrieveLastError() << std::endl; return; } - myService = OpenService(scm,"InspIRCd",SERVICE_ALL_ACCESS); + myService = OpenService(scm,TEXT("InspIRCd"),SERVICE_ALL_ACCESS); if (!myService) { - printf("Unable to open service: %s\n", dlerror()); + std::cout << "Unable to open service: " << RetrieveLastError() << std::endl; CloseServiceHandle(scm); return; } if (!DeleteService(myService)) { - printf("Unable to delete service: %s\n", dlerror()); + std::cout << "Unable to delete service: " << RetrieveLastError() << std::endl; CloseServiceHandle(myService); CloseServiceHandle(scm); return; } - printf("Service removed.\n"); + std::cout << "Service removed." << std::endl; CloseServiceHandle(myService); CloseServiceHandle(scm); } @@ -338,7 +354,7 @@ int main(int argc, char** argv) scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE); if (scm) { - myService = OpenService(scm,"InspIRCd",SERVICE_ALL_ACCESS); + myService = OpenService(scm,TEXT("InspIRCd"),SERVICE_ALL_ACCESS); if (!myService) { /* Service not installed or no permission to modify it */ @@ -365,7 +381,7 @@ int main(int argc, char** argv) SERVICE_TABLE_ENTRY serviceTable[] = { - {"InspIRCd", (LPSERVICE_MAIN_FUNCTION) ServiceMain }, + {TEXT("InspIRCd"), (LPSERVICE_MAIN_FUNCTION) ServiceMain }, {NULL, NULL} };