Link to home
Start Free TrialLog in
Avatar of vterekh
vterekh

asked on

cyclical dll loading

Hi
I have some DLLs like:
------------------------------------------------
library Report;
uses
  SysUtils,
  Classes,
  Main in 'Main.pas';
exports
  GetReportName name 'GetReportName';
end.

unit Main;
interface

uses Forms, Classes;
function GetReportName: String; export; stdcall;

implementation

const
  ReportName = 'Name1';

function GetReportName;
begin
  Result := ReportName;
end;

end.
------------------------------------------------


And EXE app. It looks through the given directory, finds all DLLs that have the structure described above (with the different constant "ReportName") and for instance prints the Name of the report in the DLL:

------------------------------------------------
type
  TGetReportName = function: String; stdcall;
var
  DllInstance: THandle;
  GetReportName: TGetReportName;
  ReportName: String;

. . . . .  

if ExtractFileExt(SearchRec.Name) = '.dll' then
begin
 DllInstance := LoadLibrary(PChar(SearchRec.Name));
 if DllInstance <> 0 then
 begin
  @GetReportName := GetProcAddress(DllInstance, 'GetReportName');
  if @GetReportName <> nil then
  begin
   ReportName := GetReportName;

   ShowMessage(ReportName);
  end;
  FreeLibrary(DllInstance);
 end;
end;

After finding the first DLL it's OK, but in the second iteration an exception is generated in the string:

ReportName := GetReportName;

ReportName variable unaccessible :(

What's wrong in my code?
ASKER CERTIFIED SOLUTION
Avatar of Madshi
Madshi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vterekh
vterekh

ASKER

2 Madshi:

Thanks a lot!
Adding ShareMem it started working.

But as for the string

exports GetReportName;

it was located in the

library Report;

Probably you didn't notice it.
Yep, missed that GetReportName...  :-)

About ShareMem: When using ShareMem you have to distribute Borland's memory manager dll with your application. It's named borlndmm.dll, if I remember right. You're allowed to do so...

Regards, Madshi.