Results 1 to 15 of 15

Thread: Loading a DLL dynamically

  1. #1

    Thread Starter
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    If you don't know the name of the function at runtime that you need then the dll is pretty much useless to you. Though there is a way to find out all the functions in a dll (load it up in a hex editor and look towards the end of the dll).

    If you want a plug-in type archetecture like netscape or ie, you need to design an interface (a blank class with all the functions you will need a dll to support compiled into a dll) that any new dll will use (implements <interface name>). This way, if you need the dll to run code that modifies something, you pass the data to a known function that is available to any dll you load.

    To load a dll that you don't know about until runtime, keep an ini file, a registry setting, or some other way to store the dll names in and do something along these lines:
    Code:
    Dim myDLL As Object
    
    Set myDLL = CreateObject(myDLLName & "." & myDLLObject)
    I'm fully unsure if this will work, but you can give it a try
    -Excalibur

  2. #2
    Member
    Join Date
    May 2001
    Location
    Adelaide, Australia
    Posts
    51
    Yes, I do know the name of the function at runtime, just not design time, and yes, it is for a plug-in like system. What would myDLLName be? The DLL filename w/o extension? What about paths? Can I include a path in that?

    I think I understand what you mean with the interface... there isn't a way of running a function w/o knowing it's name at design time, though?

    Also, when using myDLLName, would the DLL have to be registered with REGSVR32 before being referenced? If so, is there a way of doing this through API rather than a DOS command?

    Thanks for your help; I'll look further into the CreateObject command, too.
    Matthew Draper
    [email protected]

    "Genius may have its limitations, but stupidity is not thus handicapped." - Elbert Hubbard

    "I like long walks, especially when they are taken by people who annoy me." - Noel Coward

  3. #3

    Thread Starter
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    If you do not know the name of the function at design time, how can you use it?

    the DLLName is actually the application name (not the file name) of the .dll you're grabbing the object (class) from. For instance:
    Code:
    Set F = CreateObject("Scripting.FileSystemObject")
    Set F = CreateObject("Excel.Sheet1")
    it's the internal application name of the dll.

    I believe it must be registered, yes.

    You can use the Shell command to run regsvr32 to register the dll. I'm unsure if there is a way via API, there probably is, but I don't know what it is.
    -Excalibur

  4. #4
    Member
    Join Date
    May 2001
    Location
    Adelaide, Australia
    Posts
    51
    I'm constructing a scripting language of sorts.. and I want to be able to "plug-in" extra commands (from DLLs) at runtime. The function name is unique for each command, ideally, so you can't actually know what they are at design time.

    In C/C++, you would do this using something to do with function pointers, probably.

    How do I set the application name for a DLL I create, do you know?

    Thanks again for all your help
    Matthew Draper
    [email protected]

    "Genius may have its limitations, but stupidity is not thus handicapped." - Elbert Hubbard

    "I like long walks, especially when they are taken by people who annoy me." - Noel Coward

  5. #5

    Thread Starter
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Well, an idea would be to do this:

    Within your command, add the following code:
    Code:
    Public Function Do_Command()
    End Function
    Or something very similar to this. Make the project name equal to your command name. Then all you have to do is run that function whenever you need to execute your command.
    This way, you know the function name right off the bat, no matter what command is added, and if you make it an interface that each command implements, you are sure to have the correct function each time. It won't matter what code is in that function in your command, just that the function exists is enough. That's the cool thing about OOP
    -Excalibur

  6. #6
    Member
    Join Date
    May 2001
    Location
    Adelaide, Australia
    Posts
    51
    So... is the "internal application name" the project name, or some well-hidden project setting?

    Thanks (yet again ) for your help... I'll have to try this later today, when I can be bothered creating & compiling some test projects
    Matthew Draper
    [email protected]

    "Genius may have its limitations, but stupidity is not thus handicapped." - Elbert Hubbard

    "I like long walks, especially when they are taken by people who annoy me." - Noel Coward

  7. #7

    Thread Starter
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    it is the project name (or possibly the application name setting in compile/options).
    -Excalibur

  8. #8
    Member
    Join Date
    May 2001
    Location
    Adelaide, Australia
    Posts
    51
    Okay, thanks
    Matthew Draper
    [email protected]

    "Genius may have its limitations, but stupidity is not thus handicapped." - Elbert Hubbard

    "I like long walks, especially when they are taken by people who annoy me." - Noel Coward

  9. #9
    jim mcnamara
    Guest
    For what it's worth -

    MicroBasic and I had a go around on whether or not you can Enum methods and properties in an unknown COFF object file. I found documentation about code that does this for the Object Browser, and it isn't just part of VB, it's part of SCM, and other OS components. I believe it's one of those deliberately obfuscated features that is a major security loophole.

  10. #10
    Member
    Join Date
    May 2001
    Location
    Adelaide, Australia
    Posts
    51
    Sounds interesting, Jim. Could you give me a few more details?
    Matthew Draper
    [email protected]

    "Genius may have its limitations, but stupidity is not thus handicapped." - Elbert Hubbard

    "I like long walks, especially when they are taken by people who annoy me." - Noel Coward

  11. #11
    Member
    Join Date
    May 2001
    Location
    Adelaide, Australia
    Posts
    51
    I can't get this to compile, let alone actually communicate

    Can you see any probs in this code (attached) ?
    Attached Files Attached Files
    Matthew Draper
    [email protected]

    "Genius may have its limitations, but stupidity is not thus handicapped." - Elbert Hubbard

    "I like long walks, especially when they are taken by people who annoy me." - Noel Coward

  12. #12
    gaffa
    Guest
    Assuming you are attempting to plug-in COM (ActiveX) DLL's, you can use the TypeLibInfo library to query the interface at runtime and find out what methods/properties etc it supports.

    Are you writing a plugin system for plugins that support a standard interface (so it would be early-binding), or a generic plugin system for any interface (late-binding)?

    I've posted a couple of things on this site before about creating plug-in systems, but I haven't got the links handy. They do come up in a search though (under "plugin" I think)

    The CreateObject uses the ProgrammaticID of the control, which is the name of the project (ie: the project name in the properties window in VB), followed by the name of the class that is being created.

    Alternatively, you can use the GUID, and extract the ProgID from the GUID using the TypeLibInfo library, or even better, load the file by file name, register the file using the API and then extract the ProgID from that file (which is handy if you want to have a plug-in directory where all you plugins go, without necessarily having to register those DLL's before the application loads.)

    The attached project demostrates a basic plugin system (it just loads language DLLs from a particular directory at runtime). This is the original post where there is a better run down of how it works: http://forums.vb-world.com/showthrea...threadid=53873

    - gaffa
    Attached Files Attached Files

  13. #13
    Member
    Join Date
    May 2001
    Location
    Adelaide, Australia
    Posts
    51
    Thanks gaffa, that app is a real help .

    Now I just have to work out if the DLLs can also contain a reference to an Interface, which is passed to it's function from the app, and this interface would supply various I/O functions for the DLL. Do you know if this is / is not possible?

    I'll try later today, and will probably post whether or not I've been sucessful.

    Thanks again
    Matthew Draper
    [email protected]

    "Genius may have its limitations, but stupidity is not thus handicapped." - Elbert Hubbard

    "I like long walks, especially when they are taken by people who annoy me." - Noel Coward

  14. #14
    Member
    Join Date
    May 2001
    Location
    Adelaide, Australia
    Posts
    51
    Yes! I got it working to do a callback to the main application, and I've now come to the conslusion that this is *perfect* for what I want.

    Code:
        For Each Day In Eternity
            Call BeGratefulTo("gaffa")
        Next Day
    Code:
        Dim intCount As Integer
        For intCount = 0 To (1 / 0) ' Infinity
            Call SendMessageTo("gaffa", "Thank you!")
        Next intCount
    And all similar code snippets, as appropriate
    Last edited by Matthew Draper; May 20th, 2001 at 06:02 AM.
    Matthew Draper
    [email protected]

    "Genius may have its limitations, but stupidity is not thus handicapped." - Elbert Hubbard

    "I like long walks, especially when they are taken by people who annoy me." - Noel Coward

  15. #15
    gaffa
    Guest
    Glad to be of help.

    That sample isn't fully featured though. I think there is a bug when registering the control - if I recall I put some REALLY bad code in there in the form of error handlers to get round a couple of problems.

    But apart from that, it seems to work OK. On the link I posted to the original post where that code was avaialbe, there was another "bit" to it - I think it was a single DLL exposing two languages - Australian English and US English. There's not a lot of difference, just a loop through the interfaces collection.

    Cheers

    - gaffa

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width