码迷,mamicode.com
首页 > 其他好文 > 详细

写一个服务程序的模板(网上还能搜到很多)

时间:2015-08-21 19:10:42      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

//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

写一个服务程序的模板(网上还能搜到很多)

标签:

原文地址:http://www.cnblogs.com/findumars/p/4748565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!