【问题标题】:DLL EntryPointNotFoundException when trying to import C/C++ DLL into C#尝试将 C/C++ DLL 导入 C# 时出现 DLL EntryPointNotFoundException
【发布时间】:2021-04-12 18:49:41
【问题描述】:

我的标题:

#define DLLExport __declspec (dllexport)
#define MAXCOLS 3


extern "C" {

    typedef struct Matrix {

        double** array; //contains all values
        int rows;
        int cols;


    }Matrix;


    DLLExport Matrix zeros(int num_rows, int num_cols);
    DLLExport void print(Matrix mat);
    DLLExport Matrix add(Matrix a, Matrix b);
    DLLExport Matrix subtract(Matrix a, Matrix b);
    DLLExport Matrix scalar_mult(Matrix a, double s);
    DLLExport Matrix from_array(static int rows, static int cols, double a[][MAXCOLS]);
    DLLExport Matrix slice_by_rows(Matrix y, int row_1, int row_2);
    DLLExport Matrix vstack(Matrix a, Matrix b);
    DLLExport Matrix transpose(Matrix a);
    DLLExport Matrix diagonal(Matrix y);
    DLLExport Matrix elem_mul(Matrix a, Matrix b);
    DLLExport Matrix row_sum(Matrix y);

}

这是我的 CPP 文件(我省略了大部分功能):

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "Header.h"


#define DLLExport __declspec (dllexport)
#define MAXCOLS 3

extern "C" {

    DLLExport Matrix zeros(int num_rows, int num_cols) {

        double** arr = (double**)calloc(num_rows, sizeof(double*));
        for (int i = 0; i < num_rows; i++) {

            arr[i] = (double*)calloc(num_cols, sizeof(double));

        }

        Matrix mat;
        mat.array = arr;
        mat.rows = num_rows;
        mat.cols = num_cols;

        return mat;

    }


    DLLExport void print(Matrix mat) {

        for (int i = 0; i < mat.rows; i++) {
            for (int j = 0; j < mat.cols; j++) {
                printf("%f ", mat.array[i][j]);
            }
            printf("\n");
        }

    }
//...
}

这是我的 C# 文件中的内容:

//...
public unsafe struct Matrix
    {
        public double** array;
        public int rows;
        public int cols;

    }

    [DllImport("FastMatrix")]
    public static extern unsafe Matrix zeros(int num_rows, int num_cols);

    [DllImport("FastMatrix")]
    public static extern void print(Matrix mat);
    //...

我正在尝试在 Unity 中执行此操作,因此可能缺少一个设置。当我只有 .cpp 文件和一些基本函数(如添加两个整数、乘法等)时它可以工作,但是当我添加这些函数时它突然停止工作。

编辑:

看来我只需要重新启动 Unity。现在一切似乎都在工作,除了当我尝试在编辑器中点击“播放”时,Unity 完全崩溃了。我猜这是因为一些 C 指针在 C# 中没有正确播放。

【问题讨论】:

  • 不用__cdecl吗?另外:想知道为什么要在 C 中执行此操作,我认为如果您知道自己在做什么,它可以在 C# 中执行
  • C++ 函数是否使用修饰/损坏的名称导出?如果是,则必须在 DllImport.EntryPoint 字段中指定修饰名称。此外,您的 C++ 函数可能使用 __cdecl 调用约定,但 DllImport 默认为 __stdcall 以与 Win32 API 兼容。请改用DllImport.CallingConvention 属性指定Cdecl
  • 具体是哪些入口点或哪些点没有找到?发布错误消息。

标签: c# c++ c unity3d


【解决方案1】:

如果您刚刚导入了 DLL,并且已经导入了另一个同名的 DLL,请重新启动 Unity 以卸载旧的 DLL。

【讨论】:

    猜你喜欢
    • 2018-10-25
    • 2012-10-05
    • 2011-01-28
    • 1970-01-01
    • 2021-01-21
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多