Jump to content

Load DLL at runtime (dynamically)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Void

Void

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 411 posts
I would like to load a DLL at runtime in C# or C++ .NET 2.0, such as the microsoft office PIAs, and if they do not have the DLL to set a variable.

I figure the only way I can "catch" if they do not have the PIAs is by using a try/catch system? How do I even load DLLs at runtime?
Void

#2
RobSoftware

RobSoftware

    Programmer

  • Members
  • PipPipPipPip
  • 143 posts
I'm not sure how you load it by run time. Here is how I do it.

I add it as a resource to the project. Then I do exactly what you said above, a try/catch statement such as (using the word PIA):

This is the code in C++


try {

    Word::ApplicationClass^ myWordApp = gcnew Word::ApplicationClass();

    hasWordPIA = true; // bool declared elsewhere

}

catch (System::Exception^) { // The user dos not have the PIA

    hasWordPIA = false;

}


I have this set as a function and run the function when I need to test whether or not they can use the PIA. I'm not sure this is the correct method but it works.

#3
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts
You'd use reflection to load the DLL at runtime. You can google for reflection and should get a few hits showing you code samples, or let me know and I'll drum something up for you in C#.

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Did a search for C# Reflection and the first result seemed to be a good one:

http://my.execpc.com...reflection.html

Also check out this article: http://www.csharphel...archive124.html

#5
dirkfirst

dirkfirst

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 354 posts
Reflection? Never heard of it. Can anyone give an example?

#6
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts

dirkfirst said:

Reflection? Never heard of it. Can anyone give an example?
Here's a snippet that should do the trick...it's in VB.NET, but it shouldn't be too diffcult to mentally translate to C#:

If you know the class name, DLL, and constructor args (if any) you can create an instance and cast it to a known interface. assembly.dll is the dll the class is defined in, Namespace.Class is the full name of the class, and IInterface is the known interface to cast to. Note that there's different overloads on Assembly.CreateInstance for default constructors versus parameterized ones.

'****Load an assembly when you know the class name

Dim asm As Reflection.Assembly 

asm = Reflection.Assembly.LoadFrom("assembly.dll")

' Declare a variable to hold the returned object

Dim obj as Object

' Variable to cast the object to a known interface

Dim iObj As IInterface


' For a default constructor - just create

obj = asm.CreateInstance("Namespace.Class", True)


' For a parameterized constructor - create parameters as an object array

Dim params(0) As Object

params(0) = "Parameter1"

' Create object and cast to interface

obj = asm.CreateInstance("Namespace.Class", _

   True, _

   Reflection.BindingFlags.Default, _

   Nothing, _

   params, _

   Nothing, _

   Nothing)


iObj = CType(obj, IInterface)


If you want to search out a type that implements a known interface, you can do it like this. One way to use this for plugins is to iterate through all DLLs in a Plugin directory and look for implementors of IPlugin. Once found, you could save the full classname to a config file and load the known plugins using the first method to save the performance hit of iterating all types on startup.

' ***** Look at each type in an assembly to see if it implements an interface

Dim asm As Reflection.Assembly

asm = Reflection.Assembly.LoadFrom("assembly.dll")

For Each typeAsm As Type In asm.GetTypes

    If Not typeAsm.GetInterface(GetType(IInterface).FullName) Is Nothing Then

	' Type typeAsm.Fullname in asm.Fullname supports the interface

               Dim obj as Object, iObj as IInterface

              obj = asm.CreateInstance(typeAsm.FullName, True)

              Return Ctype(obj, iObj)

    End If

Next