请问哪位高手写过windows services,能给我个详细的例子吗?(200分)

  • 主题发起人 主题发起人 finalrinoa
  • 开始时间 开始时间
F

finalrinoa

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样才能在services中调用windows拨号程序
 
E-Mail:lunart@sina.com
 
the service (just beeps the speaker every 5 secs) :<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp;Windows, Messages, SysUtils, Classes,SvcMgr, ExtCtrls &nbsp;;<br><br>type<br>&nbsp;TService2 = class(TService)<br>&nbsp; &nbsp;Timer1: TTimer;<br>&nbsp; &nbsp;procedure Timer1Timer(Sender: TObject);<br>&nbsp;private<br>&nbsp; &nbsp;{ Private declarations }<br>&nbsp;public<br>&nbsp; &nbsp;function GetServiceController: TServiceController; override;<br>&nbsp; &nbsp;{ Public declarations }<br>&nbsp;end;<br><br>var<br>&nbsp;Service2: TService2;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure ServiceController(CtrlCode: DWord); stdcall;<br>begin<br>&nbsp;Service2.Controller(CtrlCode);<br>end;<br><br>function TService2.GetServiceController: TServiceController;<br>begin<br>&nbsp;Result := ServiceController;<br>end;<br><br>procedure TService2.Timer1Timer(Sender: TObject);<br>begin<br>MessageBeep(1);<br>end;<br><br>end.<br><br><br><br>control app(not nessesary if you use control panel) :<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp;StdCtrls,WinSvc;<br><br>type<br>&nbsp;TForm1 = class(TForm)<br>&nbsp; &nbsp;Button1: TButton;<br>&nbsp; &nbsp;Button2: TButton;<br>&nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp;procedure Button2Click(Sender: TObject);<br>&nbsp;private<br>&nbsp; &nbsp;{ Private declarations }<br>&nbsp;public<br>&nbsp; &nbsp;{ Public declarations }<br>&nbsp;end;<br><br>var<br>&nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>function ServiceStart(sMachine, sService: String) : Boolean;<br>var<br>&nbsp; schm, &nbsp; schs: SC_Handle;<br>&nbsp; ss: TServiceStatus;<br>&nbsp; psTemp: PChar;<br>&nbsp; dwChkP: DWord;<br>begin<br>&nbsp; ss.dwCurrentState := 0;<br>&nbsp;schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);<br>&nbsp; if (schm&gt;0) then<br>&nbsp; begin<br>&nbsp; &nbsp;schs := OpenService(schm, PChar(sService), SERVICE_START or<br>SERVICE_QUERY_STATUS);<br>&nbsp; &nbsp; if (schs&gt;0) then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;psTemp := nil;<br>&nbsp; &nbsp; &nbsp; if (StartService(schs, 0, psTemp)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; if (QueryServiceStatus(schs, ss)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while (SERVICE_RUNNING&lt;&gt;ss.dwCurrentState) do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dwChkP := ss.dwCheckPoint;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sleep(ss.dwWaitHint);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (not QueryServiceStatus(schs, ss)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ss.dwCheckPoint &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; CloseServiceHandle(schs);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp;CloseServiceHandle(schm);<br>&nbsp; end;<br>&nbsp;Result := SERVICE_RUNNING=ss.dwCurrentState;<br>end;<br><br>function ServiceStop(sMachine, sService: String) : Boolean;<br>var &nbsp; schm, &nbsp; schs: SC_Handle;<br>&nbsp; ss: TServiceStatus;<br>&nbsp;dwChkP: DWord;<br>begin<br>schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);<br>&nbsp;if (schm&gt;0) then &nbsp; begin<br>&nbsp; &nbsp; schs := OpenService(schm, PChar(sService), SERVICE_STOP or<br>SERVICE_QUERY_STATUS);<br>&nbsp; &nbsp; if (schs&gt;0) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp;if (ControlService(schs, SERVICE_CONTROL_STOP, ss)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; if (QueryServiceStatus(schs, ss)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while (SERVICE_STOPPED&lt;&gt;ss.dwCurrentState) do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dwChkP := ss.dwCheckPoint;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sleep(ss.dwWaitHint);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (not QueryServiceStatus(schs, ss)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ss.dwCheckPoint &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; CloseServiceHandle(schs);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp;CloseServiceHandle(schm);<br>&nbsp; end;<br>&nbsp; Result := SERVICE_STOPPED=ss.dwCurrentState;<br>end;<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>ServiceStart('','Service2');<br>file://ServiceStart('//computername','Alerter');<br>file://this would be for remote services<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>ServiceStop('','Service2');<br>end;<br><br>end.<br><br><br>you can also install it from code (still working on that part myself) using<br>CreateService etc..<br><br>theres very little docs on services so if anyone else has any feel free to<br>add here :-)<br>
 
http://www.delphibbs.com/delphibbs/dispq.asp?LID=2021867
 
to 如云<br>我要的是windows services的具体写法和注意事项,我并不是要什么其它的东西<br>不过还是谢谢你<br>
 
to finalrinoa <br>&nbsp; &nbsp;用Delphi的向导就 OK 了,其他和写正常的应用程序没什么区别。关键是你的服务与应用程序提供什么接口(TCP/IP/RPC/共享内存....)。
 
---- Windows NT服务程序不同于一般的运行程序,它不需要NT登录进去,只需要开机进入NT系统便可以运行,一般用于系统服务方面的应用,学会编写NT服务程序对网络管理人员而言是非常重要的,Delphi4.0作为一种高效、快速、强大的开发语言,为开发NT服务程序提供了非常便捷的方法,加上其可视化界面以及与数据库的完美结合,使我们开发与数据库有关的NT服务程序变得非常简单,下面以编写一个NT定期备份程序为例,介绍如何运用Delphi编写Windows NT的服务程序。 <br><br>---- 打开Delphi编辑器,选择菜单中的File|New,在New Item中选择Service Application项,Delphi便自动为你建立一个基于TServiceApplication的新工程,TserviceApplication是一个封装NT服务程序的类,它包含一个Tservice1对象以及服务程序的装卸、注册、取消方法。 <br><br>---- 将Tservice1对象的属性做下列更改: <br><br>DisplayName与Name改为:DataBackup,<br>ServiceStartName属性改为系统管理员<br>用户(如DOMAIN/Administrator)和Password则输入用户密码<br><br>---- 这样,服务程序运行时将自己以该用户的权限操作NT。 <br><br>---- 这样,一个NT服务程序编写已经完成,在Delphi编辑器中选择菜单Run|Parameters,在Parameters中输入/install,程序编译运行后,一个名为DataBackup的NT服务程序已经安装好,你可以双击控制面板中的服务项目,将会看到此服务程序,只是此服务程序没有任何东西而已;在Delphi编辑器中选择菜单Run|Parameters,在Parameters中输入/uninstall,程序编译运行后,系统将会将此服务程序卸掉。 <br><br>---- 服务程序是通过控制一个线程的生成、暂停、继续、停止来达到服务目的的,因此我们必须加入一个Tsession对象来编写文件自动备份代码,在Delphi编辑器中选择菜单中的File|New,在New Item中选择Thread Object项,Delphi会提示你为该Tsession对象输入一个名称(输入DataCopy),Delphi便生成了一个基于Tsession的DataCopy对象,并提供了一个Execute过程供重载,我们要Execute过程中输入以下程序: <br><br>var<br>Hour, Min, Sec, MSec: Word;<br>TimeStamp,DirectoryEdit1,DirectoryEdit2:String;<br>SearchRec: TSearchRec;<br>Source,Temp,Dest:Pchar;<br>F,F1:THandle;<br>FF,FF1:WIN32_FIND_DATA;<br>Begin<br>{每次文件备份执行时间}<br>TimeStamp:='12:00';<br>{文件备份源目录}<br>DirectoryEdit1:='C:/temp';<br>{文件备份目录}<br>DirectoryEdit2:='C:/temp1';<br>while True do<br>begin<br>DecodeTime(Time,Hour, Min, Sec, MSec);<br>IF Trim(TimeStamp)=Format<br>('%-2.2d:%-2.2d',[Hour,Min]) then<br>begin<br>GetMem(Source,250);<br>GetMem(Dest,250);<br>GetMem(Temp,250);<br><br>StrPcopy(Dest,DirectoryEdit2+'/<br>'+FormatDateTime('YYYYMMDD',Date));<br>CreateDirectory(Dest,nil);<br>IFFindFirst(DirectoryEdit1+'/*.*',faAnyFile,<br>SearchRec)=0 then<br>begin<br>repeat<br>StrPcopy(Source,DirectoryEdit1+'/'+SearchRec.Name);<br><br>StrPcopy(Dest,DirectoryEdit2+'/'<br>+FormatDateTime('YYYYMMDD',Date)+'/'+SearchRec.Name);<br>copyfile(Source,Dest,False);<br>until FindNext(SearchRec)&lt; &gt;0;<br>end;<br>SysUtils.FindClose(SearchRec);<br>FindClose(F);<br>FreeMem(Source,250);<br>FreeMem(Dest,250);<br>FreeMem(Temp,250);<br>end;<br>sleep(60000);<br>end;<br><br>end;<br><br>---- 此线程执行时每隔一分钟将检查一次时间,看是否到了备份时间,如果是则将DirectoryEdit1中的所有文件拷到DirectoryEdit2目录中去。 <br>---- 现在编写服务控制DataCopy线程的代码,在TdataBackup对象中的OnStart、OnStop、OnPause、OnContinue事件中分别输入如下代码: <br><br>procedure TDataBackup.DataBackupStart<br>(Sender: TService;<br>var Started: Boolean);<br>begin<br>DataThread:= TDataCopy.Create(False);<br>Started := True;<br>end;<br><br>procedure TDataBackup.DataBackupStop<br>(Sender: TService;<br>var Stopped: Boolean);<br>begin<br>DataThread.Terminate;<br>Stopped := True;<br>end;<br><br>procedure TDataBackup.DataBackupPause<br>(Sender: TService;<br>var Paused: Boolean);<br>begin<br>DataThread.Suspend;<br>Paused := True;<br>end;<br><br>procedure TDataBackup.DataBackupContinue<br>(Sender: TService;<br>var Continued: Boolean);<br>begin<br>DataThread.Resume;<br>Continued := True;<br>end;<br><br>---- 这样一个文件自动备份程序已经完成,编译好后,加上/install参数执行程序,系统会将此服务程序安装,由于服务程序中StartType属性为stAuto,NT每次启动时,此程序自动执行,你可以在控制面板中的服务项目来启动、暂停、恢复、停止它,也可以加上/uninstall参数执行程序来卸掉它。通过上述例子我们可以看到,用Delphi编写NT服务器程序十分方便,而且你也可以在Tservice对象上加许多非可视化控件来与数据库关联,实现很多复杂的功能。<br>&nbsp;<br><br>
 
谢谢laoli
 
后退
顶部