Adding references (dll) through code (at app runtime, of cource)

Hi! I am having this question:
I am designing a application, in which, you could open a DLL file, using the OpenFileDialog,

in the dll, there will be a class for example, Main.
Now, These classes would be all different (much like plug-ins in any plug-in supported apps)
So, When the user click 'Open' the application should add the new reference, through VB code, and load it's data in to the viewing application.
Here is, for example, code of a DLL
Sub Main()
Dim map_author As String = "Somebody"
Dim author_email As String = "somebody@example.com"
End Sub

Ok, so that is the dll.
Well, in the application, that will 'read' the dll, it should read it and assign the labels in application, with data from dll:
In application:
Label1 is author's name
Label2 is author's email

So, it could be:
Label1.Text = MyDll.map_author

So, how do you add references at runtime of the application
Thx!



Answer this question

Adding references (dll) through code (at app runtime, of cource)

  • Gunjan Moghe

    What exception did it throw
    Possible issues would be not having the right path to the assembly (try using system.io.file.exists to see if the file is where you expect it to be), or not specifying the full name of the type you want to create.

    I don't know too much about loading a data source at runtime, I'm afraid - what I was suggesting was having a simple text file with whatever data you're trying to read. Why do you need to load this at runtime



  • cheesesarnie

    Ok, the program is like a train dispatcher, so it will have maps for different routes. So, for ea DLL, it will have it's own map, and buttons, and thing like that.
    (PS: The app is for playing multiplayer games in Microsoft Train Simulator)

    Anyway, at first, I tried to use config.ini files, and have a Shockwave Flash Player control on the form, (for the track and signal layout) and that quite did not work, mainly because I am not familiar with ActionScript, but I am familiar with Flash 8.

    So, I want to try the DLL data.

    The exception it threw was
    Object variable or With block variable not set.


  • Kuku

    It did not work, it kept throwing exception....

    Ok, if I wanted, instead of reference, create a data source, also a DLL,
    how would I connect it and get it's data and load it into the app

    PS: I just need to load one DLL at once.


  • adavidson

    OK, Thank you! it seems to be working, I just got to get this straight


  • somewww

    The issue is probably that either the location of the dll isn't correct, or the full name of the type isn't correct - the code should work otherwise - check where you're getting the exception.

    In any case, what kind of data will you have in those files

    It seems that you want generic data stored in the files - if you're creating them, the best solution is probably to use serialization.

    Module Module1

    Sub Main()
    Dim c As New TrainMap
    c.Name =
    "testing"
    SaveMap("testfile.bin", c)
    Dim d As TrainMap = LoadMap("testfile.bin")
    Console.WriteLine(d.Name)
    End Sub

    <Serializable()> Class TrainMap
    Public Name As String
    <Serializable()> Class Connection
    ' etc
    End Class
    Public Connections(10) As Connection
    End Class

    Sub SaveMap(ByVal file As String, ByVal map As TrainMap)
    Dim fs As System.IO.FileStream = IO.File.OpenWrite(file)
    Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
    formatter.Serialize(fs, map)
    fs.Close()
    End Sub

    Function LoadMap(ByVal File As String) As TrainMap
    Dim fs As System.IO.FileStream = IO.File.OpenRead(File)
    Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
    Dim ret As TrainMap = formatter.Deserialize(fs)
    fs.Close()
    Return ret
    End Function

    End Module

    This way, as long as the object you're saving is serializable (consists of native types, and array/collections/classes of said types) you can write them and read them very easily to files in binary (or text) format.



  • Sean Vikoren

    Before I begin, if all you're doing is reading fixed information, a simple data file is probably a better solution - loading a dll makes sense if there actually is code you want to execute.

    That said, assuming you know of a dll named mydll.dll, that has a class myClass that has a method named "runme"

    Dim myassembly As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile("Mydll.dll")
    Dim
    objclass As Object = myassembly.CreateInstance("myClass", True)
    'you can now use late binding to call methods or check properties in the class
    objclass.runme()

    If you don't want to use late binding, you can use reflection to call runme. I'll leave that as an exercise, though :)

     

    Edit: a couple of comments - note that you won't be able to declare elements from the class you loaded with the correct type - that is because at the point where you're writing your code, the compiler doesn't even know those types exist, or if the dll you're loading will even be there when you run the program (it's a good idea to check if "mydll.dll" is there, and if myassembly actually contains something after you try to load it). You'll have to use object, and either use reflection calls or late binding (option strict off).

    Also, you're treading into advanced topics here :) - but you may want to read on app domains. Right now, once you load that assembly, it will stay in memory for the duration of the program. That may or may not be an issue. AppDomains allow, amongst other things, to be unloaded from memory, along with any assemblies they loaded.



  • Adding references (dll) through code (at app runtime, of cource)