创建 C# DLL 以在 C++ 可执行文件中使用

问题描述 投票:0回答:1

我已经在这个话题上苦苦挣扎了 1 周,但我所做的一切似乎都不起作用。 我做了一个非常简单的 C# 类:

namespace SimpleMathLib
{
    public class SimpleMath
    {
        public float SumFloat(float a, float b)
        {
            return a + b;
        }

        public int SumInt(int a, int b)
        {
            return a + b;
        }
    }
}

在配置为构建 DLL 的 Visual Studio 2022 解决方案中:

我确实遵循了一些在线教程,但给我带来的问题较少的是关于创建使用 CLR 的 C++ 包装器并将其编译为静态库 (.lib) 的教程。 以下是我在此包装器中添加的脚本:

// SimpleMathLibWrapper.h
#pragma once

class SimpleMathLibWrapperPrivate;

class __declspec(dllexport) SimpleMathLibWrapper
{
    private:
        SimpleMathLibWrapperPrivate* _private;

    public:
        SimpleMathLibWrapper();
        ~SimpleMathLibWrapper();

        float SumFloat(const float a, const float b);
        int SumInt(const int a, const int b);
};

// SimpleMathLibWrapper.cpp
#include "..\public\SimpleMathLibWrapper.h"

#include <msclr\auto_gcroot.h>
#include <msclr\marshal_cppstd.h>

using namespace System::Runtime::InteropServices;
using namespace SimpleMathLib;

class SimpleMathLibWrapperPrivate
{
    public:
        msclr::auto_gcroot<SimpleMath^> simpleMathCSharp;
};

SimpleMathLibWrapper::SimpleMathLibWrapper()
{
    _private = new SimpleMathLibWrapperPrivate();
    _private->simpleMathCSharp = gcnew SimpleMath();
}

SimpleMathLibWrapper::~SimpleMathLibWrapper()
{
    delete _private;
}

float SimpleMathLibWrapper::SumFloat(const float a, const float b)
{
    return _private->simpleMathCSharp->SumFloat(a, b);
}

int SimpleMathLibWrapper::SumInt(const int a, const int b)
{
    return _private->simpleMathCSharp->SumInt(a, b);
}

这些是我必须更改的 Visual Studio CLR 库项目的设置: 此外,我将编译后的 C# DLL 引用添加到项目中

现在,对于 C++ 可执行项目,它是一个包含以下脚本的 Visual Studio C++ 控制台项目:

// SimpleMathLibUser.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

//#define LOAD_DLL_MANUALLY

#include <iostream>
#include <Windows.h>

#ifndef LOAD_DLL_MANUALLY
    #include "SimpleMathLibWrapper.h"
#endif // !LOAD_DLL_MANUALLY


#ifdef LOAD_DLL_MANUALLY
    void PrintExecutablePath()
    {
        TCHAR exePath[MAX_PATH];
        GetModuleFileName(NULL, exePath, MAX_PATH);
        char narrowExePath[MAX_PATH];

        // convert the string to a narrow character string
        if (WideCharToMultiByte(CP_ACP, 0, exePath, -1, narrowExePath, MAX_PATH, 0, 0) == 0)
        {
            std::cerr << "Failed to convert the path to a narrow character string." << std::endl;
            return;
        }

        char* lastSlash = strrchr(narrowExePath, '\\');
        if (lastSlash != NULL)
        {
            *lastSlash = '\0';
        }

        std::cout << "Current directory: " << narrowExePath << std::endl << std::endl;
    }
#endif // LOAD_DLL_MANUALLY

