在哪里初始化托管 C++/CLI DLL?
使用 C++/CLI 创建 DLL 时,或者我应该问,是否存在与 DllMain 等效的东西?
这段初始化代码中不能调用的内容是否有任何限制?
What is, or should I ask, is there, an equivalent to DllMain when creating a DLL using C++/CLI?
Are there any restrictions on what cannot be called from this initialization code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 .NET 2.0 开始,您就有了一个“模块初始值设定项”。 有关如何解决加载程序锁定问题的详细信息,请参阅此处 和也在这里
直接回答您的问题,此页面引用了标准,内容如下:
“模块初始值设定项中允许的代码没有限制。模块初始值设定项允许运行和调用托管和非托管代码。”
Since .NET 2.0 you have a "module initializer". See here for more information on how that solves the loader lock problem and also here
For a direct answer to your question, this page quotes the standard which says:
"There are no limitations on what code is permitted in a module initializer. Module initializers are permitted to run and call both managed and unmanaged code."
如果您在另一个托管项目(例如 ac# 应用程序)中使用该 dll,则无需执行任何操作...只要您尝试访问的类是 ref 类,您可以从任何其他托管应用程序访问它们。
If you're using the dll in another managed project (a c# application for example), you don't need to do anything... As long as the classes you try to access are
ref
classes, you can access them from any other managed application..Net dll 的一大优点是它们可以避免加载程序锁。 一个副作用是没有 DllMain。
One giant advantage of .Net dlls is that they avoid the loader lock. One side effect is that there's no DllMain.
Dan:关于加载器锁、C++/CLI 的 CLR 延迟加载以及混合模式二进制文件的正确初始化,我昨天刚刚在 此处的主题。
或多或少,如果您有混合模式二进制文件,则在
DllMain()
中时不得导致任何托管代码运行。Dan: With respect to the loader lock, C++/CLI's delay load of the CLR and proper initialization for a mixed mode binary, I just posted yesterday on the subject here.
More or less, if you have a mixed mode binary, you must not cause any managed code to run while you are in
DllMain()
.