简体   繁体   English

尝试将 C/C++ DLL 导入 C# 时出现 DLL EntryPointNotFoundException

[英]DLL EntryPointNotFoundException when trying to import C/C++ DLL into C#

My header:我的 header:

#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);

}

And this is my CPP file (I've omitted most of the functions):这是我的 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");
        }

    }
//...
}

This is what I have in my C# file:这就是我的 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);
    //...

I'm trying to do this in Unity, so there might be a setting I'm missing.我正在尝试在 Unity 中执行此操作,因此可能缺少一个设置。 It worked when I had just the.cpp file and some basic functions (like add two ints, multiply, etc.), but when I added these functions it suddenly stopped working.当我只有 .cpp 文件和一些基本函数(如添加两个整数、乘法等)时它可以工作,但是当我添加这些函数时它突然停止工作。

EDIT:编辑:

So it appears that all I needed was to restart Unity.所以看来我只需要重新启动 Unity。 Everything appears to be working now, except for Unity flat out crashing when I try to hit "play" in the editor.现在一切似乎都在工作,除了当我尝试在编辑器中点击“播放”时,Unity 完全崩溃了。 I'm guessing that's from some C pointers not playing in C# properly.我猜这是因为一些 C 指针没有在 C# 中正确播放。

If you just imported your DLL and you had another one of the same name already imported, restart Unity to unload the old DLL.如果您刚刚导入了 DLL 并且已经导入了另一个同名的,请重新启动 Unity 以卸载旧的 DLL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM