C#调用fmu方法

C#调用C++接口声明
[DllImport(@"C:\AddNoClass.dll", CallingConvention = CallingConvention.Cdecl)]
extern static double TwoNumberAdd(double num1, double num2);
C++接口参数导致的问题

有如下代码段:

//C++接口导出
ADD_NO_CLASS_DLL void* func1(char* arg1, void* arg2);
ADD_NO_CLASS_DLL void* func2(string arg1, void* arg2);

//在C#中如下方式导入
[DllImport(@"C:\AddNoClass.dll", CallingConvention = CallingConvention.Cdecl)]
extern unsafe static void* func1(string arg1, void* arg2);
[DllImport(@"C:\AddNoClass.dll", CallingConvention = CallingConvention.Cdecl)]
extern unsafe static void* func2(string arg1, void* arg2);

func1与func2的导出接口参数类型分别为char*string类型,在C#中调用两函数,执行func2时会报错如下:

Additional information: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

通常,这个错误的引发是参数传递错误,如某导出接口一参数类型为fmi2CallbackFunctions(struct指针),则在DllImport时,引入函数参数类型应写作fmi2CallbackFunctions

Unity调用C封装的fmu接口,出现以下错误:(DllImport时不写“.dll”)
System.DllNotFoundException: C:\Users\wangxh\Documents\Visual Studio 2010\Projects\CallFMUDll\Debug\CompFmuEncapsulation.dll
at (wrapper managed-to-native) OnSimStart:instantiate (double,double)
at OnSimStart.OnClick () [0x0001b] in C:\Users\wangxh\Documents\MayaModelImport\Assets\Scripts\OnSimStart.cs:59

你可能感兴趣的:(C#调用fmu方法)