PDA

Click to See Complete Forum and Search --> : Dynamically Reference Dll


Asterisk
May 2nd, 2000, 12:26 AM
Is there any way to dynamically set a reference to a dll using vb code alone. The problem i have is that i want to write a prog that will interact will an variable amount of dll's, and so i want to set up the references dynamically depending on which dll's are going to be used

Any help will be greatly appreciated

Thanks

Sam Finch
May 2nd, 2000, 12:48 AM
It's quite Hard, it depends on the interfaces of the Dlls, If you can somehow control the interface to something that takes four long integers by value, Remember anything by reference is the same as passing it's pointer by value.

If so you can use the LoadLibrary API to get the dll into memory, GetProcAddress gets the address of a function (hModule should be the return value of loadlibrary), FreeLibrary releases the memory.

CallWindowProc can call a function by it's address as long as it has the set four long integer interface, the first parameter is the address of the function.

If you can't predict the interface then you're in trouble, If you can limit it to a few interfaces then you can set up a class module with a method for each interface you need to use, you can then modify the classes vtable and call the classes methods to call you're functions.

There's an article on vTable Modification at http://www.bstorage.com/nervenet/VTableModification.asp, but it's not easy.

Clunietp
May 2nd, 2000, 11:31 PM
just use late binding. You can specify the object at runtime

Asterisk
May 3rd, 2000, 12:47 AM
How does late binding work?

Clunietp
May 3rd, 2000, 12:57 PM
Declare your variable as Object (as opposed to ADODB.Recordset, or some other specific object)

When you want to create a certain object, just put in a string variable

dim objMyObject as object
set objMyObject = createobject("ADODB.Recordset")

or you could use a string variable in place of a constant

The performance is much worse than explicitly declaring your objects, but you have the benefit of flexibility. You do not, however, have an easy way to tell what methods that the object supports. If you build your objects with a standard interface (set of properties and methods) you can interchange them at runtime easily. There was an article a few months back in VBPJ that discussed these concepts....

HTH

Tom