更改 C++ 内 Dll 的 .NET 4 运行时激活策略代码

发布于 2024-12-02 04:03:25 字数 901 浏览 1 评论 0原文

我们开发了一个 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 技术交流群。

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

发布评论

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

评论(1

反目相谮 2024-12-09 04:03:25

据我了解,这是不可能的。

问题是,您需要应用的配置设置 (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.

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