2002-06-04 12:26:23 +00:00
|
|
|
#ifndef CWIN32PLATFORM_H
|
|
|
|
#define CWIN32PLATFORM_H
|
|
|
|
|
|
|
|
#include "IPlatform.h"
|
2002-06-08 21:48:00 +00:00
|
|
|
#include "CCondVar.h"
|
|
|
|
#include "CMutex.h"
|
2002-06-10 16:49:46 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2002-06-08 21:48:00 +00:00
|
|
|
#include <windows.h>
|
2002-06-04 12:26:23 +00:00
|
|
|
|
|
|
|
class CWin32Platform : public IPlatform {
|
|
|
|
public:
|
2002-06-08 21:48:00 +00:00
|
|
|
typedef int (*RunFunc)(CMutex*);
|
|
|
|
typedef void (*StopFunc)(void);
|
|
|
|
|
2002-06-04 12:26:23 +00:00
|
|
|
CWin32Platform();
|
|
|
|
virtual ~CWin32Platform();
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
// returns true iff the platform is win95/98/me
|
|
|
|
static bool isWindows95Family();
|
|
|
|
|
|
|
|
// utility for calling SetServiceStatus()
|
|
|
|
static void setStatus(SERVICE_STATUS_HANDLE, DWORD state);
|
|
|
|
static void setStatus(SERVICE_STATUS_HANDLE,
|
2002-06-10 22:06:45 +00:00
|
|
|
DWORD state, DWORD step, DWORD waitHint);
|
2002-06-08 21:48:00 +00:00
|
|
|
static void setStatusError(SERVICE_STATUS_HANDLE, DWORD error);
|
|
|
|
|
|
|
|
// run a service. the RunFunc should unlock the passed in mutex
|
|
|
|
// (which will be locked on entry) when not initializing or
|
|
|
|
// shutting down (i.e. when running its loop). StopFunc should
|
|
|
|
// cause the RunFunc() to return. returns what RunFunc returns.
|
|
|
|
// RunFunc should throw CDaemonFailed if the service fails.
|
|
|
|
int runDaemon(RunFunc, StopFunc);
|
|
|
|
|
|
|
|
// thrown by RunFunc on service failure. result is the error
|
|
|
|
// code reported by the service.
|
|
|
|
class CDaemonFailed {
|
|
|
|
public:
|
|
|
|
CDaemonFailed(int result) : m_result(result) { }
|
|
|
|
|
|
|
|
public:
|
|
|
|
int m_result;
|
|
|
|
};
|
|
|
|
|
2002-06-04 12:26:23 +00:00
|
|
|
// IPlatform overrides
|
2002-06-08 21:48:00 +00:00
|
|
|
virtual bool installDaemon(const char* name,
|
2002-06-10 22:06:45 +00:00
|
|
|
const char* description,
|
|
|
|
const char* pathname,
|
|
|
|
const char* commandLine);
|
2002-06-14 18:08:20 +00:00
|
|
|
virtual EResult uninstallDaemon(const char* name);
|
2002-06-08 21:48:00 +00:00
|
|
|
virtual int daemonize(const char* name, DaemonFunc);
|
2002-06-14 18:08:20 +00:00
|
|
|
virtual void installDaemonLogger(const char* name);
|
2002-06-04 12:26:23 +00:00
|
|
|
virtual const char* getBasename(const char* pathname) const;
|
|
|
|
virtual CString getUserDirectory() const;
|
|
|
|
virtual CString getSystemDirectory() const;
|
|
|
|
virtual CString addPathComponent(
|
2002-06-10 22:06:45 +00:00
|
|
|
const CString& prefix,
|
|
|
|
const CString& suffix) const;
|
2002-06-04 12:26:23 +00:00
|
|
|
|
|
|
|
private:
|
2002-06-08 21:48:00 +00:00
|
|
|
static HKEY openKey(HKEY parent, const char*);
|
|
|
|
static HKEY openKey(HKEY parent, const char**);
|
|
|
|
static void closeKey(HKEY);
|
|
|
|
static void deleteKey(HKEY, const char* name);
|
|
|
|
static void deleteValue(HKEY, const char* name);
|
|
|
|
static void setValue(HKEY, const char* name,
|
2002-06-10 22:06:45 +00:00
|
|
|
const CString& value);
|
2002-06-08 21:48:00 +00:00
|
|
|
static CString readValueString(HKEY, const char* name);
|
|
|
|
static HKEY openNTServicesKey();
|
|
|
|
static HKEY open95ServicesKey();
|
|
|
|
|
|
|
|
void serviceMain(DWORD, LPTSTR*);
|
|
|
|
static void WINAPI serviceMainEntry(DWORD, LPTSTR*);
|
|
|
|
|
2002-07-24 19:26:18 +00:00
|
|
|
void runDaemonThread(void*);
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
void serviceHandler(DWORD ctrl);
|
|
|
|
static void WINAPI serviceHandlerEntry(DWORD ctrl);
|
|
|
|
|
|
|
|
static bool serviceLogger(int, const char*);
|
|
|
|
|
|
|
|
private:
|
|
|
|
DaemonFunc m_daemonFunc;
|
|
|
|
int m_daemonResult;
|
|
|
|
|
|
|
|
SERVICE_STATUS_HANDLE m_statusHandle;
|
|
|
|
|
|
|
|
CMutex* m_serviceMutex;
|
|
|
|
CCondVar<DWORD>* m_serviceState;
|
|
|
|
bool m_serviceHandlerWaiting;
|
|
|
|
bool m_serviceRunning;
|
|
|
|
StopFunc m_stop;
|
|
|
|
|
|
|
|
static HANDLE s_eventLog;
|
|
|
|
|
|
|
|
static CWin32Platform* s_daemonPlatform;
|
2002-06-04 12:26:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|