混合托管/非托管程序集加载顺序

发布于 2024-12-22 10:11:29 字数 788 浏览 1 评论 0原文

我对混合 .NET 程序集(即混合托管/非托管 C++ 程序集)的 CLR 和 CRT 的初始化顺序有一些疑问。

因此,如果我有一个混合模式程序集 dll 文件,它会通过 Assembly.Load 加载。我注意到,在首先调用某些托管代码之前,任何静态本机对象都不会被初始化或调用。好的。所以我认为在启动时,CLR 代码首先被初始化,CRT 初始化最后被调用。停工指令是怎样的? CRT 是否先关闭,CLR 最后关闭?

那么事情是这样的吗?

start of program lifetime

initilialize CLR
...initilialize CRT
...construct native static instances

... program runs....

...shutdown CLR
...destruct native static instances
shutdown CLR

end of program lifetime

或者是按照其他顺序?

我的问题也适用于可执行文件的混合模式程序集(即 .exe)。是不是很相似?

start of program lifetime

initilialize CLR
...initilialize CRT
...construct native static instances

... program runs....

...shutdown CLR
...destruct native static instances
shutdown CLR

end of program lifetime

I have some questions about the order of initialization for the CLR and the CRT for mixed .NET assemblies (i.e. meaning mixed managed/unmanaged C++ assemblies).

So if I have a mixed mode assembly dll file, and it gets loaded via Assembly.Load. I notice that any static native objects won't get initialized or called until some managed code is called first. OK. So I take it that on startup that the CLR code gets initialized first, and CRT initialization gets called last. How is the order for shutdown? Is the CRT shutdown first, and the CLR shutdown last?

So is this how it goes?

start of program lifetime

initilialize CLR
...initilialize CRT
...construct native static instances

... program runs....

...shutdown CLR
...destruct native static instances
shutdown CLR

end of program lifetime

Or is it in some other order?

My question also applies to mixed mode assemblies that are executables (i.e. .exe). It is similar?

start of program lifetime

initilialize CLR
...initilialize CRT
...construct native static instances

... program runs....

...shutdown CLR
...destruct native static instances
shutdown CLR

end of program lifetime

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

闻呓 2024-12-29 10:11:29

混合模式程序集的初始化的 MSDN 页面对此进行了介绍。

这实际上与你的想法相反。本机代码首先初始化,然后是托管代码。您无权访问 DllMain 内的任何托管代码。

MSDN 上没有明确记录拆卸过程顺序,C++/CLI 规范中似乎也没有明确记录。我相信这是特定于实现的,并在与附录的本机库和托管库之间的交互相关的“未记录行为”部分中进行了介绍。

This is covered in the MSDN Page for Initialization of Mixed Mode Assemblies.

It's actually the opposite of your idea. Native code gets initialized first, then the managed code. You are not allowed to access any managed code inside of DllMain.

The tear down process order isn't explicitly documented on MSDN, and doesn't appear to be documented explicitly in the C++/CLI specifications. I believe this is implementation specific, and covered under the "Undocumented Behavior" section relating to interaction between native and managed libraries of the appendix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文