Delayed DLL directive causing application deadlock on FireDAC query - delphi

I have an application set up in the following manner in Delphi XE5:
main.exe: calls a function in sub.dll using the export delayed directive
function MyFunction: boolean; external 'sub.dll' delayed;
sub.dll: contains a FireDAC query object which runs a simple SELECT query.
Upon opening the query, with the delayed directive the application does not terminate when the main form is closed (process main.exe remains in task manager). Process explorer shows a thread remaining for sub.dll. The main.exe process terminates correctly when I do not specify the delayed directive. What am I missing? I feel like I'm not freeing an object but I can't figure out what it is.
Simplified code:
Main.exe:
program Main;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
function MyFunction: boolean; external 'Sub.dll' delayed;
begin
try
MyFunction;
except
on E: Exception do begin
Writeln(E.ClassName, ': ', E.Message);
readln;
end;
end;
end.
Sub.dll
library Sub;
uses
System.SysUtils,
System.Classes,
DBConn in 'DBConn.pas';
{$R *.res}
function MyFunction: boolean; export;
var Conn: TConn;
begin
Conn := TConn.Create;
Conn.Destroy;
Result := True;
end;
exports
MyFunction;
begin
end.
DBConn.pas
unit DBConn;
interface
uses
FireDAC.Stan.Intf, FireDAC.Stan.Option,
FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
FireDAC.Phys, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Stan.Param,
FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, FireDAC.VCLUI.Wait,
FireDAC.Comp.UI, FireDAC.Phys.ODBCBase, FireDAC.Phys.ASA, Data.DB,
FireDAC.Comp.DataSet, FireDAC.Comp.Client;
type
TConn = class
FDConnection: TFDConnection;
FDQuery: TFDQuery;
constructor Create;
destructor Destroy; override;
end;
var
Conn: TConn;
{ TConn }
implementation
constructor TConn.Create;
begin
FDConnection := TFDConnection.Create(nil);
//Set database connection parameters
with FDConnection do begin
close; Params.Clear;
Params.Add('DriverID=ASA');
Params.Add('Database=');
Params.Add('Server=');
Params.Add('USER_NAME=');
Params.Add('PASSWORD=');
open;
end;
FDQuery := TFDQuery.Create(nil);
with FDQuery do begin
Connection := FDConnection;
close; unprepare; SQL.Clear;
SQL.Add('Select first LAST_NAME');
SQL.Add('From USERS');
SQL.Add('Order By LAST_NAME');
prepare; open; //this causes the deadlock
writeln(Output, FieldByName('LAST_NAME').AsString);
end;
end;
destructor TConn.Destroy;
begin
FDConnection.Close;
FDConnection.Free;
inherited;
end;
end.

This is a FireDAC limitation. See http://docwiki.embarcadero.com/RADStudio/XE8/en/DLL_Development_(FireDAC)#FireDAC_DLL_Unloading

VCL will reliably work in Dll only if your main Exe and Dll are compiled in the same version of Delphi with runtime packages enabled for them.

Related

Delphi FreeLibrary freezes when using TTask in DLL

Here is my code in DLL:
procedure TTaskTest;
begin
TTask.Run(
procedure
begin
Sleep(300);
end);
end;
exports TTaskTest;
After calling this method in host app, then call FreeLibrary will freeze host app.
After debug , I found that the program freezes at if TMonitor.Wait(FLock, Timeout) then in TLightweightEvent.WaitFor , but the debugger cannot step into TMonitor.Wait.
How to solve?
This issue was reported (RSP-13742 Problem with ITask, IFuture inside DLL).
It was closed "Works as Expected" with a remark:
To prevent this failure using ITask or IFuture from a DLL, the DLL will need to be using its own instance of TThreadPool in place of the default instance of TThreadPool.
Here is an example from Embarcadero how to handle it:
library TestLib;
uses
System.SysUtils,
System.Classes,
System.Threading;
{$R *.res}
VAR
tpool: TThreadPool;
procedure TestDelay;
begin
tpool := TThreadPool.Create;
try
TTask.Run(
procedure begin
Sleep(300);
end,
tpool
);
finally
FreeAndNil(tpool);
end;
end;
exports
TestDelay;
begin
end.
Another way is to create the threadpool when the library is loaded, and add a release procedure, which you call before calling FreeLibrary.
// In dll
procedure TestDelay;
begin
TTask.Run(
procedure begin
Sleep(300);
end,
tpool
);
end;
procedure ReleaseThreadPool;
begin
FreeAndNil(tpool);
end;
exports
TestDelay,ReleaseThreadPool;
begin
tpool := TThreadPool.Create;
end.

