如何防止DLL加载直到第一次调用它? [英] How to prevent a DLL load until the first call to it?

查看:62
本文介绍了如何防止DLL加载直到第一次调用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好......



我有一个引用DLL文件的vb.net项目,我们称之为mylib.DLL。假设我的代码如下:



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:

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



该项目显然引用了mylib.dll,它完美无缺。



现在出现问题:



如果我将项目复制到另一台没有dll的机器上,并运行代码,A永远不为零,程序尝试加载mylib.DLL,即使它不需要它。



问题是:如何我可以阻止DLL加载,直到我需要在其中运行代码?是否有允许我这样做的指令?



您诚挚的


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

推荐答案

正式,它听起来像胡说八道。没有调用DLL这样的概念。并且,如果你有一个没有加载的DLL的调用,根据定义,这不是一个调用。



然而,接近它的东西可能是真的实际意义。为简单起见,我们假设这不仅仅是一个DLL,而是一些.NET程序集的主要模块。如果这是一个本机DLL,这也是可能的,但是由于你没有提到它是什么,让我做一些使推测更容易的假设。 (如果您有兴趣,我们以后可以讨论本机DLL。)



所以,你可以有一些插件界面,可能会或可能不能在某些程序集中实现,而这些程序集并未加载。但实现程序集和主机程序集已知接口(否则,您将如何使用插件?)。现在,您可以在调用之前推迟加载程序集。为此,在主机程序集中添加另一个接口实现。



假设:

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:
public interface IPlugin {
    void UpdateCalculations();
    void SomeOtherMethod();
    //...
}





在主机应用程序上:



On the host application:

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





抱歉wri在C#中使用它 - 我只想表达这个想法,你应该至少理解一些C#,你是否使用.NET。

另外,我没有给你很多关于反射操作的细节,因为我我不确定你是否想使用.NET程序集。如果你澄清你想做什么并问我进一步的问题,我会回答。

如果你想加载本机(非托管)DLL,你可以做类似的事情,但然后主机端PluginWrapper会有一些不同于本机DLL的界面(它不会真正导出类)。你只需要方便地包装所有DLL调用。



-SA


你可以使用反射来动态加载你的dll:

.NET中的反思 [ ^ ]
You may use reflection to dynamic load your dll:
Reflection in .NET[^]


这篇关于如何防止DLL加载直到第一次调用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