Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从C++;动态链接库? 问题是,我试图从C++程序中访问一个C++变量中声明的全局变量。即使我在一个重复调用的函数中将变量设置为15(在本例中为减法),每次调用getter(在本例中为除法),返回值都是零。 这是我的代码。头文件: #ifdef MATHFUNCSDLL_EXPORTS #define MATHFUNCSDLL_API __declspec(dllexport) #else #define MATHFUNCSDLL_API __declspec(dllimport) #endif namespace MathFuncs { class MyMathFuncs { public: static MATHFUNCSDLL_API double Add(double a, double b); static MATHFUNCSDLL_API double Subtract(double a, double b); static MATHFUNCSDLL_API double Multiply(double a, double b); static MATHFUNCSDLL_API double Divide(double a, double b); }; }_C#_C++_Variables_Dll_Unity3d - Fatal编程技术网

C# 如何从C++;动态链接库? 问题是,我试图从C++程序中访问一个C++变量中声明的全局变量。即使我在一个重复调用的函数中将变量设置为15(在本例中为减法),每次调用getter(在本例中为除法),返回值都是零。 这是我的代码。头文件: #ifdef MATHFUNCSDLL_EXPORTS #define MATHFUNCSDLL_API __declspec(dllexport) #else #define MATHFUNCSDLL_API __declspec(dllimport) #endif namespace MathFuncs { class MyMathFuncs { public: static MATHFUNCSDLL_API double Add(double a, double b); static MATHFUNCSDLL_API double Subtract(double a, double b); static MATHFUNCSDLL_API double Multiply(double a, double b); static MATHFUNCSDLL_API double Divide(double a, double b); }; }

C# 如何从C++;动态链接库? 问题是,我试图从C++程序中访问一个C++变量中声明的全局变量。即使我在一个重复调用的函数中将变量设置为15(在本例中为减法),每次调用getter(在本例中为除法),返回值都是零。 这是我的代码。头文件: #ifdef MATHFUNCSDLL_EXPORTS #define MATHFUNCSDLL_API __declspec(dllexport) #else #define MATHFUNCSDLL_API __declspec(dllimport) #endif namespace MathFuncs { class MyMathFuncs { public: static MATHFUNCSDLL_API double Add(double a, double b); static MATHFUNCSDLL_API double Subtract(double a, double b); static MATHFUNCSDLL_API double Multiply(double a, double b); static MATHFUNCSDLL_API double Divide(double a, double b); }; },c#,c++,variables,dll,unity3d,C#,C++,Variables,Dll,Unity3d,C++代码: __declspec(dllexport) double signID; //this is my variable __declspec(dllexport) double __cdecl MyMathFuncs::Subtract(double a, double b){ //.. some code signID = 15; //this function is the setter } __declspec(dllexport) double __cdecl

C++代码:

__declspec(dllexport) double signID; //this is my variable

__declspec(dllexport) double __cdecl MyMathFuncs::Subtract(double a, double b){
   //.. some code
   signID = 15; //this function is the setter
}

__declspec(dllexport) double __cdecl MyMathFuncs::Divide(double a, double b)
{
    return signID; //this function is the getter, it return zero when called from C#
}
在我的C#代码中,我使用了以下方法: 我一直从getter函数中得到一个零返回值,为什么,如何解决这个问题

编辑:C#代码:

我猜(没有看到你的C++代码,这个问题在你的调用约定中)。 这意味着调用函数负责堆栈。在本例中,您的.net程序.c#默认为调用约定u stdcall。这对于采用或返回参数的方法非常重要,因为它决定堆栈的行为

我已经包括了代码,它应该向您展示一种方法,使这项工作

c++代码头

#define DLLEXPORT __declspec(dllexport)
#ifdef __cplusplus
extern "C" { //Used to prevent name mangling on dll export
#endif //__cplusplus

double signID;

DLLEXPORT void __stdcall SetDoubleValue();
DLLEXPORT double __stdcall ReturnDoubleValue(double dummyone, double dummytwo);

#ifdef __cplusplus
}
#endif //__cplusplus
c++

c#导入代码

[DllImport("DllExport.dll")]
public static extern void SetDoubleValue();

[DllImport("DllExport.dll")]
public static extern double ReturnDoubleValue(double dummyone, double dummytwo);
注意:如果您不能或不想从#cdecl更改,也可以将您的c#定义更改为

[DllImport("DllExport.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void SetDoubleValue();

[DllImport("DllExport.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern double ReturnDoubleValue(double dummyone, double dummytwo);

看起来我的原始代码和发布的解决方案都能正常工作-我似乎错误地识别了DLL中没有的问题。

你能添加你的C代码吗
[DllImport("DllExport.dll")]
public static extern void SetDoubleValue();

[DllImport("DllExport.dll")]
public static extern double ReturnDoubleValue(double dummyone, double dummytwo);
[DllImport("DllExport.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void SetDoubleValue();

[DllImport("DllExport.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern double ReturnDoubleValue(double dummyone, double dummytwo);