Home > Back-end >  Delphi written after the PC program, how to join service start?
Delphi written after the PC program, how to join service start?

Time:11-13

Written in Delphi a PC software can run normally, now want to join to start the service way, ladies and gentlemen, I would like to ask about how to implement the service start, best post code

CodePudding user response:

Written services directly, if there are interface into a service, a bit of trouble,

CodePudding user response:

Service and desktop interaction function only win 2 k, xp, can server2003, Windows NT before, after the Vista + don't support, routine is the service start a separate interface program, using CreateProcessAsUser,

CodePudding user response:

Reference: https://docs.microsoft.com/zh-cn/windows/desktop/Services/interactive-services

CodePudding user response:

refer to the second floor early play big play nuclear response:
service and desktop interaction function only win 2 k, xp, can server2003, Windows NT, before after Vista + does not support, the conventional approach is to start a separate service interface program, using CreateProcessAsUser, such as


The equivalent of two applications

CodePudding user response:

///this thread is the unit/////////////////////////////////////


The unit WorkerThreadU;

Interface

USES the
System. Classes;

Type
TWorkerThread=class (TThread)
Private
FPaused: Boolean;

Protected
Procedure the Execute; Override.
Public
Procedure Pause;
Procedure Continue;
end;

Implementation

USES the
System. The SysUtils, System. Ioutils;

Procedure TWorkerThread. Continue;
The begin
FPaused:=False;
end;

Procedure TWorkerThread. Execute;
Var
ExePath LogFileName: string;
Log: TStreamWriter;
The begin
Try
FPaused:=False;
ExePath:=TPath GetDirectoryName (GetModuleName (HInstance));
Bine LogFileName:=TPath.Com (ExePath,
The ClassName + '_' + IntToStr (CurrentThread. ThreadID) + '. TXT ');
Log:=TStreamWriter. Create (TFileStream. Create (LogFileName fmCreate or fmShareDenyWrite));
Try
While not Terminated do
The begin
If not FPaused then
The begin
The Log. WriteLine (" the Message from the thread: '+ TimeToStr (now));
end;
TThread. Sleep (1000);
end;
The finally
The Free;
end;
Except,
On E: the Exception do
The begin
Bine TFile. WriteAllText (TPath.Com (ExePath, 'CRASH_LOG. TXT), e.c. with our fabrication: lassName + "+ E.M essage);
End
end;
end;

Procedure TWorkerThread. Pause;
The begin
FPaused:=True;
end;

End.

///////////////////the following is a service unit///////////////////////////////////
The unit ServiceU;

Interface

USES the
Winapi. Windows, Winapi Messages, System. SysUtils, System. Classes, Vcl. Graphics, Vcl. Controls, Vcl. SvcMgr, Vcl. Dialogs,
WorkerThreadU;

Type
TSampleService=class (TService)
Procedure ServiceExecute (Sender: TService);
Procedure ServiceStart (Sender: TService; Var Started: Boolean);
Procedure ServiceStop (Sender: TService; Var Stopped: Boolean);
Procedure ServicePause (Sender: TService; Var Paused: Boolean);
Procedure ServiceContinue (Sender: TService; Var Continued: Boolean);
Procedure ServiceAfterInstall (Sender: TService);
Private
FWorkerThread: TWorkerThread;
{Private declarations}
Public
The function GetServiceController: TServiceController; Override.
{Public declarations}
end;

{$R *. DFM}


Var
SampleService: TSampleService;

Implementation

USES the
System. Win. Registry;

Procedure ServiceController (CtrlCode: DWord); Stdcall;
The begin
SampleService. Controller (CtrlCode);
end;

The function TSampleService. GetServiceController: TServiceController;
The begin
Result:=ServiceController;
end;

Procedure TSampleService. ServiceAfterInstall (Sender: TService);
Var
Reg: TRegistry;
The begin
Reg:=TRegistry. Create (KEY_READ or KEY_WRITE);
Try
Reg. RootKey:=HKEY_LOCAL_MACHINE;
If Reg. OpenKey (' \ SYSTEM \ CurrentControlSet \ Services \ '+ name, false) then
The begin
Reg. WriteString (' Description ', 'My Fantastic Windows Service);
Reg. CloseKey;
end;
The finally
Reg. Free;
end;
end;

Procedure TSampleService. ServiceContinue (Sender: TService;
Var Continued: Boolean);
The begin
FWorkerThread. Continue;
Continued:=True;
end;

Procedure TSampleService. ServicePause (Sender: TService; Var Paused: Boolean);
The begin
FWorkerThread. Pause;
Paused:=True;
end;

Procedure TSampleService. ServiceStart (Sender: TService; Var Started: Boolean);
The begin
FWorkerThread:=TWorkerThread. Create (True);
FWorkerThread. Start;
Started:=True;
end;

Procedure TSampleService. ServiceStop (Sender: TService; Var Stopped: Boolean);
The begin
FWorkerThread. The Terminate;
FWorkerThread. WaitFor;
FreeAndNil (FWorkerThread);
Stopped:=True;
end;

Procedure TSampleService. ServiceExecute (Sender: TService);
The begin
While not Terminated do
The begin
nullnullnullnullnullnullnullnullnullnullnull
  • Related