码迷,mamicode.com
首页 > 编程语言 > 详细

C语言编写Windows服务程序

时间:2014-12-16 08:41:42      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:blog   http   ar   io   os   sp   on   文件   art   

原文: C语言编写Windows服务程序

#include <Windows.h>
#include <stdio.h>

#define SLEEP_TIME 5000                          // 间隔时间
#define LOGFILE "C:\\memstatus.txt"              // 信息输出文件


SERVICE_STATUS ServiceStatus;  // 服务状态
SERVICE_STATUS_HANDLE hStatus; // 服务状态句柄

void  ServiceMain(int argc, char** argv);
void  CtrlHandler(DWORD request);
int InitService();

int WriteToLog(char* str)
{
	FILE* pfile;
	fopen_s(&pfile,LOGFILE,"a+");
	if (pfile==NULL)
	{
		return -1;
	}
	fprintf_s(pfile,"%s\n",str);
	fclose(pfile);
	return 0;
}

// Service initialization
int InitService() 
{ 
	OutputDebugString("Monitoring started.");
	int result;
	result = WriteToLog("Monitoring started.");
	return(result); 
}
// Control Handler
void  CtrlHandler(DWORD request)
{
	switch (request)
	{
	case SERVICE_CONTROL_STOP:
		OutputDebugString("Monitoring stopped.");
		WriteToLog("Monitoring stopped.");

		ServiceStatus.dwWin32ExitCode = 0; 
		ServiceStatus.dwCurrentState = SERVICE_STOPPED; 
		SetServiceStatus (hStatus, &ServiceStatus);
		return;
	case SERVICE_CONTROL_SHUTDOWN:
		OutputDebugString("Monitoring stopped.");
		WriteToLog("Monitoring stopped.");

		ServiceStatus.dwWin32ExitCode = 0; 
		ServiceStatus.dwCurrentState = SERVICE_STOPPED; 
		SetServiceStatus (hStatus, &ServiceStatus);
		return;
	default:
		break;
	}
	// Report current status
	SetServiceStatus (hStatus, &ServiceStatus);
	return;
}

void  ServiceMain(int argc, char** argv)
{
	int error;
	ServiceStatus.dwServiceType = 
		SERVICE_WIN32;
	ServiceStatus.dwCurrentState = 
		SERVICE_START_PENDING;
	ServiceStatus.dwControlsAccepted = 
		SERVICE_ACCEPT_SHUTDOWN | 
		SERVICE_ACCEPT_STOP;//在本例中只接受系统关机和停止服务两种控制命令
	ServiceStatus.dwWin32ExitCode = 0;
	ServiceStatus.dwServiceSpecificExitCode = 0;
	ServiceStatus.dwCheckPoint = 0;
	ServiceStatus.dwWaitHint = 0;
	hStatus = ::RegisterServiceCtrlHandler(
		"MemoryStatus", 
		(LPHANDLER_FUNCTION)CtrlHandler);
	if (hStatus == (SERVICE_STATUS_HANDLE)0)
	{
		WriteToLog("RegisterServiceCtrlHandler failed");
		return;
	}
	WriteToLog("RegisterServiceCtrlHandler success");
	// Initialize Service 
	error = InitService();
	if (error) 
	{
		// Initialization failed
		ServiceStatus.dwCurrentState = 
			SERVICE_STOPPED; 
		ServiceStatus.dwWin32ExitCode = -1; 
		SetServiceStatus(hStatus, &ServiceStatus); 
		return; 
	} 
	// 向SCM 报告运行状态
	ServiceStatus.dwCurrentState = 
		SERVICE_RUNNING;
	SetServiceStatus (hStatus, &ServiceStatus);
	
	// 下面就开始任务循环了,你可以添加你自己希望服务做的工作
	MEMORYSTATUS memstatus;
	while (ServiceStatus.dwCurrentState == 
		SERVICE_RUNNING)
	{
		char buffer[16];
		GlobalMemoryStatus(&memstatus);
		int availmb = memstatus.dwAvailPhys/1024/1024;
		sprintf_s(buffer,100,"available memory is %dMB",availmb);
		OutputDebugString(buffer);
		int result = WriteToLog(buffer);
		if (result)
		{
			ServiceStatus.dwCurrentState = 
				SERVICE_STOPPED; 
			ServiceStatus.dwWin32ExitCode      = -1; 
			SetServiceStatus(hStatus, 
				&ServiceStatus);
			return;
		}
		Sleep(SLEEP_TIME);
	}
	WriteToLog("service stopped");
}



void main()
{
	SERVICE_TABLE_ENTRY ServiceTable[2];
	ServiceTable[0].lpServiceName="MemoryStatus";
	ServiceTable[0].lpServiceProc=(LPSERVICE_MAIN_FUNCTION)ServiceMain;
	ServiceTable[1].lpServiceName=NULL;
	ServiceTable[1].lpServiceProc=NULL;
	StartServiceCtrlDispatcher(ServiceTable);

}

 

命令行操作:


bubuko.com,布布扣 

 

注意我这里在sc create MemortyStatus binpath= D:\MemoryStatus.exe里面有个拼写错误,MemoryStatus写成了MemortyStatus,多了个t,这个没关系,只要你在下面操作时候将错就错就行了。

运行结果:

bubuko.com,布布扣

 

最后,停止和删除服务:

bubuko.com,布布扣

 

参考:

http://www.vckbase.com/index.php/wv/1193

http://www.2cto.com/kf/201111/111990.html

C语言编写Windows服务程序

标签:blog   http   ar   io   os   sp   on   文件   art   

原文地址:http://www.cnblogs.com/lonelyxmas/p/4166269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!