After using XE development tools, write the Service Application services,
Respectively has the following window code:
1. The service definition
The unit uFileServer;
Interface
USES the
Winapi. Windows, Winapi Messages, System. SysUtils, System. Classes, Vcl. Graphics,
The Vcl. Controls, Vcl SvcMgr, Vcl. Dialogs, Main;
Type
TZJGAFileServer=class (TService)
Procedure ServiceContinue (Sender: TService; Var Continued: Boolean);
Procedure ServiceExecute (Sender: TService);
Procedure ServicePause (Sender: TService; Var Paused: Boolean);
Procedure ServiceShutdown (Sender: TService);
Procedure ServiceStart (Sender: TService; Var Started: Boolean);
Procedure ServiceStop (Sender: TService; Var Stopped: Boolean);
Private
{Private declarations}
Public
The function GetServiceController: TServiceController; Override.
{Public declarations}
end;
Var
ZJGAFileServer: TZJGAFileServer;
FrmMain: TFrmMain;
Implementation
{$R *. DFM}
Procedure ServiceController (CtrlCode: DWord); Stdcall;
The begin
ZJGAFileServer. Controller (CtrlCode);
end;
The function TZJGAFileServer. GetServiceController: TServiceController;
The begin
Result:=ServiceController;
end;
Procedure TZJGAFileServer. ServiceContinue (Sender: TService;
Var Continued: Boolean);
The begin
While not Terminated do
The begin
Sleep (10);
ServiceThread. ProcessRequests (False);
end;
end;
Procedure TZJGAFileServer. ServiceExecute (Sender: TService);
The begin
While not Terminated do
The begin
Sleep (10);
ServiceThread. ProcessRequests (False);
end;
end;
Procedure TZJGAFileServer. ServicePause (Sender: TService; Var Paused: Boolean);
The begin
Paused:=True;
WriteLog (FormatDateTime (' MM - DD YYYY - HH: MM: SS ', Now) + 'ServicePause End. ");
end;
Procedure TZJGAFileServer. ServiceShutdown (Sender: TService);
The begin
G_gbCanClose:=True;
FrmMain. Free;
Status:=csStopped;
ReportStatus ();
end;
Procedure TZJGAFileServer. ServiceStart (Sender: TService; Var Started: Boolean);
The begin
WriteLog (FormatDateTime (' MM - DD YYYY - HH: MM: SS ', Now) + 'ServiceStart End. ");
Started:=True;
The Vcl. Svcmgr. Application CreateForm (TFrmMain FrmMain);
G_gbCanClose:=False;
FrmMain. Hide;
end;
Procedure TZJGAFileServer. ServiceStop (Sender: TService; Var Stopped: Boolean);
The begin
WriteLog (FormatDateTime (' MM - DD YYYY - HH: MM: SS ', Now) + 'ServiceStop End. ");
Stopped:=True;
G_gbCanClose:=True;
FrmMain. Free;
end;
End.
2. Service popups
The unit Main;
Interface
USES the
Winapi. Windows, Winapi Messages, System. SysUtils, System. Variants, System. Classes, Vcl. Graphics,
The Vcl. Controls, Vcl. Forms, Vcl. Dialogs, Vcl. ExtCtrls, ShellApi, Vcl. StdCtrls;
Const
WM_TrayIcon=WM_USER + 1234;
Type
TFrmMain=class (TForm)
Timer1: TTimer;
For: TButton;
TrayIcon1: TTrayIcon;
Procedure FormCreate (Sender: TObject);
Procedure FormCloseQuery (Sender: TObject; Var CanClose: Boolean);
Procedure FormDestroy (Sender: TObject);
Procedure Timer1Timer (Sender: TObject);
Procedure Button1Click (Sender: TObject);
Procedure TrayIcon1DblClick (Sender: TObject);
Private
{Private declarations}
IconData: TNotifyIconData;
Procedure AddIconToTray;
Procedure DelIconFromTray;
Procedure TrayIconMessage (var Msg: TMessage); The message WM_TrayIcon;
Procedure SysButtonMsg (var Msg: TMessage); The message WM_SYSCOMMAND;
Public
{Public declarations}
end;
Var
FrmMain: TFrmMain;
G_gbCanClose: Boolean;
G_FileNum: Integer;
//write temporary log
Procedure WriteLog (Log: String);
Implementation
{$R *. DFM}
//write temporary log
Procedure WriteLog (Log: String);
Var WriteFile: TextFile; SQLFileFullName: String;
The begin
//generated file
SQLFileFullName:='D: \ UpFile \ Log \ test' + FormatFloat (' 0000 ', g_FileNum) + 'Log';
AssignFile (WriteFile SQLFileFullName);
ReWrite (WriteFile);
Try
WriteLn (WriteFile, Log);
The finally
CloseFile WriteFile ();
end;
Inc (g_FileNum);
end;
Procedure SendHokKey; Stdcall;
Var
HDesk_WL: HDESK;
The begin
HDesk_WL:=OpenDesktop (' Winlogon '0, False, DESKTOP_JOURNALPLAYBACK);
If (HDesk_WL & lt;> 0) then
If (SetThreadDesktop (HDesk_WL)=True) then
PostMessage (HWND_BROADCAST, WM_HOTKEY, 0,
MAKELONG (MOD_ALT or MOD_CONTROL VK_DELETE));
end;
Procedure TFrmMain. AddIconToTray;
The begin
ZeroMemory (@ IconData, SizeOf (TNotifyIconData));
IconData. CbSize:=SizeOf (TNotifyIconData);
IconData. Wnd:=Handle;
IconData. : uID=1;
IconData. UFlags:=NIF_MESSAGE or NIF_ICON the or NIF_TIP;
IconData. UCallbackMessage:=WM_TrayIcon;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull