vc编写windows服务:Windows服务编写原理及探讨(4)来源: 发布时间:星期四, 2009年2月12日 浏览:132次 评论:0
( 4) ![]() ![]() 前面几章 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 我在第 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 所以停止服务 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 开发服务时 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 上面 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 摆在下面 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 事实上很不幸 ![]() ![]() ![]() ![]() ![]() ![]() net pause kservice 的后在“事件查看器”里面 ![]() ![]() ![]() ![]() ![]() ![]() SERVICE_PAUSE_PENDING SERVICE_PAUSE_PENDING SERVICE_PAUSED SERVICE_PAUSED 看上去很奇怪是不是? ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 接下来我又进行了其它 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 为了解决这个问题 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 如果说前面 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 假如我 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 还有 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 解决这所有 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 事情没有完美 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 这种情况够糟糕了 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 说起来好像很容易 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 下面是我写 ![]() ![]() ![]() ![]() ![]() ![]() # ![]() # ![]() # ![]() # ![]() # ![]() # ![]() # ![]() # ![]() void WINAPI KServiceMain(DWORD argc, LPTSTR * argv); void InstallService(const char * szServiceName); void LogEvent(LPCTSTR pFormat, ...); void Start ![]() void Stop ![]() SERVICE_STATUS ssStatus; SERVICE_STATUS_HANDLE sshStatusHandle; ![]() ![]() ![]() ![]() { ![]() ![]() ![]() ![]() { InstallService(\"KService\"); ![]() } SERVICE_TABLE_ENTRYservice_table_entry ![]() { { \"KService\", KServiceMain }, { NULL, NULL } }; ::StartServiceCtrlDispatcher(service_table_entry); ![]() } void InstallService(const char * szServiceName) { SC_HANDLE handle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); char szFilename[256]; ::GetModuleFileName(NULL, szFilename, 255); SC_HANDLE hService = ::CreateService(handle, szServiceName, szServiceName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, szFilename, NULL, NULL, NULL, NULL, NULL); ::CloseServiceHandle(hService); ::CloseServiceHandle(handle); } SERVICE_STATUS servicestatus; SERVICE_STATUS_HANDLE servicestatushandle; void WINAPI ServiceCtrlHandler(DWORD dwControl) { switch (dwControl) { //下面虽然添加了暂停、继续等请求 ![]() ![]() //这是为什么呢?到了下面 ![]() ![]() ![]() servicestatus.dwCurrentState = SERVICE_PAUSE_PENDING; // TODO: add code to ![]() ![]() ![]() // This value need to try a lot to confirm // ... ::SetServiceStatus(servicestatushandle, &servicestatus); // TODO: add code to pause the service // not called in this service // ... servicestatus.dwCurrentState = SERVICE_PAUSED; // TODO: add code to ![]() ![]() ![]() ![]() ![]() servicestatus.dwCurrentState = SERVICE_CONTINUE_PENDING; // TODO: add code to ![]() ![]() ![]() ::SetServiceStatus(servicestatushandle, &servicestatus); // TODO: add code to unpause the service // not called in this service // ... servicestatus.dwCurrentState = SERVICE_RUNNING; // TODO: add code to ![]() ![]() ![]() ![]() ![]() servicestatus.dwCurrentState = SERVICE_STOP_PENDING; // TODO: add code to ![]() ![]() ![]() ::SetServiceStatus(servicestatushandle, &servicestatus); // TODO: add code to stop the service Stop ![]() servicestatus.dwCurrentState = SERVICE_STOPPED; // TODO: add code to ![]() ![]() ![]() ![]() ![]() // TODO: add code for system shutdown // as quick as possible ![]() ![]() // TODO: add code to ![]() // ... servicestatus.dwCurrentState = SERVICE_RUNNING; ![]() } ::SetServiceStatus(servicestatushandle, &servicestatus); } void WINAPI KServiceMain(DWORD argc, LPTSTR * argv) { servicestatus.dwServiceType = SERVICE_WIN32; servicestatus.dwCurrentState = SERVICE_START_PENDING; servicestatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;//上面 ![]() ![]() servicestatus.dwWin32ExitCode = 0; servicestatus.dwServiceSpec ![]() servicestatus.dwCheckPo ![]() servicestatus.dwWaitH ![]() servicestatushandle = ::RegisterServiceCtrlHandler(\"KService\", ServiceCtrlHandler); ![]() ![]() { ![]() } bool bInitialized = false; // Initialize the service // ... Start ![]() bInitialized = true; servicestatus.dwCheckPo ![]() servicestatus.dwWaitH ![]() ![]() { servicestatus.dwCurrentState = SERVICE_STOPPED; servicestatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR; servicestatus.dwServiceSpec ![]() } ![]() { servicestatus.dwCurrentState = SERVICE_RUNNING; } ::SetServiceStatus(servicestatushandle, &servicestatus); ![]() } void Start ![]() { LogEvent(\"Service Starting...\"); } void LogEvent(LPCTSTR pFormat, ...) { TCHAR chMsg[256]; HANDLE hEventSource; LPTSTR lpszStrings[1]; va_list pArg; va_start(pArg, pFormat); _vstpr ![]() va_end(pArg); lpszStrings[0] = chMsg; ![]() { // Get a handle to use with ReportEvent ![]() hEventSource = RegisterEventSource(NULL, \"KService\"); ![]() { // Write to event log. ReportEvent(hEventSource, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, (LPCTSTR*) &lpszStrings[0], NULL); DeregisterEventSource(hEventSource); } } ![]() { // As we are not running as a service, just write the error to the console. _putts(chMsg); } } void Stop ![]() { LogEvent(\"Service Stoped.\"); } 0
相关文章读者评论发表评论 |
|