Guest User

Untitled

a guest
Apr 26th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. #define SLEEP_TIME 5000
  9. #define LOGFILE "C:\MyServices\memstatus.txt"
  10.  
  11. SERVICE_STATUS ServiceStatus;
  12. SERVICE_STATUS_HANDLE hStatus;
  13.  
  14. void ServiceMain(int argc, char** argv);
  15. void ControlHandler(DWORD request);
  16. int InitService();
  17.  
  18. int WriteToLog(string file_name, string input)
  19. {
  20. FILE *f;
  21. fopen_s(&f, file_name.c_str(), "a+");
  22. fprintf(f, "%sn", input.c_str());
  23. fclose(f);
  24. return 0;
  25. }
  26.  
  27. string N = "MemoryStatus";
  28. LPWSTR Name = new wchar_t(N.size() + 1);
  29.  
  30. int main()
  31. {
  32. SERVICE_TABLE_ENTRY ServiceTable[2];
  33. ServiceTable[0].lpServiceName = Name;
  34. ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
  35.  
  36. ServiceTable[1].lpServiceName = NULL;
  37. ServiceTable[1].lpServiceProc = NULL;
  38. // Start the control dispatcher thread for our service
  39. StartServiceCtrlDispatcher(ServiceTable);
  40. return 0;
  41. }
  42.  
  43.  
  44. void ServiceMain(int argc, char** argv)
  45. {
  46. int error;
  47.  
  48. ServiceStatus.dwServiceType = SERVICE_WIN32;
  49. ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
  50. ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
  51. ServiceStatus.dwWin32ExitCode = 0;
  52. ServiceStatus.dwServiceSpecificExitCode = 0;
  53. ServiceStatus.dwCheckPoint = 0;
  54. ServiceStatus.dwWaitHint = 0;
  55.  
  56. hStatus = RegisterServiceCtrlHandler(
  57. Name,
  58. (LPHANDLER_FUNCTION)ControlHandler);
  59. if (hStatus == (SERVICE_STATUS_HANDLE)0)
  60. {
  61. // Registering Control Handler failed
  62. return;
  63. }
  64. // Initialize Service
  65. error = InitService();
  66. if (error)
  67. {
  68. // Initialization failed
  69. ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  70. ServiceStatus.dwWin32ExitCode = -1;
  71. SetServiceStatus(hStatus, &ServiceStatus);
  72. return;
  73. }
  74. // We report the running status to SCM.
  75. ServiceStatus.dwCurrentState = SERVICE_RUNNING;
  76. SetServiceStatus(hStatus, &ServiceStatus);
  77.  
  78. MEMORYSTATUS memory;
  79. // The worker loop of a service
  80. while (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
  81. {
  82. char buffer[16];
  83. GlobalMemoryStatus(&memory);
  84. sprintf_s(buffer, "%d", memory.dwAvailPhys);
  85. int result = WriteToLog("TestFile.txt", buffer);
  86. if (result)
  87. {
  88. ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  89. ServiceStatus.dwWin32ExitCode = -1;
  90. SetServiceStatus(hStatus, &ServiceStatus);
  91. return;
  92. }
  93.  
  94. Sleep(SLEEP_TIME);
  95. }
  96. return;
  97. }
  98.  
  99. // Service initialization
  100. int InitService()
  101. {
  102. int result;
  103. result = WriteToLog("TestFile.txt", "Monitoring started.");
  104. return(result);
  105. }
  106.  
  107. // Control handler function
  108. void ControlHandler(DWORD request)
  109. {
  110. switch (request)
  111. {
  112. case SERVICE_CONTROL_STOP:
  113. WriteToLog("TestFile.txt", "Monitoring stopped.");
  114.  
  115. ServiceStatus.dwWin32ExitCode = 0;
  116. ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  117. SetServiceStatus(hStatus, &ServiceStatus);
  118. return;
  119.  
  120. case SERVICE_CONTROL_SHUTDOWN:
  121. WriteToLog("TestFile.txt", "Monitoring stopped.");
  122.  
  123. ServiceStatus.dwWin32ExitCode = 0;
  124. ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  125. SetServiceStatus(hStatus, &ServiceStatus);
  126. return;
  127.  
  128. default:
  129. break;
  130. }
  131.  
  132. // Report current status
  133. SetServiceStatus(hStatus, &ServiceStatus);
  134.  
  135. return;
  136. }
Add Comment
Please, Sign In to add comment