使用 .NET Core 2.1 托管 C++ [英] Managed C++ with .NET Core 2.1

查看:40
本文介绍了使用 .NET Core 2.1 托管 C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用 C++ 编写的库.为了使其与我们更现代的 .NET 项目更兼容,我们将这个 C++ 库包装在另一个 .NET 项目中.从完整的 .NET Framework 项目(4.5、4.6 等)中引用它时,它工作正常.

We have a library written in C++. To make it more compatible with our more modern .NET projects, we wrapped this C++ library in another .NET project. It works fine when referencing it from full .NET Framework projects (4.5, 4.6, etc.).

我正在使用 .NET Core 2.1 创建一个新应用程序,并且我正在尝试引用这个封装在 .NET C++ 库中".在我第一次尝试时,它说无法加载程序集失败.我通过安装 .NET Core SDK x86 并强制我的应用程序使用 x86,而不是 Any CPU 解决了这个问题.

I am creating a new application using .NET Core 2.1 and I am trying to reference this "wrapped-in-.NET C++ library". On my first attempt, it failed saying the assembly couldn't be loaded. I fixed this problem by installing .NET Core SDK x86 and forcing my application to use x86, not Any CPU.

我没有遇到构建错误,但是当我尝试在这个库中实例化一个类时,我得到以下异常:

I get no build errors, but when I try to instantiate a class within this library, I get the following exception:

<CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load.
 ---> System.EntryPointNotFoundException: A library name must be specified in a DllImport attribute applied to non-IJW methods.
   at _getFiberPtrId()
   at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )
   at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
   --- End of inner exception stack trace ---
   at <CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, Exception innerException)
   at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
   at .cctor()

.NET Core 2.1 是否完全支持这种场景?

Does .NET Core 2.1 support this scenario at all?

推荐答案

正如其他人所指出的,.NET Core 当前不支持 C++/CLI(又名托管 C++").如果要调用 .NET Core 中的本机程序集,则必须使用 PInvoke(如您所见).

As others pointed out, .NET Core does not currently support C++/CLI (aka "managed C++"). If you want to call into native assemblies in .NET Core, you must use PInvoke (as you discovered).

你也可以在 AnyCPU 中编译你的 .NET Core 项目,只要你保持 32- &64 位版本您的本机库并在您的 PInvoke 调用周围添加特殊的分支逻辑:

You can also compile your .NET Core project in AnyCPU, as long as you keep around both 32- & 64-bit versions your native library and add special branching logic around your PInvoke calls:

using System;

public static class NativeMethods
{
    public static Boolean ValidateAdminUser(String username, String password)
    {
        if (Environment.Is64BitProcess)
        {
            return NativeMethods64.ValidateAdminUser(username, password);
        }
        else
        {
            return NativeMethods32.ValidateAdminUser(username, password);
        }
    }

    private static class NativeMethods64
    {
        [DllImport("MyLibrary.amd64.dll", EntryPoint = "ValidateAdminUser", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean ValidateAdminUser(String username, String password);
    }

    private static class NativeMethods32
    {
        [DllImport("MyLibrary.x86.dll", EntryPoint = "ValidateAdminUser", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean ValidateAdminUser(String username, String password);
    }
}

MyLibrary.amd64.dll 和 MyLibrary.x86.dll 程序集位于同一目录中的位置.如果您可以将相对路径放入 DllImport 并具有 x86/amd64 子目录,那就太好了,但我还没有想出如何做到这一点.

Where you have your MyLibrary.amd64.dll and MyLibrary.x86.dll assemblies in the same directory. It would be nice if you could put relative paths into DllImport and have x86/amd64 subdirectories, but I haven't figured out how to do that.

这篇关于使用 .NET Core 2.1 托管 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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