Solved

How to reference a Dll at runtime

Posted on 1998-12-01
13
436 Views
Last Modified: 2012-05-04
I'm working on a program in where I get to know the name of the Dll's I'll use at runtime, and of course I can't reference it using the reference menu in Vb.

If someone needs more details e-mail me al javierma@cic.usb.ve

0
Comment
Question by:javierma
  • 3
  • 3
  • 3
  • +3
13 Comments
 
LVL 3

Expert Comment

by:vbWhiz
ID: 1447730
Are these dll's interchangable - have the same functions etc that you will be using. It may be possible to accomplish this with COM & Type Libraries (.tlb).
0
 
LVL 1

Expert Comment

by:wizard2072098
ID: 1447731
Unfortunately, VB doesn't have a method for dynamically binding to a type library at run-time (at least not that I'm aware of). Alas, this would be nice, but for now you may have to relegate yourself to creating a generic interface to the DLLs for run-time binding.
0
 
LVL 4

Expert Comment

by:idcanada
ID: 1447732

Wizard2 is right,
you can not reference a DLLduring runtime.
In a compiled EXE, VB builds himself a list of available resources, which can be
seen in the object explorer.

You MUST add these references before you compile a project.
0
 
LVL 1

Author Comment

by:javierma
ID: 1447733
I´ll give more details on the use of the Dll so you can propose an alternate answer.
The Dll's identify a group of objects wich share some common characteristics. Each Dll has the information for creating some objects like apearance and behabior. The problem is that I want to distribute the final application with only some basic objects. If the user needs extra objects he buys them and just copy the Dll (wich contains the extra objects) in a directory so when the program starts it recognizes the added Dll.

I´ve seen other programs behave like that i.e. MathLab.

Since it´s becomming more dificult than I first thought I´ll increase 50 points to the question.
0
 
LVL 4

Expert Comment

by:idcanada
ID: 1447734

As far as VB goes, you simply can NOT do that.
I know it isn't a pleasant answer.

But I would suggest making a small DLL to demonstate
and if the user wants to purchase send him the new DLL
with the same Binary Comp.


0
 
LVL 1

Expert Comment

by:muffinthedog
ID: 1447735
Wizard2 is right: see http://premium.microsoft.com/msdn/library/books/techlang/hcvb/html/loadingmodule.htm. Could get around it by using a little C DLL function to dynamically bind at runtime (why not just use some form of licensing and distribute a single DLL)
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:javierma
ID: 1447736
I found a way but is ineficient. IF the Dll is in the registry of the system I CAN use it with the function CreateObject but Vb loads some error handlers and routines that slow down my app performance. This type of declaration is knokn as late binding. I was told that in Vb6 there wasn´t this slow down problem but i only have Vb5.
0
 
LVL 3

Expert Comment

by:vbWhiz
ID: 1447737
You can create all the dll's in advance and add them to your project references. Then you can write code that uses your objects with one precaution - you must check for the dll's existance before dimming any variables as a type that might not be included. For instance, if you plan on putting the dll's in the same directory as the exe then you could write code that looks like this:

Public Sub UseMyDogObject()

If filelen(App.path & "\Dog.dll") <> 0 then
   ReallyUseDogObject
Else
   MsgBox "You must purchase the dog object from me."
End IF

End Sub

Public Sub ReallyUseDogObject()
   Dim oDog As Dog
   Set oDog = New Dog
   MsgBox Dog.Bark
   MsgBox "Etc..."
   set oDog = Nothing
End Sub

[-Note- this will not compile correctly if the dll's do not exist but if you compile this and then remove some dll's it will just not use the objects from the dll]
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1447738
The fast solution is to define an interface that all the DLL should support. This interface is just a public class in what might be a seperate DLL.
Every DLL should now implement this interface using the keyword implements.

Implements IMyDLLInterface

In the calling exe you can now create the object using create object and assign it to an IMyDLLInterface object

Dim dllInstance as IMyDLLInstance
set  dllInstance  = CreateObject ("Proj.Class")

Now it is early binding again.
0
 
LVL 1

Author Comment

by:javierma
ID: 1447739
Hi Mirkwood, I already tried your aproach in Vb5 and this is what hapened: Vb ctreated the object and killed it before reference it to the variable (dllInstance in your example) but this is the closest I´ve been. I don't know if is the same situation in Vb6.

Is there any way to do it in C++ and try to use it in visual?.

vbwhiz, your solution is very good but I can´t plan the objects that are going to be added in the future. I was thinking of a special module in all the Dll that tell the program which object are available in the Dll. Both of you are very close.
0
 
LVL 3

Expert Comment

by:vbWhiz
ID: 1447740
javierma, how about having an intermediate dll that contains code like my dog example. This dll would have only one class that can then create instances of the other classes from the other dlls. You could then recompile a new version of this small intermediate dll every time you create some new objects and send it along with the new dll.
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1447741
We use it with VB5 and it works fine.
I would try it again if I were you because this definitelly works.
0
 
LVL 13

Accepted Solution

by:
Mirkwood earned 220 total points
ID: 1447742
Sometimes, you just know you have the right answer.

We use it with VB5 and it works fine.
I would try it again if I were you because this definitelly works.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now