Solved

DLL Question

Posted on 1998-05-28
7
210 Views
Last Modified: 2012-05-04
Hi All,

Can someone give me a hand here?  I am writing a program which is, in its basic form, is a very simple front end.  What I want to do is for it to detect when a DLL has been placed into the same directory and add it too a drop down menu.

I have added all the functions declared within the DLL(s) into the main program.  I have also made the menu items invisible which call the DLL once it is found (when the DLL is detected, its menu item is turned on and vise versa).

In my FormCreate method I have done a:

TipOfTheDay.Visible := FileExists('TOTD.DLL') ;
(One example) to enable or disable that menu item when ever that DLL is detected.

However, when I compile the program, it tells me that the DLL cant be found - which is true - I havent copied it into to the correct location yet.

I copied the DLL into the correct directory, and it compiles and runs perfectly.  However, if I run the program again without the DLL, I get a message "a required DLL file,  TOTD.DLL, was not found" error.

Can some one tell me how I can let the program run without these DLLs being present, and detect when the DLLs are present???

Thanks for your help.


Stuart Johnson.
0
Comment
Question by:Stuart_Johnson
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
ahalya earned 100 total points
ID: 1349010
you are getting this problem becasue you are using what is called a "static loading". This happens when you define your functions from your DLL  as external functions. In this case the references are resolved during compilation.

You should go for dynamic loading.  Use the "LoadLibrary" and "GetProcAddress"  functions to dynamically load the DLL functions. (you don't have to declare the "external" functions sepereately in this case). i can give you an example if you like.
0
 
LVL 7

Expert Comment

by:ahalya
ID: 1349011
Here goes a simple example that runs the function called "ProcName" from the DLL called "DLLName".



function BoolDLLFunc(DLLName, ProcName:pchar):boolean;

var  h : THandle;
     f : function:boolean;stdcall;

begin
Result := false;
h := LoadLibrary(DLLName); // Get the handle of the .DLL to be loaded...
if h <> 0 then
try
   @f := GetProcAddress(h, ProcName); // Load the procedure from the .DLL...
   if @f <> nil then
      begin;
      Result := f;      //  Execute the function
      end;
finally
   FreeLibrary(h);      // Free the library...
   end;
end;


0
 
LVL 4

Expert Comment

by:d003303
ID: 1349012
Hi Stuart,
what's the purpose of the DLL ? Do you want to create sth like an extendable front end ? If so, maybe you should define a plug-in interface for yout DLLs, that would make it much easier.

Slash/d003303
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 1349013
Ahalya.
I'll give this a go and see how it works.  Looks fine.

Slash.
What do you mean by a "plug in interface".  This sounds like what I am trying to achieve.  I want the program to be expandable, say like Adobe Photo Shop is with its plug ins.

Stu.
0
 
LVL 4

Expert Comment

by:d003303
ID: 1349014
Yo,
a plug-in interface is a set of defined function entries in a DLL. Because you are the designer of the interface, you would have to think wich functions you need. The hosting program normally parses a specified directory for files, loads them with LoadLibrary and looks if the function entries exist. If yes, the plug-in is active and bound. If no, FreeLibrary is called immediately. The basic work is done by ahalya's code.
You could easily create a plug-in object wrapper that does this stuff.

Slash/d003303
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 1349015
I have got it going as per your instructions.  Works like a charm.  Thank you very much.

Stuart.
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 1349016
I have got it going as per your instructions.  Works like a charm.  Thank you very much.

Stuart.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

773 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