首页 > 代码库 > C语言编写Windows服务程序
C语言编写Windows服务程序
原文: 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 initializationint InitService() { OutputDebugString("Monitoring started."); int result; result = WriteToLog("Monitoring started."); return(result); }// Control Handlervoid 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);}
命令行操作:
注意我这里在sc create MemortyStatus binpath= D:\MemoryStatus.exe里面有个拼写错误,MemoryStatus写成了MemortyStatus,多了个t,这个没关系,只要你在下面操作时候将错就错就行了。
运行结果:
最后,停止和删除服务:
参考:
http://www.vckbase.com/index.php/wv/1193
http://www.2cto.com/kf/201111/111990.html
C语言编写Windows服务程序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。