#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); }
命令行操作:
注意我这里在sc create MemortyStatus binpath= D:\MemoryStatus.exe里面有个拼写错误,MemoryStatus写成了MemortyStatus,多了个t,这个没关系,只要你在下面操作时候将错就错就行了。
运行结果:
最后,停止和删除服务:
参考: