加载时执行DLL [英] Dll execution on load

查看:64
本文介绍了加载时执行DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对任何人来说都是美好的时光!
在大学里学习面向对象的程序设计时,我们遇到了一个需要创建dll的任务,以便部分代码在加载时完全执行.我的意思是类似C ++中的DllMain.我试图上网,但只找到了创建静态构造函数的建议,它将在第一次创建对象时初始化我们需要的所有内容,但是没有,因为主要的要求是,NOTHING可以用原始的形式重写代码,仅允许使用使用DllName"行.因此,问题是:是否有可能使Dll完全在将其加载到项目中时执行某些操作?
非常感谢您,希望有人会帮助我.

Good time of day to anyone!
While learning object oriented programming in university we got a task that requires creating dll so that some part of code is executed exactly on load,I mean something like DllMain in c++. I tried to surf the internet but managed to find only an advice to create static constructor,that will initialize all we need on first creation of object,BUT it dosen''t, as the main requirement is that NOTHING can be rewritten in the original code,only line "using DllName" is allowed. So the question is: Is there any possibility to make Dll execute something exactly on it''s load into project?
Thank you very much,hope someone will help me.

推荐答案

...已编辑,使用短语技巧"消除了我的原始回答可能带来的负面影响问题" ...

即使我认为此处提出的问题是棘手的"问题,也没有明确的解决方案……至少在.NET WinForms中没有,但我还是不得不澄清我先前的评论,目的是避免造成任何不必要的混乱,并有可能清除这里形成的奇怪的形而上学烟雾,也许是来自古老瑜伽士营火的烟雾,这是阿德瓦塔·韦丹塔(Advaita Vedanta)的"neti neti"学校的从业者,忠于Avadhuta Gita :)

的确如此,正如MSDN所说:用户无法控制程序中何时执行静态构造函数",程序员,您可以 force 调用静态类的构造函数.

例如,假设我创建一个类型为类库"的.NET项目,该项目具有一个静态类:
... edited to remove the possibly negative sense of my original response using the phrase "trick question" ...

Even though I think the question posed here is a "tricky" question that has no clear solution ... at least not in .NET WinForms, I feel compelled to clarify my earlier comment with the goal of avoiding creating any unnecessary confusion, and, possibly, clearing away a strange cloud of metaphysical smoke that has materialized here, perhaps smoke from an ancient yogi''s campfire, a practitioner of Advaita Vedanta''s ''neti neti'' school, as espoused in the Avadhuta Gita :)

While it is true, as MSDN says: "The user has no control on when the static constructor is executed in the program," you, the programmer, can force the constructor of a static class to be called.

For example, suppose I create a .NET project of type ''Class Library'' which has one static class:
using System;

namespace AStaticClassExperiment
{
    public class StaticClass
    {
        // the static constructor
        static StaticClass()
        {
            Console.WriteLine("myField = " + myField);
        }

        // a public static Property of type string
        public static string myField { get; set; }
    }
}

现在,我将其编译为DLL.然后,我创建一个新的.NET WinForm项目,在其中添加对该DLL的引用:

Now I compile this into a DLL. And, then, I create a new .NET WinForm project in which I add a reference to that DLL:

using AStaticClassExperiment;

如果在此新WinForm项目的代码中,我执行以下代码:

If, in the code for this new WinForm project, I execute this code:

AStaticClassExperiment.StaticClass.myField = "Hello from the DLL.";

访问字段的行为静态类的类保证将调用该类的静态构造函数,正如您可以通过检查输出窗口"来验证的那样.

一个静态类可以构成DLL的全部内容吗?
因此,当在WinForms项目中引用静态类时,在使用/引用静态类的某些部分之前:它在哪里?它是什么 ?好吧,您可能会认为它是未使用的模板",蓝图"或待办事项":一旦被选中",很可能会触发许多行为,并且改变自己的内部状态.

希望这对您有帮助!

The act of accessing a field of the static class guarantees the static constructor of the class will be invoked, as you can verify by examining the Output Window.

Can a single static class constitute the entire contents of a DLL: yes !

