#include <boost/log/trivial.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/asio/ssl.hpp>
#include <windows.h>
#include <stdio.h>
#define SLEEP_TIME 5000
#define LOGFILE "C:\\AASSLInstaller_D_56\\memstatus.txt"
SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE hStatus;
void ServiceMain(int argc, char** argv);
void ControlHandler(DWORD request);
int InitService();
int WriteToLog(char* str)
{
FILE* log;
log = fopen(LOGFILE, "a+");
if (log == NULL)
return -1;
fprintf(log, "%s\n", str);
fclose(log);
return 0;
}
//void ThreadFunction()
//{
// WriteToLog("at ThreadFunction");
// //std::cout << "hello World " << std::endl;
// //getchar();
//}
int main()
{
using namespace boost::log::trivial; //for logging
boost::log::sources::severity_logger_mt< boost::log::trivial::severity_level > lg;
//WriteToLog("at main");
//return 1;
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;
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(
"MemoryStatus",
(LPHANDLER_FUNCTION)ControlHandler);
if (hStatus == (SERVICE_STATUS_HANDLE)0)
{
// Registering Control Handler failed
return;
}
// Initialize Service
//error = InitService();
error = 0;
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
//boost::thread t(&ThreadFunction);
while (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
{
char buffer[16];
GlobalMemoryStatus(&memory);
sprintf(buffer, "%d", memory.dwAvailPhys);
int result = WriteToLog(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("Monitoring started.");
return(result);
}
// Control handler function
void ControlHandler(DWORD request)
{
switch(request)
{
case SERVICE_CONTROL_STOP:
WriteToLog("Monitoring stopped.");
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
case SERVICE_CONTROL_SHUTDOWN:
WriteToLog("Monitoring stopped.");
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
default:
break;
}
// Report current status
SetServiceStatus (hStatus, &ServiceStatus);
return;
}
2. Dump
Dump Summary
------------
Dump File: with_ssl_56.exe.4208.dmp : C:\Dumps\with_ssl_56.exe.4208.dmp
Last Write Time: 9/26/2014 11:59:38 AM
Process Name: with_ssl_56.exe : C:\lpa_c\wix_for_service\build_win\src\with_ssl_56\Debug\with_ssl_56.exe
Process Architecture: x86
Exception Code: 0xC0000005
Exception Information: The thread tried to read from or write to a virtual address for which it does not have the appropriate access.
Heap Information: Not Present
System Information
------------------
OS Version: 6.0.6002
CLR Version(s):
Modules
-------
Module Name Module Path Module Version
----------- ----------- --------------
with_ssl_56.exe C:\lpa_c\wix_for_service\build_win\src\with_ssl_56\Debug\with_ssl_56.exe 0.0.0.0
ntdll.dll C:\Windows\System32\ntdll.dll 6.0.6002.18881
kernel32.dll C:\Windows\System32\kernel32.dll 6.0.6002.19034
advapi32.dll C:\Windows\System32\advapi32.dll 6.0.6002.18005
rpcrt4.dll C:\Windows\System32\rpcrt4.dll 6.0.6002.18882
boost_log-vc100-mt-gd-1_56.dll C:\boost_1_56_0_dyn\stage\lib\boost_log-vc100-mt-gd-1_56.dll 0.0.0.0
boost_date_time-vc100-mt-gd-1_56.dll C:\boost_1_56_0_dyn\stage\lib\boost_date_time-vc100-mt-gd-1_56.dll 0.0.0.0
MSVCP100D.dll C:\Windows\System32\MSVCP100D.dll 10.0.30319.1
MSVCR100D.dll C:\Windows\System32\MSVCR100D.dll 10.0.30319.1
boost_system-vc100-mt-gd-1_56.dll C:\boost_1_56_0_dyn\stage\lib\boost_system-vc100-mt-gd-1_56.dll 0.0.0.0
boost_filesystem-vc100-mt-gd-1_56.dll C:\boost_1_56_0_dyn\stage\lib\boost_filesystem-vc100-mt-gd-1_56.dll 0.0.0.0
boost_chrono-vc100-mt-gd-1_56.dll C:\boost_1_56_0_dyn\stage\lib\boost_chrono-vc100-mt-gd-1_56.dll 0.0.0.0
boost_thread-vc100-mt-gd-1_56.dll C:\boost_1_56_0_dyn\stage\lib\boost_thread-vc100-mt-gd-1_56.dll 0.0.0.0
psapi.dll C:\Windows\System32\psapi.dll 6.0.6000.16386
ws2_32.dll C:\Windows\System32\ws2_32.dll 6.0.6001.18000
msvcrt.dll C:\Windows\System32\msvcrt.dll 7.0.6002.18551
nsi.dll C:\Windows\System32\nsi.dll 6.0.6001.18000
user32.dll C:\Windows\System32\user32.dll 6.0.6002.18005
gdi32.dll C:\Windows\System32\gdi32.dll 6.0.6002.19171