{"id":950,"date":"2010-03-12T22:35:00","date_gmt":"2010-03-12T02:35:00","guid":{"rendered":""},"modified":"2013-11-17T16:49:04","modified_gmt":"2013-11-17T08:49:04","slug":"delphi%e6%b3%a8%e5%86%8c%e4%b8%8e%e5%8d%b8%e8%bd%bd%e7%b3%bb%e7%bb%9f%e6%9c%8d%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/950.html","title":{"rendered":"Delphi\u6ce8\u518c\u4e0e\u5378\u8f7d\u7cfb\u7edf\u670d\u52a1"},"content":{"rendered":"<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\nprogram Service;\r\n\r\nuses\r\n  Windows,\r\n  WinSvc;\r\n\r\nconst\r\nServiceName: pchar = 'AFX Service';           \/\/\u670d\u52a1\u540d\r\nDisplayName: pchar = 'AFX Demo Service';      \/\/\u670d\u52a1\u663e\u793a\u540d\u79f0\r\n\r\nvar\r\nStatus: TServiceStatus;\r\nStatusHandle: SERVICE_STATUS_HANDLE;\r\nServiceTable: array &#x5B;0..1] of TServiceTableEntry;\r\nStopped: boolean;\r\nPaused: boolean;\r\n\r\nprocedure ServiceMain;\r\nbegin\r\nrepeat\r\n    if not Paused then\r\n    begin\r\n      Beep(1000, 1000);\r\n      Sleep(1000);\r\n    end;\r\nuntil Stopped;\r\nend;\r\n\r\nprocedure ServiceCtrlHandler(Control: dword); stdcall;\r\nbegin\r\ncase Control of\r\n    SERVICE_CONTROL_STOP:\r\n      begin\r\n        Stopped := True;\r\n        Status.dwCurrentState := SERVICE_STOP_PENDING;\r\n        SetServiceStatus(StatusHandle, Status);\r\n      end;\r\n    SERVICE_CONTROL_PAUSE:\r\n      begin\r\n        Paused := True;\r\n        Status.dwcurrentstate := SERVICE_PAUSED;\r\n        SetServiceStatus(StatusHandle, Status);\r\n      end;\r\n    SERVICE_CONTROL_CONTINUE:\r\n      begin\r\n        Paused := False;\r\n        Status.dwCurrentState := SERVICE_RUNNING;\r\n        SetServiceStatus(StatusHandle, Status);\r\n      end;\r\n    SERVICE_CONTROL_INTERROGATE: SetServiceStatus(StatusHandle, Status);\r\n    SERVICE_CONTROL_SHUTDOWN: Stopped := True;\r\nend;\r\nend;\r\n\r\nprocedure ServiceCtrlDispatcher(dwArgc: dword; var lpszArgv: pchar); stdcall;\r\nbegin\r\nStatusHandle := RegisterServiceCtrlHandler(ServiceName, @ServiceCtrlHandler);\r\nif StatusHandle &lt;&gt; 0 then\r\nbegin\r\n    ZeroMemory(@Status, SizeOf(Status));\r\n    Status.dwServiceType := SERVICE_WIN32_OWN_PROCESS or SERVICE_INTERACTIVE_PROCESS;\r\n    Status.dwCurrentState:= SERVICE_START_PENDING;\r\n    Status.dwControlsAccepted := SERVICE_ACCEPT_STOP or SERVICE_ACCEPT_PAUSE_CONTINUE;\r\n    Status.dwWaitHint := 1000;\r\n    SetServiceStatus(StatusHandle, Status);\r\n    Stopped := False;\r\n    Paused := False;\r\n    Status.dwCurrentState := SERVICE_RUNNING;\r\n    SetServiceStatus(StatusHandle, Status);\r\n    ServiceMain;\r\n    Status.dwCurrentState := SERVICE_STOPPED;\r\n    SetServiceStatus(StatusHandle, Status);\r\nend;\r\nend;\r\n\r\nprocedure UninstallService(ServiceName: pchar);\r\nvar\r\nSCManager: SC_HANDLE;\r\nService: SC_HANDLE;\r\nbegin\r\nSCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);\r\nif SCManager = 0 then Exit;\r\ntry\r\n    Service := OpenService(SCManager, ServiceName, SERVICE_ALL_ACCESS);\r\n    ControlService(Service, SERVICE_CONTROL_STOP, Status);\r\n    DeleteService(Service);\r\n    CloseServiceHandle(Service);\r\nfinally\r\n    CloseServiceHandle(SCManager);\r\nend;\r\nend;\r\n\r\nprocedure InstallService(ServiceName, DisplayName: pchar; FileName: string);\r\nvar\r\nSCManager: SC_HANDLE;\r\nService: SC_HANDLE;\r\nArgs: pchar;\r\nbegin\r\nSCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);\r\nif SCManager = 0 then Exit;\r\ntry\r\n    Service := CreateService(SCManager,         \/\/\u53e5\u67c4\r\n                            ServiceName,        \/\/\u670d\u52a1\u540d\u79f0\r\n                            DisplayName,        \/\/\u663e\u793a\u670d\u52a1\u540d\r\n                            SERVICE_ALL_ACCESS, \/\/\u670d\u52a1\u8bbf\u95ee\u7c7b\u578b\r\n                            SERVICE_WIN32_OWN_PROCESS or SERVICE_INTERACTIVE_PROCESS,\/\/\u670d\u52a1\u7c7b\u578b\r\n                            SERVICE_AUTO_START,   \/\/\u81ea\u52a8\u542f\u52a8\u670d\u52a1\r\n                            SERVICE_ERROR_IGNORE, \/\/\u5ffd\u7565\u9519\u8bef\r\n                            pchar(FileName),      \/\/\u542f\u52a8\u7684\u6587\u4ef6\u540d\r\n                            nil, nil, nil, nil, nil);\r\n    Args := nil;\r\n    StartService(Service, 0, Args);\r\n    CloseServiceHandle(Service);\r\nfinally\r\n    CloseServiceHandle(SCManager);\r\nend;\r\nend;\r\n\r\nbegin\r\nif ParamStr(1) = '\/i' then\r\nbegin\r\n    InstallService(ServiceName, DisplayName, ParamStr(0));\r\nend\r\nelse if ParamStr(1) = '\/u' then\r\nbegin\r\n    UninstallService(ServiceName);\r\nend\r\nelse\r\nbegin\r\n    ServiceTable&#x5B;0].lpServiceName := ServiceName;\r\n    ServiceTable&#x5B;0].lpServiceProc := @ServiceCtrlDispatcher;\r\n    ServiceTable&#x5B;1].lpServiceName := nil;\r\n    ServiceTable&#x5B;1].lpServiceProc := nil;\r\n    StartServiceCtrlDispatcher(ServiceTable&#x5B;0]);\r\nend;\r\nend.\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>program Service; uses Windows, WinSvc; const ServiceNam [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-950","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/950","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=950"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/950\/revisions"}],"predecessor-version":[{"id":4705,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/950\/revisions\/4705"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}