[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

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?
0
vterekh
Asked:
vterekh
  • 2
1 Solution
 
MadshiCommented:
You have to add this line to your dll:

exports GetReportName;

You can delete the "export" calling convention. It has no effect in 32bit programming anymore.

Please add ShareMem at the first location of both your exe's and your dll's uses clause.

Regards, Madshi.
0
 
vterekhAuthor Commented:
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.
0
 
MadshiCommented:
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now