from a pause case SERVICE_CONTROL_CONTINUE: if runningService ampamp pauseService // Tell the SCM whats happening success SendStatusToSCM SERVICE_CONTINUE_PENDING NO_ERROR 0 1 1000 pauseServiceFALSE ResumeThreadthreadHandle currentState SERVICE_RUNNING break // Update current status case SERVICE_CONTROL_INTERROGATE: // it will fall to bottom and send status break // Do nothing in a shutdown. Could do cleanup // here but it must be very quick. case SERVICE_CONTROL_SHUTDOWN: return default: break SendStatusToSCMcurrentState NO_ERROR 0 0 0 // Handle an error from ServiceMain by cleaning up // and telling SCM that the service didnt start. VOID terminateDWORD error // if terminateEvent has been created close it. if terminateEvent CloseHandleterminateEvent // Send a message to the scm to tell about stopage if serviceStatusHandle SendStatusToSCMSERVICE_STOPPED error 0 0 0 // If the thread has started kill it off if threadHandle CloseHandlethreadHandle // Do not need to close serviceStatusHandle // ServiceMain is called when the SCM wants to // start the service. When it returns the service // has stopped. It therefore waits on an event // just before the end of the function and // that event gets set when it is time to stop. // It also returns on any error because the // service cannot start if there is an eror. VOID ServiceMainDWORD argc LPTSTR argv BOOL success // immediately call Registration function serviceStatusHandle RegisterServiceCtrlHandler SERVICE_NAME LPHANDLER_FUNCTIONHandler if serviceStatusHandle terminateGetLastError return // Notify SCM of progress success SendStatusToSCMSERVICE_START_PENDING NO_ERROR 0 1 5000 if success terminateGetLastError return // create the termination event terminateEvent CreateEvent 0 TRUE FALSE 0 if terminateEvent terminateGetLastError return // Notify SCM of progress success SendStatusToSCMSERVICE_START_PENDING NO_ERROR 0 2 1000 if success terminateGetLastError return // Check for startup params if argc 2 int temp atoiargv1 if temp lt 1000 beepDelay DEFAULT_BEEP_DELAY else beepDelay temp // Notify SCM of progress success SendStatusToSCMSERVICE_START_PENDING NO_ERROR 0 3 5000 if success terminateGetLastError return // Start the service itself success InitService if success terminateGetLastError return // The service is now running. // Notify SCM of progress success SendStatusToSCMSERVICE_RUNNING NO_ERROR 0 0 0 if success terminateGetLastError return // Wait for stop signal and then terminate WaitForSingleObject terminateEvent INFINITE terminate0 VOID mainVOID SERVICE_TABLE_ENTRY serviceTable SERVICE_NAME LPSERVICE_MAIN_FUNCTION ServiceMain NULL NULL BOOL success // Register with the SCM success StartServiceCtrlDispatcherserviceTable if success ErrorHandlerquotIn StartServiceCtrlDispatcherquot GetLastError 列表2 安装NT服务的代码 file:// // From the book quotWin32 System Services: The Heart of Windows NTquot // by Marshall Brain // Published by Prentice Hall file:// // This code installs a service. file:// // install.cpp include include void ErrorHandlerchar s DWORD err cout ltlt s ltlt endl cout ltlt quotError number: quot ltlt err ltlt endl ExitProcesserr void mainint argc char argv SC_HANDLE newService scm if argc 4 cout ltlt quotUsage:nquot cout ltlt quot install service_name service_label executablenquot cout ltlt quot service_name is the name used internally by the SCMnquot cout ltlt quot service_label is the name that appears in the Services appletnquot cout ltlt quot for multiple words put them in.
上一篇:
程序员应具备的素质(2)
下一篇:
数字钟的设计