NT Services
Author
Zhou Renjian
Create@
2004-02-17 22:27
NT Services
Service Control Manager (SCM).
Design the Service
The codes should have the following codes in main():
SERVICE_TABLE_ENTRY servicetable[]={
{strServiceName,(LPSERVICE_MAIN_FUNCTION)ServiceMain}, // "My service"
{NULL,NULL} //Must be NULL to indicate that it ends.
};
StartServiceCtrlDispatcher(servicetable);
========main() is simple actually============
ServiceMain()
void ServiceMain(DWORD argc, LPTSTR *argv)
1. RegisterServiceCtrlHandler
2. UpdateServiceStatus
3. CreateEvent // for later blocking the ServiceMain
4. UpdateServiceStatus // notify the SCM
5. StartServiceThread // start a thread :: create a thread
6. UpdateServiceStatus // notify the process to the SCM
7. WaitForSingleObject // waiting fo the event happing
8. CloseHandle // finish.
Install Service:
scm=OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
CreateService(scm,
"NishService", //name
"Buster's first NT service", // display name
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START, // startup type
SERVICE_ERROR_NORMAL,
"D:\\nish\\FirstService\\Debug\\FirstService.exe", // path of .exe
0,0,0,0,0);
Start the Service
NishService=OpenService(scm,"NishService",SERVICE_ALL_ACCESS);
StartService(NishService,0,NULL);
Stop Service
ControlService(NishService,SERVICE_CONTROL_STOP,&m_SERVICE_STATUS);