windows 编写简单的服务程序
这个服务程序,每隔1秒钟,在C:\log.txt文件中写入当前剩余内存大小MB
注:cmd管理员启动,binPath= 添加完整的路径(绝对路径)否则 系统找不到指定文件
创建服务 sc create 服务名 binPath= E:\smallinsect\WindowsAPI\bin\x64\Release\DemoService01.exe
启动服务 sc start 服务名
查询服务 sc query 服务名
删除服务 sc delete 服务名
停止服务 sc stop 服务名
配置服务:
sc config 服务名 start= demand //手动
sc condig 服务名 start= auto //自动
sc config 服务名 start= disabled //禁用
1 #define _CRT_SECURE_NO_WARNINGS 2 3 #include <stdio.h> 4 #include <windows.h> 5 6 #define SLEEP_TIME 1000 7 #define FILE_PATH "c:/log.txt" 8 9 VOID WINAPI ServiceMain(DWORD dwNumServicesArgs, LPTSTR *lpServiceArgVectors); 10 VOID WINAPI ServiceCtrlHandler(DWORD dwControl); 11 int WriteToLog(const char *szMsg); 12 13 BOOL bRunning = TRUE; 14 SERVICE_STATUS m_ServiceStatus;//服务状态 15 SERVICE_STATUS_HANDLE m_ServiceStatusHandle;//服务状态句柄 16 17 TCHAR szServiceName[] = TEXT("DemoService");//服务名字 18 19 int main(int argc, char *argv[]) { 20 21 //服务分配表 22 SERVICE_TABLE_ENTRY DispatchTable[2]; 23 //服务名称 24 DispatchTable[0].lpServiceName = szServiceName; 25 //服务主函数 26 DispatchTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain; 27 28 DispatchTable[1].lpServiceName = NULL; 29 DispatchTable[1].lpServiceProc = NULL; 30 31 //注册服务的主函数 32 //服务控制分配器 给服务创建一个线程 33 StartServiceCtrlDispatcher(DispatchTable); 34 35 system("pause"); 36 return 0; 37 } 38 39 //服务的主函数实现 40 VOID WINAPI ServiceMain(DWORD dwNumServicesArgs, LPTSTR *lpServiceArgVectors) { 41 42 MEMORYSTATUS memstatus;//内存信息 43 char szMsg[512]; 44 SIZE_T availmb; 45 46 //服务类型 WIN32 47 m_ServiceStatus.dwServiceType = SERVICE_WIN32; 48 //服务状态 等待 49 m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING; 50 //服务控制 关机 服务停止 51 m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP; 52 //服务退出 53 m_ServiceStatus.dwWin32ExitCode = 0; 54 //服务退出 55 m_ServiceStatus.dwServiceSpecificExitCode = 0; 56 m_ServiceStatus.dwCheckPoint = 0; 57 m_ServiceStatus.dwWaitHint = 0; 58 //注册服务控制函数 59 m_ServiceStatusHandle = RegisterServiceCtrlHandler(szServiceName, ServiceCtrlHandler); 60 if (m_ServiceStatusHandle == 0) { 61 WriteToLog("RegisterServiceCtrlHandler fail"); 62 } 63 WriteToLog("RegisterServiceCtrlHandler success"); 64 65 //服务正在运行了 66 m_ServiceStatus.dwCurrentState = SERVICE_RUNNING; 67 //报告服务的状态 68 SetServiceStatus(m_ServiceStatusHandle, &m_ServiceStatus); 69 70 bRunning = TRUE; 71 72 while (bRunning) { 73 //检测电脑当前的内存大小 74 GlobalMemoryStatus(&memstatus); 75 //将内存换算成MB 76 availmb = memstatus.dwAvailPhys / 1024 / 1024; 77 #ifdef _WIN64 78 sprintf(szMsg, "available memory is %lld MB", availmb); 79 #else 80 sprintf(szMsg, "available memory is %ld MB", availmb); 81 #endif 82 WriteToLog(szMsg); 83 Sleep(SLEEP_TIME); 84 } 85 WriteToLog("service stop"); 86 } 87 88 //服务控制函数 89 VOID WINAPI ServiceCtrlHandler(DWORD dwControl) { 90 switch (dwControl) { 91 case SERVICE_CONTROL_STOP://停止 92 bRunning = FALSE; 93 m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;//停止状态 94 break; 95 case SERVICE_CONTROL_SHUTDOWN://关机 96 bRunning = FALSE; 97 m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;//停止状态 98 break; 99 default: 100 break; 101 } 102 //报告服务状态 103 SetServiceStatus(m_ServiceStatusHandle, &m_ServiceStatus); 104 } 105 106 //写日志函数 107 int WriteToLog(const char *szMsg) { 108 FILE* pfile = fopen(FILE_PATH, "a+"); 109 if (pfile == NULL) { 110 return -1; 111 } 112 113 fprintf(pfile, "%s\n", szMsg); 114 fclose(pfile); 115 return 0; 116 }
安装服务
安装成功,在服务里能看到安装的服务。
启动服务
C盘目录下有log.txt文件出现
停止服务
删除服务