Why won't TTimer work in OTL worker task?

I wanted to realize a repetitive task in an OmniThreadLibrary worker task, that runs in another thread. The task should be executed every 3 seconds, for example.
Therefore I wrote a TOmniWorker descendant with an instance of TTimer as you can see below:
program Project14;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Vcl.ExtCtrls,
Vcl.Forms,
OtlTaskControl;
type
TMyTimerWorker = class(TOmniWorker)
strict private
FTimer: TTimer;
procedure DoOnTimer(Sender: TObject);
protected
function Initialize: Boolean; override;
procedure Cleanup; override;
end;
{ TMyTimerWorker }
procedure TMyTimerWorker.Cleanup;
begin
FTimer.Free;
inherited;
end;
procedure TMyTimerWorker.DoOnTimer(Sender: TObject);
begin
Beep;
end;
function TMyTimerWorker.Initialize: Boolean;
begin
Result := inherited;
if not Result then exit;
FTimer := TTimer.Create(nil);
FTimer.OnTimer := DoOnTimer;
FTimer.Interval := 3000;
FTimer.Enabled := True; // note: this isn't necessary, but is added to avoid hints that 'Enabled' might be 'False'
end;
var
LTimerWorker: IOmniWorker;
begin
try
LTimerWorker := TMyTimerWorker.Create;
CreateTask(LTimerWorker).Unobserved.Run;
while True do
Application.ProcessMessages;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
I set breakpoints in Initialize and DoOnTimer. Former executes well but latter won't be called at all. BTW: Cleanup isn't called neither, so the task is still running.
What am I doing wrong? Is it impossible to use a TTimer in an OTL task? If yes, why?
UPDATE: I found a workaround for TTimer () but why does TTimer approach not work?
You TTimer-based code doesn't work because TTimer uses windows messages to trigger the timer event and windows messages are not processed in an OTL worker by default.
Call .MsgWait before .Run and internal worker loop will use MsgWaitForMultipleObjects instead of WaitForMultipleObjects which will allow for message processing.
Saying that, you really should not use TTimer in background tasks because - as others have said - TTimer is not threadsafe.

Windows service in created and started in Delphi, bot not properly running

Hi i have created windows services in Delphi. I have installed and started the Services. Every thing working fine. Even i can check it in Task Manager.My service is Running.
But the code which i included inside the OnExecute method is not working.
My whole Code:
unit MyService;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.SvcMgr, Vcl.Dialogs,
Vcl.ExtCtrls,Jira;
type
TJiraTestlink = class(TService)
procedure ServiceExecute(Sender: TService);
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;
var
JiraTestlink: TJiraTestlink;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
JiraTestlink.Controller(CtrlCode);
end;
function TJiraTestlink.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TJiraTestlink.ServiceExecute(Sender: TService);
const
SecBetweenRuns = 10;
var
Count: Integer;
begin
Count := 0;
while not Terminated do
begin
Inc(Count);
if Count >= SecBetweenRuns then
begin
Count := 0;
{ place your service code here }
{ this is where the action happens }
ShowMessage(DateTimeToStr(Now));
end;
Sleep(1000);
ServiceThread.ProcessRequests(False);
end;
end;
end.
I am not sure where i did mistake. Any help will be appreciated. Thanks.
Since Vista, services are isolated and run in session 0, a non-interactive session. Interactive processes run in different sessions, starting from session 1 for the first logged on user.
This means that you cannot use your service to show UI. Calls to ShowMessage cannot work, by design, in a service.
You will need to find some other way to debug your service. For instance logging messages to a text file. Or OutputDebugString and a viewer like SysInternals DebugView which is capable of catching these debug strings from different sessions.

DataSnap Rest Server windows Service

i'm try to building a DataSnap Rest Application running as a windows service, but the wizard just have "Stand-alone VCL application", "Stand-alone console application" and "ISAPI dynamic link library"(i'm using Delphi XE2 enterprise). Someone can help me. Thanks.
It would be more convenient if the DataSnap REST wizard had the option to create a Windows Service (how else would you run an application server?), but you can work around it with a little fiddling.
The first time I ran in to this, I created a regular DataSnap REST server as a VCL application and a regular Windows service and copied the relevant portions from the REST server to the service. Just make sure the output directory for the service is the same as the directory for the VCL application.
The service's .dpr might look like this:
program Service;
uses
Vcl.SvcMgr,
Web.WebReq,
IdHTTPWebBrokerBridge,
WebModuleUnit1 in '..\GUI\WebModuleUnit1.pas' {WebModule1: TWebModule},
ServerMethodsUnit1 in '..\GUI\ServerMethodsUnit1.pas' {ServerMethods1: TDSServerModule},
ServerContainerUnit1 in '..\GUI\ServerContainerUnit1.pas' {ServerContainer1: TDataModule},
Unit1 in 'Unit1.pas' {Service1: TService};
{$R *.RES}
begin
if not Application.DelayInitialize or Application.Installing then
Application.Initialize;
if WebRequestHandler <> nil then
WebRequestHandler.WebModuleClass := WebModuleClass;
Application.CreateForm(TService1, Service1);
Application.Run;
end.
The service's main unit might look like this:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.SvcMgr, Vcl.Dialogs,
IdHTTPWebBrokerBridge, Web.HTTPApp;
type
TService1 = class(TService)
procedure ServiceCreate(Sender: TObject);
procedure ServiceExecute(Sender: TService);
procedure ServiceStart(Sender: TService; var Started: Boolean);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
private
FServer: TIdHTTPWebBrokerBridge;
procedure TerminateThreads;
public
function GetServiceController: TServiceController; override;
end;
var
Service1: TService1;
implementation
{$R *.DFM}
uses
//Datasnap.DSService; // XE2
Datasnap.DSSession; // XE3
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
Service1.Controller(CtrlCode);
end;
{ TService1}
function TService1.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TService1.ServiceCreate(Sender: TObject);
begin
FServer := TIdHTTPWebBrokerBridge.Create(Self);
end;
procedure TService1.ServiceExecute(Sender: TService);
begin
while not Terminated do
begin
Sleep(1000);
ServiceThread.ProcessRequests(False);
end;
end;
procedure TService1.ServiceStart(Sender: TService; var Started: Boolean);
begin
if not FServer.Active then
begin
FServer.Bindings.Clear;
FServer.DefaultPort := 8080;
FServer.Active := True;
end;
end;
procedure TService1.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
TerminateThreads;
FServer.Active := False;
FServer.Bindings.Clear;
ServiceThread.Terminate;
end;
procedure TService1.TerminateThreads;
begin
if TDSSessionManager.Instance <> nil then
TDSSessionManager.Instance.TerminateAllSessions;
end;
end.

Can I get notification that a process I did not spawn is shutting down in Windows XP/7?

