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?
Load DLL at runtime (dynamically)
Started by Void, Jun 14 2006 05:42 AM
5 replies to this topic
#1
Posted 14 June 2006 - 05:42 AM
Void
|
|
|
#2
Posted 14 June 2006 - 01:33 PM
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++
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.
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
Posted 16 June 2006 - 07:13 AM
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_*
Posted 16 June 2006 - 08:57 AM
Guest_Jordan_*
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
http://my.execpc.com...reflection.html
Also check out this article: http://www.csharphel...archive124.html
#5
Posted 27 June 2006 - 01:55 PM
Reflection? Never heard of it. Can anyone give an example?
#6
Posted 04 July 2006 - 05:21 AM
dirkfirst said:
Reflection? Never heard of it. Can anyone give an example?
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


Sign In
Create Account


Back to top









