C服务程序模板

在DoWork中添加自己的代码。
 
服务安装: sc create srvdemo binpath= "F:\srvdemo.exe"
服务启动:sc start srvdemo 参数1 参数2    //net start方式不支持参数
服务停止:sc/net stop srvdemo
服务删除:sc delete srvdemo
 
其他命令:sc description srvdemo "服务描述" //添加服务描述
 
#include <windows.h>

CHAR SrvName[] = "SrvDemo";
SERVICE_STATUS      g_hStatusService;
SERVICE_STATUS_HANDLE    g_hStatusHandle;

VOID WINAPI      ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv);
VOID WINAPI      ServiceHandler(DWORD fdwControl);

DWORD DoInitialize(DWORD  dwArgc, LPTSTR *lpszArgv);
VOID DoWork(DWORD  dwArgc, LPTSTR *lpszArgv);

int main(int argc, char** argv)
{
	SERVICE_TABLE_ENTRY ServiceTable[2];
	ServiceTable[0].lpServiceName = SrvName;
	ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
	ServiceTable[1].lpServiceName = NULL;
	ServiceTable[1].lpServiceProc = NULL;

	StartServiceCtrlDispatcher(ServiceTable);

	return 0;
}

//服务控制处理函数
VOID WINAPI ServiceHandler(DWORD fdwControl)
{
	switch (fdwControl)
	{
	case SERVICE_CONTROL_PAUSE:
		g_hStatusService.dwCurrentState = SERVICE_PAUSED;
		break;

	case SERVICE_CONTROL_CONTINUE:
		g_hStatusService.dwCurrentState = SERVICE_RUNNING;
		break;

	case SERVICE_CONTROL_STOP:
	case SERVICE_CONTROL_SHUTDOWN:
		g_hStatusService.dwCurrentState = SERVICE_STOPPED;
		g_hStatusService.dwWin32ExitCode = 0;
		g_hStatusService.dwCheckPoint = 0;
		g_hStatusService.dwWaitHint = 0;
		SetServiceStatus(g_hStatusHandle, &g_hStatusService);
		return;

	case SERVICE_CONTROL_INTERROGATE:
		break;

	default:
		break;
	}

	SetServiceStatus(g_hStatusHandle, &g_hStatusService);
	return;
}

//服务主函数
VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
{
	g_hStatusHandle = RegisterServiceCtrlHandler(SrvName,
		(LPHANDLER_FUNCTION)ServiceHandler);
	if (g_hStatusHandle == NULL)
		return;

	// 设置一些参数
	g_hStatusService.dwServiceType = SERVICE_WIN32;
	g_hStatusService.dwCurrentState = SERVICE_START_PENDING;
	g_hStatusService.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE;
	g_hStatusService.dwWin32ExitCode = 0;
	g_hStatusService.dwServiceSpecificExitCode = 0;
	g_hStatusService.dwCheckPoint = 0;
	g_hStatusService.dwWaitHint = 0;
	SetServiceStatus(g_hStatusHandle, &g_hStatusService);
	
	// 初始化操作
	DWORD dwRet = DoInitialize(dwArgc, lpszArgv);
	if (dwRet != NO_ERROR)
	{
		g_hStatusService.dwCurrentState = SERVICE_STOPPED;
		g_hStatusService.dwWin32ExitCode = dwRet;
		SetServiceStatus(g_hStatusHandle, &g_hStatusService);
		return;
	}

	// 初始化结束,报告运行状态
	g_hStatusService.dwCurrentState = SERVICE_RUNNING;
	g_hStatusService.dwCheckPoint = 0;
	g_hStatusService.dwWaitHint = 0;
	SetServiceStatus(g_hStatusHandle, &g_hStatusService);

	//在这里做其他的工作
	DoWork(dwArgc, lpszArgv);
}

// 执行一些初始化过程
DWORD DoInitialize(DWORD  dwArgc, LPTSTR *lpszArgv)
{
	return NO_ERROR;
}


VOID DoWork(DWORD  dwArgc, LPTSTR *lpszArgv)
{
        // lpszArgv[1]是参数1
	if (dwArgc == 1)
	{
		return;
	}
	else if (dwArgc == 2)
	{
		srvlog("lpszArgv[1]: %s\n", lpszArgv[1]);
		return;
	}
	else if (dwArgc == 3)
	{
	}
}

  

posted @ 2017-08-10 10:40  鞋带儿  阅读(403)  评论(0编辑  收藏  举报