A simple note from “Write Pure Windows Service” and “How to debug Windows Service”.
Write “Windows Service”
Windows Service is kind of application but it will start when system startup. Windows service will triggerd by “SMC”(System Managed Controller). There is some important note from my writing windows service experience.
**Register your service:
**What ever your application is big or small. You must register them to SMC before you use it. Please reference ::OpenSCManager() and ::CreateService().
For more detail please reference Writing a Service Program’s main Function
Start a “Windows Service”.
You can not startup a windows service via any debugger, you need to startup it on SMC. The SMC will use ::StartServiceCtrlDispatcher to start service and wait for the call back signal “SERVICE_RUNNING”. If you can not startup windows service normally, please let the Service_Status call back and tried to figurate out the root cause. For more detail please refer to Installing a Service
Stop a Windows Service
When you tried to close a windows service from SMC.The handler will receive a “SERVICE_CONTROL_STOP” from system. Your program have to pass “SERVICE_STOP_PENDING” and call RevokeClassObjects() . For more detail please refer to Stopping a Service.
Debugging “Windows Service”
There are two “easy” ways to debug your Windows service.
Attach Windows service by debugger:
In VC6 “Build”–> “Start Debug” –>”Attach a process” to point out your service it will use debugger to debug the service.
“Debugging Windows Service”
use “DebugBreak();”. it will pop up the Window and allow you to debug into Windows Service(You must install VC6 or higher version).
For more detail, please refer this address(How to debug Windows services).
At finally, there is a best practice for you.
- VC7 or VC8 may have some problem when attach the VC6 debug version. (I think it is the pdb doesn’t match).
- To attach the thread. you may need to turn on the “Show system process”, it will show the Windows Service process.