diff --git a/base/setup/CMakeLists.txt b/base/setup/CMakeLists.txt index ee5a90d0bc5..3ae8f62bc4a 100644 --- a/base/setup/CMakeLists.txt +++ b/base/setup/CMakeLists.txt @@ -4,3 +4,5 @@ add_subdirectory(reactos) add_subdirectory(setup) add_subdirectory(usetup) add_subdirectory(welcome) +add_subdirectory(FreeMem) + diff --git a/base/setup/FreeMem/CMakeLists.txt b/base/setup/FreeMem/CMakeLists.txt new file mode 100644 index 00000000000..d8dc605c2dd --- /dev/null +++ b/base/setup/FreeMem/CMakeLists.txt @@ -0,0 +1,4 @@ +add_executable(FreeMem FreeMem.c) +set_module_type(FreeMem win32cui) +add_importlibs(FreeMem advapi32 gdi32 user32 shell32 msvcrt kernel32 ntdll) +add_cd_file(TARGET FreeMem DESTINATION reactos NO_CAB FOR bootcd) diff --git a/base/setup/FreeMem/FreeMem.c b/base/setup/FreeMem/FreeMem.c new file mode 100644 index 00000000000..8f353e06d8d --- /dev/null +++ b/base/setup/FreeMem/FreeMem.c @@ -0,0 +1,140 @@ +#include +#include +#include + +#define SLEEP_TIME 2000 + +SERVICE_STATUS ServiceStatus; +SERVICE_STATUS_HANDLE hStatus; + +void ServiceMain(int argc, char** argv); +void ControlHandler(DWORD request); +int InitService(); + +int WriteToDebug(char* str) +{ + SYSTEMTIME sysTime; + GetLocalTime(&sysTime); + DPRINT1("At %02d:%02d:%02d FreeMem %dK\n", + sysTime.wHour, sysTime.wMinute, sysTime.wSecond, atoi(str)/1024); + return 0; +} + +int WriteStatusToDebug(char* str) +{ + SYSTEMTIME sysTime; + GetLocalTime(&sysTime); + DPRINT1("%s at %02d:%02d:%02d\n", + str, sysTime.wHour, sysTime.wMinute, sysTime.wSecond); + return 0; +} + +int 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; + // Start the control dispatcher thread for our service + StartServiceCtrlDispatcher(ServiceTable); + return 0; +} + + +void ServiceMain(int argc, char** argv) +{ + int error; + MEMORYSTATUS memory; + int result; + + 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( + "FreeMem", + (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); + + // The worker loop of a service + while (ServiceStatus.dwCurrentState == SERVICE_RUNNING) + { + char buffer[16]; + GlobalMemoryStatus(&memory); + sprintf(buffer, "%ld", memory.dwAvailPhys); + result = WriteToDebug(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 = WriteStatusToDebug("Monitoring service started"); + return(result); +} + +// Control handler function +void ControlHandler(DWORD request) +{ + switch(request) + { + case SERVICE_CONTROL_STOP: + WriteStatusToDebug("Monitoring service stopped"); + + ServiceStatus.dwWin32ExitCode = 0; + ServiceStatus.dwCurrentState = SERVICE_STOPPED; + SetServiceStatus (hStatus, &ServiceStatus); + return; + + case SERVICE_CONTROL_SHUTDOWN: + WriteStatusToDebug("Monitoring service shutdown"); + + ServiceStatus.dwWin32ExitCode = 0; + ServiceStatus.dwCurrentState = SERVICE_STOPPED; + SetServiceStatus (hStatus, &ServiceStatus); + return; + + default: + break; + } + + // Report current status + SetServiceStatus (hStatus, &ServiceStatus); + + return; +} + diff --git a/base/setup/FreeMem/Notes06.txt b/base/setup/FreeMem/Notes06.txt new file mode 100644 index 00000000000..5429821108e --- /dev/null +++ b/base/setup/FreeMem/Notes06.txt @@ -0,0 +1,15 @@ + + This is a free program released to the public domain by Doug Lyons. + + Using FreeMem.exe + 1) Copy FreeMem.c to your destination path. Here I am using C:\ + 2) Open CMD prompt and execute the following line to create the service: + sc create FreeMem binpath= c:\FreeMem.exe (Note the space after the "binpath=" as its critical.) + Caution: This does not check for the validity of the path, so you need to make sure it's correct. + 3) From the CMD prompt execute the following to start the service: + sc start FreeMem + 4) To stop the service execute the following from the CMD prompt: + sc stop FreeMem + 5) To delete the service execute the following from the CMD prompt: + sc delete FreeMem +