Service applications take requests from client applications, process those requests, and return information to the client applications. They typically run in the background, without much user input. A Web, FTP, or e-mail server is an example of a service application.
New
Other, and double-click Service Application in the New Items dialog box. This adds a global variable named Application to your project, which is of type TServiceApplication.
New
Other, and double-click Service in the New Items dialog box. Do not add services to an application that is not a service application. While a TService object can be added, the application will not generate the requisite events or make the appropriate Windows calls on behalf of the service. To uninstall the services, run it from the command line using the /UNINSTALL option. (You can also use the /SILENT option to suppress the confirmation message when uninstalling).
New
Other and select Service Application from the New Items dialog box. The Service1 window appears. interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs, ScktComp; type TService1 = class(TService) ServerSocket1: TServerSocket; procedure ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket); procedure Service1Execute(Sender: TService); private { Private declarations } Stream: TMemoryStream; // Add this line here public function GetServiceController: PServiceController; override; { Public declarations } end; var Service1: TService1;
//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <SysUtils.hpp> #include <Classes.hpp> #include <SvcMgr.hpp> #include <ScktComp.hpp> //--------------------------------------------------------------------------- class TService1 : public TService { __published: TServerSocket *ServerSocket1; private: TMemoryStream *Stream; // add this line here public: __fastcall TService1(TComponent* Owner); PServiceController __fastcall GetServiceController(void); friend void __stdcall ServiceController(unsigned CtrlCode); }; //--------------------------------------------------------------------------- extern PACKAGE TService1 *Service1; //--------------------------------------------------------------------------- #endif
procedure TService1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket); var Buffer: PChar; begin Buffer := nil; while Socket.ReceiveLength > 0 do begin Buffer := AllocMem(Socket.ReceiveLength); try Socket.ReceiveBuf(Buffer^, Socket.ReceiveLength); Stream.Write(Buffer^, StrLen(Buffer)); finally FreeMem(Buffer); end; Stream.Seek(0, soFromBeginning); Stream.SaveToFile('c:\Temp\Weblog' + IntToStr(ServiceThread.ThreadID) + '.log'); end; end;
void __fastcall TService1::ServerSocket1ClientRead(TObject *Sender, TCustomWinSocket *Socket) { char *Buffer = NULL; int len = Socket->ReceiveLength(); while (len > 0) { try { Buffer = (char *)malloc(len); Socket->ReceiveBuf((void *)Buffer, len); Stream->Write(Buffer, len); } __finally { free(Buffer); } Stream->Seek(0, soFromBeginning); AnsiString LogFile = "C:\\Temp\\WebLog"; LogFile = LogFile + IntToStr(ServiceThread->ThreadID) + ".log"; Stream->SaveToFile(LogFile); } }
procedure TService1.Service1Execute(Sender: TService); begin Stream := TMemoryStream.Create; try ServerSocket1.Port := 80; // WWW port ServerSocket1.Active := True; while not Terminated do begin ServiceThread.ProcessRequests(True); end; ServerSocket1.Active := False; finally Stream.Free; end; end;
void __fastcall TService1::Service1Execute(TService *Sender) { Stream = new TMemoryStream(); try { ServerSocket1->Port = 80; // WWW port ServerSocket1->Active = true; while (!Terminated) ServiceThread->ProcessRequests(true); ServerSocket1->Active = false; } __finally { delete Stream; } }
When writing your service application, you should be aware of:
|
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
|
What do you think about this topic? Send feedback!
|