更改 C++ 内 Dll 的 .NET 4 运行时激活策略代码
我们开发了一个 C# 4.0 模块,需要从非托管 C++ 程序(以及 Fortran 程序)访问。 DLL 已正确加载,并且该方法可供 C++ 程序使用,但在调用指向 C# 代码的方法时会引发异常。对于可视化:
[Unmanaged C++ / Fortran Projects]
-> controller.dll (unmanaged C++ within the C++ project)
-> managed_wrapper.dll (managed C++ within the C# project)
-> C# 4.0 Project
当控制器尝试调用托管 C++(使用 .NET 4.0 框架)时,会引发异常。
我怀疑需要应用以下配置,因为我们之前在 创建概念证明:
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>
我正确地将其应用于上述概念证明,但那是在独立的控制台应用程序上。由于非托管 C++ 程序很大,并且设置并非每个模块都通用,因此我无法使用上述配置创建 MyApplication.exe.config
文件并立即结束。
我的问题是:是否可以将该配置应用于特定的 DLL?
We've developed a C# 4.0 module that needs to be accessed from an unmanaged C++ program (as well as a Fortran program). The DLL gets loaded correctly and the method is available to the C++ program, but an exception gets thrown when calling the method that points to the C# code. For a visualization:
[Unmanaged C++ / Fortran Projects]
-> controller.dll (unmanaged C++ within the C++ project)
-> managed_wrapper.dll (managed C++ within the C# project)
-> C# 4.0 Project
The exception gets thrown when the controller attempts to call the managed C++ (using .NET 4.0 framework).
My suspicions are that the following configuration needs to be applied, as we encountered an error previously when creating a proof of concept:
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>
I applied it correctly for the aforementioned proof of concept, but that was on a stand-alone console application. Since the unmanaged C++ program is huge and the settings are not universal for every module, I'm unable to create a MyApplication.exe.config
file with the above configuration and call it day.
My question is this: Is it possible to apply that configuration to a specific DLL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解,这是不可能的。
问题是,您需要应用的配置设置 (
useLegacyV2RuntimeActivationPolicy
) 基本上是说“对于整个应用程序,无论程序集请求什么,都使用 CLR 4。如果您可以< /em> 将其应用于单个 DLL,您将违反这里的目标 - 因为目标是强制所有程序集使用 CLR 4。
编辑:
经过进一步研究,实际上有一种方法可以在某些有限的情况下做到这一点我在我的博客上发布了有关此内容的内容,但基本上,您可以使用 CLR Hosting API 获得一些访问权限以从内部进行设置一个图书馆。
From my understanding, this is not possible.
The issue is that, the configuration setting you need to apply (
useLegacyV2RuntimeActivationPolicy
) is basically saying "for this entire application, no matter what the assembly requests, use CLR 4 instead.If you could apply it to a single DLL, you'd be violating the goals here - since the goal is to force all assemblies to use CLR 4.
Edit:
After further study, there is actually a way to do this in some limited scenarios. I posted about this on my blog, but basically, you can use the CLR Hosting API to get some access to setting this from within a library.