[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

DLL Question

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
Stuart_Johnson
Asked:
Stuart_Johnson
  • 3
  • 2
  • 2
1 Solution
 
ahalyaCommented:
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
 
ahalyaCommented:
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
 
d003303Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Stuart_JohnsonAuthor Commented:
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
 
d003303Commented:
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
 
Stuart_JohnsonAuthor Commented:
I have got it going as per your instructions.  Works like a charm.  Thank you very much.

Stuart.
0
 
Stuart_JohnsonAuthor Commented:
I have got it going as per your instructions.  Works like a charm.  Thank you very much.

Stuart.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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