Writing a Control Handler Function
来源:互联网 发布:日本行知学园熟官网 编辑:程序博客网 时间:2024/05/16 17:04
Writing a Control Handler Function
The MyServiceCtrlHandler function in the following example is the Handler function. When this function is called by the dispatcher thread, it handles the control code passed in the Opcode parameter and then calls the SetServiceStatus function to update the service's status. Every time a Handler function receives a control code, it is appropriate to return status with a call to SetServiceStatus regardless of whether the service acts on the control.
When the pause control is received, MyServiceCtrlHandler simply sets the dwCurrentState member of the SERVICE_STATUS structure to SERVICE_PAUSED. Likewise, when the continue control is received, the state is set to SERVICE_RUNNING. Therefore, MyServiceCtrlHandler is not a good example of how to handle the pause and continue controls. Because MyServiceCtrlHandler is a template for a Handler function, code for the pause and continue controls is included for completeness. A service that supports either the pause or continue control should handle these controls in a way that makes sense. Many services support neither the pause or continue control. If the service indicates that it does not support pause or continue with the dwControlsAccepted parameter, then the SCM will not send pause or continue controls to the service's Handler function.
To output debugging information, MyServiceCtrlHandler calls SvcDebugOut. The source code for SvcDebugOut is listed in Writing a Service Program's main Function. Also, note that the MyServiceStatus variable is a global variable and should be initialized as demonstrated in Writing a ServiceMain function.
#include <windows.h>SERVICE_STATUS MyServiceStatus; SERVICE_STATUS_HANDLE MyServiceStatusHandle; VOID SvcDebugOut(LPSTR String, DWORD Status);VOID WINAPI MyServiceCtrlHandler (DWORD Opcode) { DWORD status; switch(Opcode) { case SERVICE_CONTROL_PAUSE: // Do whatever it takes to pause here. MyServiceStatus.dwCurrentState = SERVICE_PAUSED; break; case SERVICE_CONTROL_CONTINUE: // Do whatever it takes to continue here. MyServiceStatus.dwCurrentState = SERVICE_RUNNING; break; case SERVICE_CONTROL_STOP: // Do whatever it takes to stop here. MyServiceStatus.dwWin32ExitCode = 0; MyServiceStatus.dwCurrentState = SERVICE_STOPPED; MyServiceStatus.dwCheckPoint = 0; MyServiceStatus.dwWaitHint = 0; if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus)) { status = GetLastError(); SvcDebugOut(" [MY_SERVICE] SetServiceStatus error %ld/n", status); } SvcDebugOut(" [MY_SERVICE] Leaving MyService /n",0); return; case SERVICE_CONTROL_INTERROGATE: // Fall through to send current status. break; default: SvcDebugOut(" [MY_SERVICE] Unrecognized opcode %ld/n", Opcode); } // Send current status. if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus)) { status = GetLastError(); SvcDebugOut(" [MY_SERVICE] SetServiceStatus error %ld/n", status); } return; }
- Writing a Control Handler Function
- Registering a Control Handler Function (Windows)
- Writing a ServiceMain Function
- Writing a sort comparison function
- Writing a Service Program's main Function
- 经典 -Writing a UI Automation Provider for a Win32-based Custom Control
- Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0
- Gstreamer插件教程2.3—编写一个插件(Writing a Plugin):chain函数(The chain function)
- Gstreamer插件教程2.4—编写一个插件(Writing a Plugin):event函数(The event function)
- Gstreamer插件教程2.5—编写一个插件(Writing a Plugin):query函数(The query function)
- MDK Executing a function or an interrupt handler in RAM
- Writing Your First Thread Function
- Writing a Research Paper
- Writing a SmartObject Service
- Writing a Network Driver
- Writing a Player Plugin
- Writing a Thesis Statement
- Writing a Persuasive Essay
- SERVICE_FAILURE_ACTIONS
- SERVICE_STATUS_PROCESS
- SERVICE_TABLE_ENTRY
- Writing a Service Program's main Function
- Writing a ServiceMain Function
- Writing a Control Handler Function
- 给中国学生的第四封信(李开复)--转载
- Creating a Multithreaded Service
- 乱码原理
- Handling Logoff Events
- 第八章configuring the database archiving mode
- C语言与Windows API编程!有无同路人?
- Opening an SCManager Database
- Installing a Service