Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <windows.h>
- #include <stdio.h>
- #include <string>
- using namespace std;
- #define SLEEP_TIME 5000
- #define LOGFILE "C:\MyServices\memstatus.txt"
- SERVICE_STATUS ServiceStatus;
- SERVICE_STATUS_HANDLE hStatus;
- void ServiceMain(int argc, char** argv);
- void ControlHandler(DWORD request);
- int InitService();
- int WriteToLog(string file_name, string input)
- {
- FILE *f;
- fopen_s(&f, file_name.c_str(), "a+");
- fprintf(f, "%sn", input.c_str());
- fclose(f);
- return 0;
- }
- string N = "MemoryStatus";
- LPWSTR Name = new wchar_t(N.size() + 1);
- int main()
- {
- SERVICE_TABLE_ENTRY ServiceTable[2];
- ServiceTable[0].lpServiceName = Name;
- ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
- ServiceTable[1].lpServiceName = NULL;
- ServiceTable[1].lpServiceProc = NULL;
- // Start the control dispatcher thread for our service
- StartServiceCtrlDispatcher(ServiceTable);
- return 0;
- }
- void ServiceMain(int argc, char** argv)
- {
- int error;
- ServiceStatus.dwServiceType = SERVICE_WIN32;
- ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
- ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
- ServiceStatus.dwWin32ExitCode = 0;
- ServiceStatus.dwServiceSpecificExitCode = 0;
- ServiceStatus.dwCheckPoint = 0;
- ServiceStatus.dwWaitHint = 0;
- hStatus = RegisterServiceCtrlHandler(
- Name,
- (LPHANDLER_FUNCTION)ControlHandler);
- if (hStatus == (SERVICE_STATUS_HANDLE)0)
- {
- // Registering Control Handler failed
- return;
- }
- // Initialize Service
- error = InitService();
- if (error)
- {
- // Initialization failed
- ServiceStatus.dwCurrentState = SERVICE_STOPPED;
- ServiceStatus.dwWin32ExitCode = -1;
- SetServiceStatus(hStatus, &ServiceStatus);
- return;
- }
- // We report the running status to SCM.
- ServiceStatus.dwCurrentState = SERVICE_RUNNING;
- SetServiceStatus(hStatus, &ServiceStatus);
- MEMORYSTATUS memory;
- // The worker loop of a service
- while (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
- {
- char buffer[16];
- GlobalMemoryStatus(&memory);
- sprintf_s(buffer, "%d", memory.dwAvailPhys);
- int result = WriteToLog("TestFile.txt", buffer);
- if (result)
- {
- ServiceStatus.dwCurrentState = SERVICE_STOPPED;
- ServiceStatus.dwWin32ExitCode = -1;
- SetServiceStatus(hStatus, &ServiceStatus);
- return;
- }
- Sleep(SLEEP_TIME);
- }
- return;
- }
- // Service initialization
- int InitService()
- {
- int result;
- result = WriteToLog("TestFile.txt", "Monitoring started.");
- return(result);
- }
- // Control handler function
- void ControlHandler(DWORD request)
- {
- switch (request)
- {
- case SERVICE_CONTROL_STOP:
- WriteToLog("TestFile.txt", "Monitoring stopped.");
- ServiceStatus.dwWin32ExitCode = 0;
- ServiceStatus.dwCurrentState = SERVICE_STOPPED;
- SetServiceStatus(hStatus, &ServiceStatus);
- return;
- case SERVICE_CONTROL_SHUTDOWN:
- WriteToLog("TestFile.txt", "Monitoring stopped.");
- ServiceStatus.dwWin32ExitCode = 0;
- ServiceStatus.dwCurrentState = SERVICE_STOPPED;
- SetServiceStatus(hStatus, &ServiceStatus);
- return;
- default:
- break;
- }
- // Report current status
- SetServiceStatus(hStatus, &ServiceStatus);
- return;
- }
Add Comment
Please, Sign In to add comment