]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - win/win32service.cpp
Merge pull request #437 from SaberUK/insp20+doxygen-update
[user/henk/code/inspircd.git] / win / win32service.cpp
index b677b6662e5d28cf6296a0683028f976a3b42569..0a4b0c5b4d51a931eac8d6fd461f12ad71e0ffd8 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#include <iostream>
 
 static SERVICE_STATUS_HANDLE serviceStatusHandle;
 static HANDLE hThreadEvent;
@@ -54,7 +55,9 @@ 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);
+       DWORD LastError = GetLastError();
+       if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, 0, LastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)err, sizeof(err), 0) == 0)
+               snprintf(err, sizeof(err), "Error code: %d", LastError);
        SetLastError(ERROR_SUCCESS);
        return err;
 }
@@ -110,8 +113,9 @@ void SetServiceRunning()
 /** Starts the worker thread above */
 void StartServiceThread()
 {
-       DWORD dwd;
-       CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WorkerThread,NULL,0,&dwd);
+       HANDLE hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WorkerThread,NULL,0,NULL);
+       if (hThread != NULL)
+               CloseHandle(hThread);
 }
 
 /** This function updates the status of the service in the SCM
@@ -244,7 +248,7 @@ void InstallService()
        scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
        if (!scm)
        {
-               printf("Unable to open service control manager: %s\n", RetrieveLastError());
+               std::cout << "Unable to open service control manager: " << RetrieveLastError() << std::endl;
                return;
        }
 
@@ -253,7 +257,7 @@ void InstallService()
 
        if (!myService)
        {
-               printf("Unable to create service: %s\n", RetrieveLastError());
+               std::cout << "Unable to create service: " << RetrieveLastError() << std::endl;
                CloseServiceHandle(scm);
                return;
        }
@@ -274,7 +278,7 @@ void InstallService()
                        BOOL success = ChangeServiceConf(myService,SERVICE_CONFIG_DESCRIPTION, &svDesc);
                        if (!success)
                        {
-                               printf("Unable to set service description: %s\n", RetrieveLastError());
+                               std::cout << "Unable to set service description: " << RetrieveLastError() << std::endl;
                                CloseServiceHandle(myService);
                                CloseServiceHandle(scm);
                                return;
@@ -283,7 +287,7 @@ void InstallService()
                FreeLibrary(advapi32);
        }
 
-       printf("Service installed.\n");
+       std::cout << "Service installed." << std::endl;
        CloseServiceHandle(myService);
        CloseServiceHandle(scm);
 }
@@ -296,27 +300,27 @@ void RemoveService()
        scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
        if (!scm)
        {
-               printf("Unable to open service control manager: %s\n", RetrieveLastError());
+               std::cout << "Unable to open service control manager: " << RetrieveLastError() << std::endl;
                return;
        }
 
        myService = OpenService(scm,TEXT("InspIRCd"),SERVICE_ALL_ACCESS);
        if (!myService)
        {
-               printf("Unable to open service: %s\n", RetrieveLastError());
+               std::cout << "Unable to open service: " << RetrieveLastError() << std::endl;
                CloseServiceHandle(scm);
                return;
        }
 
        if (!DeleteService(myService))
        {
-               printf("Unable to delete service: %s\n", RetrieveLastError());
+               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);
 }