So, when a static class is referenced in a WinForms project, before some part of it is used/referenced: where is it ? what is it ? Well, you might think of it as an ''unused template,'' or ''blueprint,'' or ''to-do list:'' that, once ''picked up'' may well trigger a number of behaviors and alter its own internal state.

Hope this is helpful !


我认为.NET不能完全满足您的需求.我认为这是有充分的理由的.

我只是想说,您似乎只需要它,但是实际上,使用.NET,您不需要这样的东西. [END EDIT]

本质上,在.NET中没有DLL之类的东西.您可以创建它们,但是DLL的概念没有意义.相反,有意义的实体是程序集和程序集的可执行模块(许多人不知道区别是因为VS允许每个程序集仅创建一个可执行模块,但是原始C#编译器允许创建非程序集的单独的可执行模块) . DLL只是可能的扩展之一,它对汇编没有任何特殊的意义.特别是,DLL和EXE之间没有明显区别(它确实具有入口点,仅此而已);和EXE文件(带有入口点)也可以用作常规的类库程序集-在某些.NET进程的运行时引用或加载.
随着模块化意识形态的转变,使可执行模块之间的界限变得更加透明是很自然的.在开发过程中,开发人员并没有真正感觉到在开发,智能感知和调试过程中放在不同程序集中的代码部分之间的界限.特别是,程序集的加载时刻是透明的(不包括在运行时使用Reflection加载程序集的情况).
[Bill]正如Bill在对Manfred的回答的评论中正确指出的那样:用户无法控制程序中何时执行静态构造函数." [END EDIT]

另一个重要因素是.NET中使用的JIT(即时编译).从本质上讲,直到第一次需要使用本机代码之前,都不会将其编译为任何本机代码.这个重要因素使加载时的执行变得毫无意义.

特别是,在加载程序集时并不会在加载后立即调用静态构造函数.当在应用程序域中首次使用类时,将调用它们.此事件的确切时刻是不确定的.它仅保证在使用类之前会调用构造函数.

—SA
I don''t think .NET allows exactly what you need; and I think there is a good reason for this.

I would rather say it only seems to you that you need it, but in fact, with .NET, you don''t need such thing. [END EDIT]

Essentially, in .NET there is no such thing as DLL. You can create them, but the concept of DLL is meaningless. Instead, meaningful entities are assemblies and executable modules of assemblies (many people don''t know the difference just because VS allows creation of just one executable module per assembly, but raw C# compilers allow creation of separate executable modules which are non-assemblies). DLL is just one of possible extensions which does not carry any special meaning to assembly. In particular, there is no distinct difference between DLL and EXE (it does have entry point, but that''s it); and EXE files (with entry point) could also be used as regular class-library assemblies — referenced or loaded during run-time of some .NET process.

With this shift in ideology of modularity, it looks natural that the boundaries between executable module are made much more transparent. During development, a developer does not really feel the boundary between parts of codes put in different assemblies in development, intellisense and debugging. In particular, the moment of the loading of assembly is made transparent (excluding the cases of loading assembly during runtime with the use of Reflection).
As Bill correctly noted in his comment to the answer by Manfred, "The user has no control on when the static constructor is executed in the program." [END EDIT]

Another essential factor is JIT (Just-in-Time compilation) used in .NET. Essentially, nothing is even compiled to native code until it needs to be used for the first time. This important factors renders execution on loaded making no sense.

In particular, the static constructors are not called at the moment of loading of assemblies, and not immediately after that. They are called when a class is to be used for the very first time in the Application Domain. Exact moment of this event is undefined. It only guarantees that a constructor is called before the class is used.

—SA


[更新]
正如Bill和SA指出的那样,我的解决方案并不完全是OP在完整问题中所提出的.我的解决方案仅解决OP在他的最后一个问题声明中提出的问题.
[/更新]

据我所知,静态类是在加载类时初始化的.因此,您可以通过调用静态方法来初始化私有静态字段.我没有尝试过,但是我认为它应该起作用.

问候,

MRB
[Update]
As Bill and SA pointed out my solution is not exactly what OP layed out in the complete question. My solution only addresses what OP asked in his last question statement.
[/Update]

As far as I know are static fields initialized when a class is loaded. So you could initialize a private static field by calling a static method. I have not tried this, but I think it should work.

Regards,

MRB


这篇关于加载时执行DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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