CoolBreeze007
Programmer
I am new to developing windows services. I have been able to develop a service template that registers a service, runs and stops properly. Now I want to add my code to this template code. I have code that reads datalogger data from a serial port. Needless to say, it runs in a loop reading data every 5 seconds. I want to make this serial port application a service and I want to ability to stop/start the service using the standard windows service manager. My question is where do I include my serial port code and how do I modify the code so that I can stop and start it properly. Snipets of the code/pseudocode are listed below.
========= Service Code =============
VOID ServiceMain( DWORD dwArgc, LPTSTR *lpszArgv)
{
CreateEvent(...)
ReportSvcStatus( SERVICE_RUNNING, NO_ERROR, 0 );
while(1)
{
WaitForSingleObject(ghSvcStopEvent, INFINITE);
ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );
return;
}
}
============ Basic Serial Port Loop =========
int _tmain( int argc, TCHAR *argv[])
{
initialization
m_hSerialComm = CreateFile(...)
while(TRUE) {
wait(5 seconds)
ReadFile(...)
if data received then
Do stuff with the data
endif
);
CloseHandle(...)
return(0);
}
========= Service Code =============
VOID ServiceMain( DWORD dwArgc, LPTSTR *lpszArgv)
{
CreateEvent(...)
ReportSvcStatus( SERVICE_RUNNING, NO_ERROR, 0 );
while(1)
{
WaitForSingleObject(ghSvcStopEvent, INFINITE);
ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );
return;
}
}
============ Basic Serial Port Loop =========
int _tmain( int argc, TCHAR *argv[])
{
initialization
m_hSerialComm = CreateFile(...)
while(TRUE) {
wait(5 seconds)
ReadFile(...)
if data received then
Do stuff with the data
endif
);
CloseHandle(...)
return(0);
}