Solved

Static DLL

Posted on 2002-04-15
7
204 Views
Last Modified: 2010-04-04
I want to know if I can specify the path of a static DLL at runtime..something like..

Function Test; external TestPath+'TEST.DLL';

but as is TestPath must be a constant...any ideas?
0
Comment
Question by:laserpro
  • 4
  • 3
7 Comments
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6943459
AFAIK you can't.  You will have to load the DLL using LoadLibrary instead.

If you haven't used LoadLibrary before, let me know and I'll post you a detailed example.

Cheers,

Stu
0
 

Author Comment

by:laserpro
ID: 6943497
so if the DLL is not in the path you have to resort to dynamic DLL loading?
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6943505
That's what my understanding of it.  Are you comfortable with dynamic loading?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:laserpro
ID: 6943528
no..I get the jest of it but only did it one a long time ago...was hoping it would not resort to that.  A nice short, simple example would be helpful...as you know I dont need many bells and whisples for what Im doing.
0
 
LVL 6

Accepted Solution

by:
Stuart_Johnson earned 50 total points
ID: 6943550
OK.  This is an short example for you.

Within the library:

function MyFunction(ID: Integer): boolean; export;
begin
  Result := ID > 5;
end;

exports
  MyFunction;

end.


Within your project:

procedure CheckID(ID: Integer);
const
  LibPath = 'C:\DLLS\TEST.DLL';

var
  MyFunc: Function(ID: Integer): boolean;
  Hnd: Integer;
begin
  Hnd := LoadLibrary(pChar(LibPath));
  if Hnd > 0 then
    try
      MyFunc := GetProcAddres(Hnd, 'MyFunc');
      try
        if MyFunc(ID) then
          ShowMessage('Yes, the ID is greater than 5')
        else
          ShowMessage('No, the ID is less than 5');
      except
        ShowMessage('Can not execute the function within the DLL');
      end;
    finally
      FreeLibrary(Hnd);
    end
  else
    ShowMessage('Library not found');
end;


This code has come COMPLETELY from memory, so if you have any problems at all, let me know and I'll actually do it in Delphi and give you any changes.  At this stage I think it should be fine.

Cheers,

Stu.
0
 

Author Comment

by:laserpro
ID: 6943564
thanks for your help.
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6943576
It's a litle more work to do dynamic linking, but one of the great things about it is you can check to make sure the DLL actually exists before you use it.  Using static linking means if the DLL doesn't exist, you're application will throw up really ugly messages (and could infact terminate ungracefully).

Glad to have helped you out.  Please ask for more help if you need it.

Cheers,

Stu.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to fill array with TArray.Create? 14 95
Delphi: ForceDirectory plain function 7 42
PHP preg_replace code convert to Delphi 14 66
DBCtrlGrid, Delphi, Scroll 8 38
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question