服务应用程序-----与高手探讨
xfax 2004-07-11 12:51:46 我要写一个服务应用程序,在后台用udp监视一个80端口,并把受到数据写入数据库,程序框架如下,运行后表面上正常,但发现没有打开数据库,也不知跟踪调试服务应用程序.
unit svcudp;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer, IdSocketHandle,
shellAPI, DB, ADODB, Variants, AppEvnts;
const
connstr = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist urity Info=False';
type
Txypowersvc = class(TService)
IdUDPS: TIdUDPServer;
cn: TADOConnection;
reglog: TADODataSet;
app: TApplicationEvents;
procedure IdUDPSUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure ServiceExecute(Sender: TService);
procedure ServiceStart(Sender: TService; var Started: Boolean);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
private
{ Private declarations }
public
{ Public declarations }
function GetServiceController: TServiceController; override;
procedure analyserev(revstr: string; var tmpreginfo: reginfo);
procedure writedatabase(revreginfo: reginfo; var answerstr: string);
end;
var
xypowersvc: Txypowersvc;
implementation
uses Unit1;
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
xypowersvc.Controller(CtrlCode);
end;
function Txypowersvc.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure Txypowersvc.IdUDPSUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
DataStringStream: TStringStream;
revstr, answerstr: string;
revreginfo: reginfo;
begin
DataStringStream := TStringStream.Create('');
DataStringStream.CopyFrom(AData, AData.Size); //读接受缓冲区
revstr := DataStringStream.DataString; //将数据转成字符串
analyserev(revstr, revreginfo); //一个特定数据分析程序,是正确的.
writedatabase(revreginfo, answerstr); //写数据库程序也正确
end;
procedure Txypowersvc.ServiceExecute(Sender: TService);
begin
IdUDPS.Active := true;
while not xypowersvc.Terminated do
begin
xypowersvc.ServiceThread.ProcessRequests(False);
end;
end;
procedure Txypowersvc.ServiceStart(Sender: TService; var Started: Boolean);
var
_AppPath: string;
begin
//打开当前目录下的数据库
_AppPath := GetCurrentDir + '\datacent.mdb';
cn.Close;
cn.ConnectionString := format(connstr, [_AppPath]);
try
cn.Open; //开连接
reglog.Open; //开表
except
exit;
end;
///上面几句在其他地方正确,在这个服务应用程序好像没有起作用.
IdUDPS.Active := true;
end;
procedure Txypowersvc.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
IdUDPS.Active := False;
end;
................