簡體   English   中英

嘗試將 C/C++ DLL 導入 C# 時出現 DLL EntryPointNotFoundException

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

我的 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);

}

這是我的 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# 中正確播放。

如果您剛剛導入了 DLL 並且已經導入了另一個同名的,請重新啟動 Unity 以卸載舊的 DLL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM