用 C 语言编写 Windows 服务程序的五个步骤(代码)
1
#include <windows.h>2
#include <stdio.h>3

4
#define SLEEP_TIME 50005
#define LOGFILE "C:\\MyServices\\memstatus.txt"6

7

/**/////////////////////////////////////////////////////////////8
// Declare several global variables to share 9
// their values across multiple functions of your program.10

/**/////////////////////////////////////////////////////////////11
SERVICE_STATUS ServiceStatus; 12
SERVICE_STATUS_HANDLE hStatus; 13

14

/**/////////////////////////////////////////////////////////////15
// Make the forward definitions of functions prototypes.16
//17

/**/////////////////////////////////////////////////////////////18
void ServiceMain(int argc, char** argv); 19
void ControlHandler(DWORD request); 20
int InitService();21

22
int WriteToLog(char* str)23


{24
FILE* log;25
log = fopen(LOGFILE, "a+");26

if (log == NULL)
{27
OutputDebugString("Log file open failed.");28
return -1;29
}30
fprintf(log, "%s\n", str);31
fclose(log);32
return 0;33
}34

35
// Service initialization36
int InitService() 37


{ 38
OutputDebugString("Monitoring started.");39
int result;40
result = WriteToLog("Monitoring started.");41
return(result); 42
}43

44
// Control Handler45
void ControlHandler(DWORD request) 46


{ 47
switch(request) 48

{ 49
case SERVICE_CONTROL_STOP: 50
OutputDebugString("Monitoring stopped.");51
WriteToLog("Monitoring stopped.");52

53
ServiceStatus.dwWin32ExitCode = 0; 54
ServiceStatus.dwCurrentState = SERVICE_STOPPED; 55
SetServiceStatus (hStatus, &ServiceStatus);56
return; 57
58
case SERVICE_CONTROL_SHUTDOWN: 59
OutputDebugString("Monitoring stopped.");60
WriteToLog("Monitoring stopped.");61

62
ServiceStatus.dwWin32ExitCode = 0; 63
ServiceStatus.dwCurrentState = SERVICE_STOPPED; 64
SetServiceStatus (hStatus, &ServiceStatus);65
return; 66
67
default:68
break;69
} 70
71
// Report current status72
SetServiceStatus (hStatus, &ServiceStatus);73
74
return; 75
}76

77
void ServiceMain(int argc, char** argv) 78


{ 79
int error; 80
81
ServiceStatus.dwServiceType = 82
SERVICE_WIN32; 83
ServiceStatus.dwCurrentState = 84
SERVICE_START_PENDING; 85
ServiceStatus.dwControlsAccepted = 86
SERVICE_ACCEPT_STOP | 87
SERVICE_ACCEPT_SHUTDOWN;88
ServiceStatus.dwWin32ExitCode = 0; 89
ServiceStatus.dwServiceSpecificExitCode = 0; 90
ServiceStatus.dwCheckPoint = 0; 91
ServiceStatus.dwWaitHint = 0; 92
93
hStatus = RegisterServiceCtrlHandler(94
"MemoryStatus", 95
(LPHANDLER_FUNCTION)ControlHandler); 96
if (hStatus == (SERVICE_STATUS_HANDLE)0) 97

{ 98
// Registering Control Handler failed99
return; 100
} 101

102
// Initialize Service 103
error = InitService(); 104
if (error) 105

{106
// Initialization failed107
ServiceStatus.dwCurrentState = 108
SERVICE_STOPPED; 109
ServiceStatus.dwWin32ExitCode = -1; 110
SetServiceStatus(hStatus, &ServiceStatus); 111
return; 112
} 113
// We report the running status to SCM. 114
ServiceStatus.dwCurrentState = 115
SERVICE_RUNNING; 116
SetServiceStatus (hStatus, &ServiceStatus);117
118
MEMORYSTATUS memory;119
// The worker loop of a service120
while (ServiceStatus.dwCurrentState == 121
SERVICE_RUNNING)122

{123
char buffer[16];124
GlobalMemoryStatus(&memory);125
sprintf(buffer, "%d", memory.dwAvailPhys);126

127
OutputDebugString(buffer);128
int result = WriteToLog(buffer);129
if (result)130

{131
ServiceStatus.dwCurrentState = 132
SERVICE_STOPPED; 133
ServiceStatus.dwWin32ExitCode = -1; 134
SetServiceStatus(hStatus, 135
&ServiceStatus);136
return;137
}138
Sleep(SLEEP_TIME);139
}140
return; 141
}142

143
void main(int argc, char* argv[])144


{ 145
SERVICE_TABLE_ENTRY ServiceTable[2];146
ServiceTable[0].lpServiceName = "MemoryStatus";147
ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;148

149
ServiceTable[1].lpServiceName = NULL;150
ServiceTable[1].lpServiceProc = NULL;151
// Start the control dispatcher thread for our service152
StartServiceCtrlDispatcher(ServiceTable);153
}154

155

浙公网安备 33010602011771号