我不太瞭解C++,所以我添加了所有的dll代碼。如何將C#託管的結構傳遞給C++非託管DLL並在結果中獲取結構體?
C++ ** ** Calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include "global.h"
struct CALCULATORSHARED_EXPORT Input {
double a;
double b;
};
struct CALCULATORSHARED_EXPORT Result {
double sum;
double diff;
double prod;
double div;
};
global.h
#ifndef CALCULATOR_GLOBAL_H
#define CALCULATOR_GLOBAL_H
#if defined(CALCULATOR_EXPORTS)
# define CALCULATORSHARED_EXPORT __declspec(dllexport)
#else
# define CALCULATORSHARED_EXPORT __declspec(dllimport)
#endif
#endif // CALCULATOR_GLOBAL_H
calculator.h
#include "calculator.h"
Result calculate(const Input& input) {
Result result;
result.sum = input.a + input.b;
result.diff = input.a - input.b;
result.prod = input.a * input.b;
result.div = input.a/input.b;
return result;
}
C#
[DllImport("Calculator.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern Result calculate(Input input);
[StructLayout(LayoutKind.Sequential)]
public struct Input
{
public double a;
public double b;
};
[StructLayout(LayoutKind.Sequential)]
public struct Result
{
public double sum;
public double diff;
public double prod;
public double div;
};
private void button1_Click(object sender, EventArgs e)
{
Input input;
Result result;
input.a = 5;
input.b = 6;
result = calculate(input);
}
我越來越Unable to find an entry point named 'calculate' in DLL 'Calculator.dll'.
你其實從出口''Calculator.dll' calculate'? – GSerg
@GSerg我不太明白你的意思是從Calculator.dll導出計算。我已經用這個dll的所有C++代碼更新了我的問題。 – Arbaaz