#ifndef __WINDOWS_SERVICE_20160113__
#define __WINDOWS_SERVICE_20160113__

#include <WinSvc.h>

namespace WinService
{
	// 工作启动回调函数;
	typedef void (CALLBACK *WorkStartCallback)();
	// 工作结束回调函数;
	typedef void (CALLBACK *WorkEndofCallback)();

	typedef struct SCVARIANT {
		TCHAR					scName[MAX_PATH];					// 服务名称;
		TCHAR					scDisplayName[MAX_PATH];			// 服务显示名称;
		TCHAR					scDescription[MAX_PATH];			// 服务描述内容;
		HANDLE					hscEvent;							// 服务控制事件;
		SERVICE_STATUS			scStatus;							// 服务状态;
		SERVICE_STATUS_HANDLE	scStatusHandle;						// 服务状态句柄;
		WorkStartCallback		lpStartWorkCallBack;				// 开启工作者;
		WorkEndofCallback		lpEndofWorkCallBack;				// 结束工作者;

		SCVARIANT()
		{
			hscEvent = NULL;
			scStatusHandle = NULL;
			lpStartWorkCallBack = NULL;
			lpEndofWorkCallBack = NULL;
			memset(scName, 0, MAX_PATH * sizeof(TCHAR));
			memset(scDescription, 0, MAX_PATH * sizeof(TCHAR));

			// 设置服务类型;
			scStatus.dwServiceType =
				SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS; // | SERVICE_INTERACTIVE_PROCESS与桌面交互;
			// 设置服务默认状态为停止;
			scStatus.dwCurrentState = SERVICE_STOPPED; // SERVICE_RUNNING;
			// 设置服务接受停止控制;
			scStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
			scStatus.dwWin32ExitCode = 0;
			scStatus.dwServiceSpecificExitCode = 0;
			scStatus.dwCheckPoint = 0;
			scStatus.dwWaitHint = 0;
		}
	}SCVariant, *pSCVariant;

	extern SCVariant g_scVariant;

	// 设置服务名称和描述名称;
	void InitServiceName(IN LPCTSTR lpServiceName = NULL, IN LPCTSTR lpServiceDescription = NULL);
	// 服务是否安装;
	BOOL IsInstalled();
	// 安装服务;
	BOOL Install();
	BOOL Uninstall();
	// windows服务程序入口函数;
	extern void WINAPI ServiceMain();
	// windows服务状态控制;
	extern void WINAPI SvcCtrlHandler(IN DWORD dwControlCode);
	// 初始化SOCKET库;
	extern int InitWinSock();
	// 服务日志事件;
	extern void SvcReportEvent(IN LPCTSTR pFormat, ...);
	// 向SCM报告服务状态;
	extern BOOL ReportSvcStatus(
		IN DWORD dwCurrentState,
		IN DWORD dwWin32ExitCode,
		IN DWORD dwServiceSpecificExitCode,
		IN DWORD dwCheckPoint,
		IN DWORD dwWaitHint);

	// 启动指定的服务;
	extern void StartSvc(IN LPCTSTR lpSvcName = NULL);
	extern void StopSvc(IN LPCTSTR lpSvcName = NULL);
	extern void KillService();
	// 设置回调函数指针;
	extern void SetCallBack(IN LPVOID lpStartWorkCallBack, IN LPVOID lpEndofWorkCallBack);
	// 在服务进程里调用外部exe;
	extern BOOL StartProcess(IN LPCTSTR lpExePath);
	// 服务日志;
	extern void LogEvent(IN LPCTSTR pFormat, ...);


	extern void UpdateSvcDacl(IN LPCTSTR lpSvcName);
	extern BOOL StopDependentServices();
	// 查询服务状态;
	extern void DoQuerySvc(IN LPCTSTR lpSvcName);
	// 更新服务描述;
	extern void DoUpdateSvcDesc(IN LPCTSTR lpSvcName);
	extern void DoDisableSvc(IN LPCTSTR lpSvcName);
	extern void DoEnableSvc(IN LPCTSTR lpSvcName);
	extern void DoDeleteSvc(IN LPCTSTR lpSvcName);
	//
	extern BOOL GetTokenByName(IN HANDLE &hToken, IN LPCTSTR lpName);
	//
	extern BOOL GetDebugPriv();
	//
	extern DWORD GetExplorerToken(OUT HANDLE* phExplorerToken);

};


#endif