Skip to content

Instantly share code, notes, and snippets.

@code-walkers
Created January 29, 2010 20:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save code-walkers/290097 to your computer and use it in GitHub Desktop.
Save code-walkers/290097 to your computer and use it in GitHub Desktop.
Writing win32 Service Application
/*The win32 service runs with its current working directory set to C:\Windows\System32
*You might need to place anyrun-time configuration scripts there.
*If you run into any confusion with the above detail, it is worthwhile to
*set the current working directory to your application installation directory like I did
*/
MyApp::OnInit()
{
//
//
//
//
//
#if defined(WIN32SERVICE)
SetCurrentDirectory("C:\\Program Files\\MyDir");
MyService *service = new MyService();
if (service) {
service->Initialize();
//service->Delete();
}
#endif /*Win32SERVICE*/
//
//
//
}
#include "stdwx.h"
#if defined(WIN32SERVICE)
#include "MyWin32Service.h"
#define LOGFILE "MyServerServiceLog.log"
#define SLEEP_TIME 5000
static void ServiceMain(int argc, char** argv) ;
static void ControlHandler(DWORD request) ;
SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE hStatus;
static int WriteToLog(char* str)
{
FILE* log;
log = fopen(LOGFILE, "a+");
if (log == NULL)
return -1;
fprintf(log, "%s\n", str);
fclose(log);
return 0;
}
static int InitService()
{
int result;
result = WriteToLog("MyService thread STARTED.");
return(result);
}
static void ControlHandler(DWORD request)
{
switch(request)
{
case SERVICE_CONTROL_STOP:
WriteToLog("MyService thread STOPPED.");
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
case SERVICE_CONTROL_SHUTDOWN:
WriteToLog("MyService thread STOPPED");
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
default:
break;
}
// Report current status
SetServiceStatus (hStatus, &ServiceStatus);
return;
}
void MyService::Initialize()
{
if (wxThread::Create() != wxTHREAD_NO_ERROR) {
wxLogError(wxT("MyService failed to create thread"));
} else {
if (this->Run() != wxTHREAD_NO_ERROR) {
wxLogError(wxT("MyService failed to execute thread"));
this->Delete();
}
}
return;
}
void ServiceMain(int argc, char** argv)
{
int error;
ServiceStatus.dwServiceType =
SERVICE_WIN32;
ServiceStatus.dwCurrentState =
SERVICE_START_PENDING;
ServiceStatus.dwControlsAccepted =
SERVICE_ACCEPT_STOP |
SERVICE_ACCEPT_SHUTDOWN;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
hStatus = RegisterServiceCtrlHandler(
"MyServer",
(LPHANDLER_FUNCTION)ControlHandler);
if (hStatus == (SERVICE_STATUS_HANDLE)0)
{
// Registering Control Handler failed
return;
}
// Initialize Service
error = InitService();
if (error)
{
// Initialization failed
ServiceStatus.dwCurrentState =
SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
// We report the running status to SCM.
ServiceStatus.dwCurrentState =
SERVICE_RUNNING;
SetServiceStatus (hStatus, &ServiceStatus);
// The worker loop of a service
while (ServiceStatus.dwCurrentState ==
SERVICE_RUNNING)
{
_SYSTEMTIME lpSystemTime;
char buffer[64];
GetLocalTime(&lpSystemTime);
sprintf(buffer,"%d-%d-%d:: %d:%d:%d",(unsigned int)lpSystemTime.wDay,(unsigned int)lpSystemTime.wMonth,(unsigned int)lpSystemTime.wYear,(unsigned int)lpSystemTime.wHour,(unsigned int)lpSystemTime.wMinute,(unsigned int)lpSystemTime.wSecond);
int result = WriteToLog(buffer);
if (result)
{
ServiceStatus.dwCurrentState =
SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus,
&ServiceStatus);
return;
}
Sleep(SLEEP_TIME);
}
return;
}
/*---------------------------------------------------------------------------*/
void *MyService::Entry()
{
SERVICE_TABLE_ENTRY ServiceTable[2];
ServiceTable[0].lpServiceName = "MyServer";
ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
ServiceTable[1].lpServiceName = NULL;
ServiceTable[1].lpServiceProc = NULL;
// Start the control dispatcher thread for our service
StartServiceCtrlDispatcher(ServiceTable);
return NULL;
}
#endif /*WIN32SERVICE*/
/*---------------------------------------------------------------------------*/
/*
Local Variables:
c-basic-offset: 4
tab-width: 8
End:
*/
/*---------------------------------------------------------------------------*/
/*> EOF MyWin32Service.cpp <*/
#ifndef __mywin32service_h
#ifndef DO_NOT_DOCUMENT
#define __mywin32service_h (1)
#endif /* !DO_NOT_DOCUMENT */
#if defined(WIN32SERVICE)
#include <windows.h>
#include <stdio.h>
class MyService: public wxThread
{
public:
int InitService();
MyService() :wxThread() {};
void Initialize();
protected:
void *Entry();
};
#endif /*WIN32SERVICE*/
#endif /*__mywin32service_h*/
/*---------------------------------------------------------------------------*/
/*
Local Variables:
c-basic-offset: 4
tab-width: 8
End:
*/
/*---------------------------------------------------------------------------*/
/*> EOF MyWin32Service.h <*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment