Unable to use LazReport In DLL library under Windows 10
Original Reporter info from Mantis: zgabrovski
-
Reporter name: Zdravko Gabrovski
Original Reporter info from Mantis: zgabrovski
- Reporter name: Zdravko Gabrovski
Description:
I have a DLL library-plugin project that uses lazReport for preview and print various kind of reports.
My first try to use library under Windows 10 causes freeze of my host application, when it try to load library via standart SafeLoadLibrary function.
After deep debug i found, that LazReport LR_Prntr unit freeze under initialization section.
A line
Prn.Printer:=Printer;
Calls Windows API
CreateDCW
in
{fpcdir}\lazarus\components\printers\win32\winprinters.inc
Which hangs under Windows 10.
I think, that the problem is very similar as described here:
https://bugs.freepascal.org/view.php?id=26801
and here:
https://docs.microsoft.com/en-gb/windows/win32/dlls/dllmain?redirectedfrom=MSDN
*
The entry-point function should perform only simple initialization or termination tasks. It must not call the LoadLibrary or LoadLibraryEx function (or a function that calls these functions), because this may create dependency loops in the DLL load order. This can result in a DLL being used before the system has executed its initialization code.
*
So, the fix I did is to remove then Prn initialization object code from initialization section, by creating a new "GlobalPrn" object, and replace a Prn with a function, that will init the Prn object on it first call:
in Interface section:
var
GlobalPrn: TfrPrinter = nil;
function Prn : TfrPrinter;
in implementation section:
function Prn: TfrPrinter;
begin
if Assigned( GlobalPrn ) then
Exit( GlobalPrn );
GlobalPrn := TfrPrinter.Create;
try
GlobalPrn.Printer:=Printer;
except
on E: Exception do begin
debugln('lazreport: unit lr_prntr: ',E.Message);
end;
end;
Exit( GlobalPrn );
end;
Steps to reproduce:
Under Windows 10:
Create a simple library project MyLib.Dll, add a dependancy to LazReport Package.
Put "Lr_class" in your uses list.
Create a simpe host project with a one button, and put OnClickEvent
LibHandle := SafeLoadLibrary('MyLib.dll')
Run the host project, Press the button, it will freeze.
Additional information:
The fix is in attached .diff file.
Mantis conversion info:
- Mantis ID: 37065
- OS: Windows 10
- OS Build: Windows 10
- Build: trunk
- Platform: Windows
- Fixed in version: 2.1 (SVN)
- Fixed in revision: 63221 (#f1076190)
- Target version: 2.2