zoukankan      html  css  js  c++  java
  • 写一个服务程序的模板(网上还能搜到很多)

    //unit UntService;
    interface
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
      DB, ADODB,Registry,ActiveX;
    type
      TService2 = class(TService)
        con1: TADOConnection;
        procedure ServiceExecute(Sender: TService);
        procedure ServiceAfterInstall(Sender: TService);
        procedure ServiceContinue(Sender: TService; var Continued: Boolean);
        procedure ServiceStart(Sender: TService; var Started: Boolean);
        procedure ServicePause(Sender: TService; var Paused: Boolean);
      private
        { Private declarations }
      public
        function GetServiceController: TServiceController; override;
        { Public declarations }
      end;
    var
      Service2: TService2;
    implementation
    {$R *.DFM}
    procedure ServiceController(CtrlCode: DWord); stdcall;
    begin
      Service2.Controller(CtrlCode);
    end;
    function TService2.GetServiceController: TServiceController;
    begin
      Result := ServiceController;
    end;
    procedure TService2.ServiceExecute(Sender: TService);
    begin
     while not Terminated do
      begin
        ServiceThread.ProcessRequests(True);
        //do some thing;
        //这里VCL创建了一个线程,并在线程中实现了一个消息泵来接收消息
        //你可以把循环检测的事件加到这里
        Sleep(100);
      end;
    end;
    procedure TService2.ServiceAfterInstall(Sender: TService);
    var
      Registry: TRegistry;
      key: String;
    begin
      Registry  := TRegistry.Create;
      try
        Registry.RootKey := HKEY_LOCAL_MACHINE;
        key := 'System/CurrentControlSet/Services/' + Name;
        if Registry.KeyExists(key) then
        begin
          Registry.OpenKey(key, True);
          Registry.WriteString('Description', 'XXXXXXXXXXXXX');
          Registry.CloseKey;
        end;
      finally
        Registry.Free;
      end;
    end;
    procedure TService2.ServiceContinue(Sender: TService;
      var Continued: Boolean);
    begin
      Continued := True;
    end;
    procedure TService2.ServiceStart(Sender: TService; var Started: Boolean);
    begin
      CoInitialize(nil);
      try
        con1.Open;
      except
        LogMessage('Database connection error');
      end;
      Started := True;
    end;
    procedure TService2.ServicePause(Sender: TService; var Paused: Boolean);
    begin
      Paused := True;
    end;
    
    end.

    参考:http://www.cnblogs.com/key-ok/p/3358916.html

  • 相关阅读:
    电脑开机时一直滴滴的响开不了机是为什么?
    电脑开机时一直滴滴的响开不了机是为什么?
    winform窗体应用实现淡入淡出等效果
    winform窗体应用实现淡入淡出等效果
    windows-如何生成转储(dmp)文件--工具篇
    面向对象(三)- Java类的方法
    面向对象 (二)- Java类的属性
    面向对象 (二)- Java类的属性
    面向对象 (一)- Java中的类和对象
    面向对象 (一)- Java中的类和对象
  • 原文地址:https://www.cnblogs.com/findumars/p/4748565.html
Copyright © 2011-2022 走看看