I have a Delphi 6 application that works with the Skype API. I want to know when the Skype client has shut down even though my software did not launch it (so I don't have a process handle for it). This way I can know if the user shut down the Skype client I can get the process ID for the Skype client fairly easily, so is there a Windows API call or other technique that accepts a process ID where I can get a notification when the process (Skype client) has terminated?
If not, is there a WinApi call that I can use to poll Windows to see if the process ID is still valid, or does Windows reuse process IDs so there's a chance that I could end up with a process ID belonging to a recently launched process that is not the Skype client, which would invalidate my polling efforts?
Call OpenProcess to get a process handle. The SYNCHRONIZE access right will probably be enough. Then wait on the handle. Something like:
HANDLE hProcess = OpenProcess(SYNCHRONIZE, FALSE, pid);
WaitForSingleObject(hProcess, INFINITE);
CloseHandle(hProcess);
You can use the __InstanceDeletionEvent WMI intrinsic event to monitor the Win32_Process class and the filter by the ProcessId property, this event run in async mode in your code.
Check this sample code (Written in delphi XE2, but must work in delphi 6 without problems)
Note : You must import the Microsoft WMI Scripting V1.2 Library before to use it.
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, WbemScripting_TLB;
type
TWmiAsyncEvent = class
private
FWQL : string;
FSink : TSWbemSink;
FLocator : ISWbemLocator;
FServices : ISWbemServices;
procedure EventReceived(ASender: TObject; const objWbemObject: ISWbemObject; const objWbemAsyncContext: ISWbemNamedValueSet);
public
procedure Start;
constructor Create(Pid : DWORD);
Destructor Destroy;override;
end;
TFrmDemo = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
AsyncEvent : TWmiAsyncEvent;
public
{ Public declarations }
end;
var
FrmDemo: TFrmDemo;
implementation
{$R *.dfm}
uses
ActiveX;
{ TWmiAsyncEvent }
constructor TWmiAsyncEvent.Create(Pid: DWORD);
begin
inherited Create;
CoInitializeEx(nil, COINIT_MULTITHREADED);
FLocator := CoSWbemLocator.Create;
FServices := FLocator.ConnectServer('.', 'root\CIMV2', '', '', '', '', wbemConnectFlagUseMaxWait, nil);
FSink := TSWbemSink.Create(nil);
FSink.OnObjectReady := EventReceived;
//construct the WQL sentence with the pid to monitor
FWQL:=Format('Select * From __InstanceDeletionEvent Within 1 Where TargetInstance ISA "Win32_Process" And TargetInstance.ProcessId=%d',[Pid]);
end;
destructor TWmiAsyncEvent.Destroy;
begin
if FSink<>nil then
FSink.Cancel;
FLocator :=nil;
FServices :=nil;
FSink :=nil;
CoUninitialize;
inherited;
end;
procedure TWmiAsyncEvent.EventReceived(ASender: TObject;
const objWbemObject: ISWbemObject;
const objWbemAsyncContext: ISWbemNamedValueSet);
var
PropVal: OLEVariant;
begin
PropVal := objWbemObject;
//do something when the event is received.
ShowMessage(Format('The Application %s Pid %d was finished',[String(PropVal.TargetInstance.Name), Integer(PropVal.TargetInstance.ProcessId)]));
end;
procedure TWmiAsyncEvent.Start;
begin
FServices.ExecNotificationQueryAsync(FSink.DefaultInterface,FWQL,'WQL', 0, nil, nil);
end;
procedure TFrmDemo.FormCreate(Sender: TObject);
begin
//here you must pass the pid of the process
AsyncEvent:=TWmiAsyncEvent.Create(1852);
AsyncEvent.Start;
end;
procedure TFrmDemo.FormDestroy(Sender: TObject);
begin
AsyncEvent.Free;
end;
end.
For more info you can check this article Delphi and WMI Events
Windows does reuse process IDs, so do not rely on that by itself.
You can use EnumProcesses() to know which processes are currently running, then grab their filenames and process IDs, etc. See this example on MSDN.

Resources