Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add Code to a Windows Service Template

Status
Not open for further replies.

CoolBreeze007

Programmer
Aug 5, 2008
6
US
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);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top