int main()
{
#ifdef LOAD_DLL_MANUALLY
    PrintExecutablePath();

    // load the DLL.
    HMODULE mathLib = LoadLibrary(TEXT("..\\Plugins\\SimpleMathLibWrapper.dll"));

    if (mathLib == NULL)
    {
        std::cerr << "Failed to load the DLL." << std::endl;
        return 1;
    }

    // get a pointer to functions from the DLL.
    float (*SumFloat)(float, float) = (float (*)(const float, const float))GetProcAddress(mathLib, "SumFloat");
    int (*SumInt)(int, int) = (int (*)(const int, const int))GetProcAddress(mathLib, "SumInt");

    if (SumFloat == NULL)
    {
        std::cerr << "Failed to find the 'SumFloat' function in the DLL." << std::endl;
        return 1;
    }

    if (SumInt == NULL)
    {
        std::cerr << "Failed to find the 'SumInt' function in the DLL." << std::endl;
        return 1;
    }

    // call the functions.
    float resultFloat = SumFloat(10.f, 5.f);
    std::cout << "Float Sum: " << resultFloat << std::endl;

    int resultInt = SumInt(2, 3);
    std::cout << "Int Sum: " << resultInt << std::endl;

    // unload the DLL.
    FreeLibrary(mathLib);
#else
    SimpleMathLibWrapper wrapper;

    std::cout << "Float Sum: " << wrapper.SumFloat(10.f, 5.f) << std::endl;
    std::cout << "Int Sum: " << wrapper.SumInt(2, 3) << std::endl;
#endif // LOAD_DLL_MANUALLY

    return 0;
}

如您所见,为此我尝试了两种方法:

  1. 手动加载包装纸
  2. 使用链接器

这些是我为链接器解决方案添加的设置(目前至少可以编译并且似乎可以在某一点之前工作):

无论 C# dll 使用哪个 .NET 版本编译,当我运行可执行文件时,我总是会收到以下错误:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. Impossible to find the scpecified file.
   in SimpleMathLibWrapper.{ctor}(SimpleMathLibWrapper* )
   in mainCRTStartup()

据我所知,.NET 版本的 Runtime 和 SDK 均已在系统中安装并正确引用。 我在C# VS项目中选择任何版本的目标框架,结果都是一样的;仅程序集名称和版本发生变化。

我还在互联网上寻找了这个特定问题,但大多数解决方案都尝试直接从 .sln 文件打开项目,而不是从 VS 打开项目,这对我不起作用。

手动加载 DLL 会导致一系列我无法解决的不同问题,因此,我保留了代码以供参考,但放弃尝试修复它。

我知道这是一篇很长的文章,我鼓励您向我询问更多详细信息,以防我错过了什么。 希望在这里找到解决方案将来能够帮助更多的人。

谢谢!!

c# c++ dll clr lib
1个回答
0
投票

在阅读了我收到的一些评论后,我终于能够让一切正常运转了!!!

首先,由于 CLR 的原因,C# DLL 需要使用小于或等于 4.7.2 的 .NET Framework 版本进行编译。 为了实现这个目标你应该做什么?像这样编辑 .csproj 文件:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
    <ImplicitUsings>disable</ImplicitUsings>
    <Nullable>disable</Nullable>
    <SignAssembly>False</SignAssembly>
    <AssemblyOriginatorKeyFile>SimpleMathManaged</AssemblyOriginatorKeyFile>
  </PropertyGroup>

</Project>

确保指向正确的版本,禁用 ,以便项目不会使用该框架不可用的 GlobalUsing ,您还需要禁用 ,也不支持。

之后,重新编译 DLL、包装器(我没有更改任何内容)以及 C++ 可执行文件以更新到最新的包装器 LIB。 确保将 C# DLL 复制到 .exe 文件所在的位置(我忘记在我的原始帖子中提及这一点),运行项目,一切都应该正常工作! :) 最后...

基本上,我最初的错误是使用 .NET 6.0(使用 Visual Studio 创建新的 C# 项目时的默认设置)编译 DLL,该版本高于 4.7.2 并且不受支持。 包装器编译过程没有启动任何警告或错误,但从可执行文件使用时它不起作用。

第二个错误是尝试使用 .Net Standard 2.1,它同样不受支持,也没有抛出任何警告。

当我按照 JonasH 和 Hans Passant(谢谢大家)在评论中的建议尝试 .NET 4.8 时,我终于收到了来自包装器的警告,告诉我目标 .NET Framework 不受支持,因为高于 4.7 .2 然后,最终瞄准正确的版本,一切正常。

我真的希望这个解决方案可以帮助每个遇到同样问题的人,因为找到解决方案并不有趣。

© www.soinside.com 2019 - 2024. All rights reserved.