#include <stdlib.h>
#include <string.h>
#include <stdio.h>
+#include <iostream>
static SERVICE_STATUS_HANDLE serviceStatusHandle;
static HANDLE hThreadEvent;
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;
}
/** 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
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;
}
if (!myService)
{
- printf("Unable to create service: %s\n", RetrieveLastError());
+ std::cout << "Unable to create service: " << RetrieveLastError() << std::endl;
CloseServiceHandle(scm);
return;
}
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;
FreeLibrary(advapi32);
}
- printf("Service installed.\n");
+ std::cout << "Service installed." << std::endl;
CloseServiceHandle(myService);
CloseServiceHandle(scm);
}
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);
}