Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi there...

I have a vb.net project referencing a DLL file, let us call it "mylib.DLL". Assume I have a code that works like this:

VB
A=ReadUserInput()
If A=0 Then
   Dim Result=CallMyDLLAndGetResult ' This is a call to a function inside mylib.DLL
   UpdateCalculation
End If

The project obviously referenced mylib.dll, and it works perfectly fine.

Now to the problem:

If I copy the project to another machine, without the dll, and run the code, and A is never zero, the program tries to load "mylib.DLL" even though it does not need it.

The question is: How can I prevent a DLL load until i need to run code inside it? Is there a directive that allows me to do so?

Yours sincerely
Posted
Comments
[no name] 27-Sep-13 12:27pm    
Is there some reason that you could not just dynamically load this DLL just prior to your code using it?
mkaatr2 27-Sep-13 12:37pm    
If I want to do so, I will have to modify 4 different projects in the solution, and the work could take over a week.

Formally, it sounds like a nonsense. There is no such concept as "call to a DLL". And, if you have a call to a DLL which is not loaded, this is not, by definition, a call.

However, something close to it may have real practical sense. For simplicity, let's assume that this is not just a DLL, but is a main module of some .NET assembly. If this is a native DLL, this is also possible, but as you did not mention what it is, let me do the assumption which makes the speculations easier. (We can later discuss native DLLs if on your request, if you are interested.)

So, you can have some "plug-in" interface which may or may not be implemented in some assembly, which is not yest loaded. But the interface is known by the implementing assembly and the host assembly (otherwise, how would you work with plug-ins?). Now, you can defer loading of the assembly before the call. To do that, add yet another implementation of the interface, in the host assembly.

Let's say:
C#
public interface IPlugin {
    void UpdateCalculations();
    void SomeOtherMethod();
    //...
}


On the host application:
C#
class PluginWrapper : IPlugin {

    IPlugin PluginImplementation;
    string pluginFile; // how you get it is up to you

    void IPlugin.UpdateCalculations() {
        if (PluginImplementation == null)
            PluginImplementation.LoadAndGetImplementation(pluginFile);
        PluginImplementation.UpdateCalculations();
    } //IPlugin.UpdateCalculations

    void IPlugin.SomeOtherMethod() {
        if (PluginImplementation == null)
            PluginImplementation.LoadAndGetImplementation(pluginFile);
        PluginImplementation.SomeOtherMethod();
    } //IPlugin.SomeOtherMethod

    // implementation of all other IPlugin members...

    IPlugin LoadAndGetImplementation(string pluginFile) {
        System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(pluginFile);
        // using assembly, get types or use some other way to find implementing type
        // check up that IPlugin is really implemented
        // instantiate the type through reflection
        // cast the obtained instance of the implementing type to IPlugin
        // return the result
    } //LoadAndGetImplementation

    //...

} //class PluginWrapper


Sorry for writing it in C# — I only want to conduct the idea, and you should understand at least some C#, is you use .NET.
Also, I did not give you much detail on reflection operations as I'm not sure you want to work with .NET assemblies. If you clarify on what you want to do and ask me further questions, I'll answer.
If you want to load native (unmanaged) DLLs, you can do something similar, but then host-side "PluginWrapper" would have somewhat different interface than native DLL (which won't really export classes). You just wrap all DLL calls the way convenient for you.

—SA
 
Share this answer
 
v5
Comments
Ron Beyer 27-Sep-13 23:23pm    
+5'd... On a side note, I've looked at a number of plugin frameworks recently for a project I'm working on. After going through MEF and seeing how convoluted it was, I finally settled on Mono.Addins and after doing a lot of work in SharpDevelop extensions I really like how its all set up (SharpDevelop requires complicated Xml files with things called "Doozers", programming is hard enough without making up words for programming constructs).
Sergey Alexandrovich Kryukov 27-Sep-13 23:39pm    
Thank you, Ron.
I use my own very simple plug-in system with a shortcut for finding a plug-in interface in a loaded assembly: it can apply an assembly-level attribute which claims that certain type(s) implement certain interface(s). As the type of an attribute parameter can be System.Type, nothing depends on any strings, which is the big main problem of many plug-in systems. Any misspelling in names cannot be detected by the compiler.
—SA
pasztorpisti 28-Sep-13 17:25pm    
+5, I always advise everyone to write only a single exported function into a DLL that returns a pointer to an interface or a descriptor (like a struct containing func ptrs in C). I also have a tip about this: Designing the interface of DLLs in C/C++ projects. This has so many advantages, for example implementing platform (in)dependent (delay) loading (windows, unix, ...) or implementing the interface many times built into the program itself (like in case of default/builtin plugins). Changing between builtin or external shared object implementation is usually a breeze. Some fight against this point for some reason...
Sergey Alexandrovich Kryukov 29-Sep-13 1:20am    
Long time ago, well before .NET, I developed the architecture based on similar approach and COM interfaces for C++ and Delphi, which allowed us to develop inter-language and cross-platform code (this way, COM-like interfaces developed via IDL worked on Unix, too)...
Thank you.
—SA
You may use reflection to dynamic load your dll:
Reflection in .NET[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Sep-13 14:13pm    
And?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900