הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Tuesday, August 15, 2006 7:03 AM
Is there a way to Dynamically load a DLL in VB.net? Dunno where else to ask this question, so decided to post it here...tq
All replies (8)
Wednesday, December 12, 2012 10:23 PM ✅Answered | 1 vote
this post is unanswered since 2006, better late than never:
http://checktechno.blogspot.com/2012/12/how-could-you-dynamically-load-your-own.html
Dim oType As System.Type
Dim oAssembly As System.Reflection.Assembly
Dim oObject As System.Object
Assembly = Assembly.LoadFrom("C:\ClassLibrary1.dll")
oType = oAssembly.GetType("ClassLibrary1.SayHello")
oObject = Activator.CreateInstance(oType)
Tuesday, August 15, 2006 9:19 AM
Dynamically load? Why? What are you trying to achieve?
Tuesday, August 15, 2006 9:26 AM
I'm building 2 version of the same application but I wish to maintain one set of code/project. Version 1 is for normal Pocket PC, Windows CE devices without barcode feature while Version 2 is for Symbol devices with barcode feature. For Symbol version, I need to load Symbol's libraries.
What I'm trying to do here is determine the type of device during runtime, then decide whether to load the Symbol libraries or not.
You may ask why not just load the libraries regardless of the type of device. I have tried doing that but there's error when I tried loading them for devices other than Symbol.
Tuesday, August 15, 2006 9:50 AM
Please someone correct me, but I think this is not possible. I don't think you can reference a library at runtime as it needs to be compiled as part of the project.
Tuesday, August 15, 2006 12:06 PM
You will have to use reflection. You can do something like:
Dim a as System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom("thisassembly.dll")
(It's also possible to store the DLL as a resource, so it doesn't need to be a physical file). Reflection can access pretty much everything in the DLL, without the need to reference the assembly. However, just like late binding, making calls on the DLL can be problematic, and require significant error checking.
Tuesday, August 15, 2006 12:49 PM | 1 vote
In addition to Assembly.Load and Assembly.LoadFrom, you could consider Assembly.LoadFile or AppDomain.CurrentDomain.CreateInstanceAndUnwrap. CreateObject is still there as is the ability to Declare an API call. FWIW, the code snippet that comes with VB2005 generates the following:
loadedAssembly = Assembly.LoadFile("C:\Folder\AssemblyName.dll")
Jim Wooley
http://devauthority.com/blogs/jwooley
Wednesday, August 16, 2006 12:21 AM
.NET assemblies are typically loaded on demand, meaning that the runtime shouldn't even try to load you dll if you don't call into it. You make sure that all calls to the Symbol library are restricted to a code path that is only executed after the type of device is validated. Another strategy would be to not directly reference you Symbol library but rather pull out the Symbol using code into an intermediary library, that way there will be no direct dependency on the library. What is the error that you get when loading the library anyway? May we see the code you're using to detect the device type and call into the library appropriately?
Wednesday, August 16, 2006 5:17 AM
Actually, I have yet to write the code. After I got the error
" Failed copying C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\wce400\X86\symbol.all.x86.cab to the device"
while testing the app on Windows CE Emulator, I had stop coding with the assumption of the Symbol libraries would not load to devices other than Symbol devices.