我试图从非托管dll调用方法,而且我被困了。 对于fmi2Type和fmi2Boolean我可以使用byte吗? 回调函数(以及指向它们的指针)应如何在C#中显示?
方法I尝试呼叫是:
fmi2Component fmi2Instantiate(fmi2String instanceName,
fmi2Type fmuType,
fmi2String fmuGUID,
fmi2String fmuResourceLocation,
const fmi2CallbackFunctions* functions,
fmi2Boolean visible,
fmi2Boolean loggingOn)
类型说明如下:
typedef void* fmi2Component; /* Pointer to FMU instance */
typedef void* fmi2ComponentEnvironment; /* Pointer to FMU environment */
typedef void* fmi2FMUstate; /* Pointer to internal FMU state */
typedef unsigned int fmi2ValueReference;
typedef double fmi2Real ;
typedef int fmi2Integer;
typedef int fmi2Boolean;
typedef char fmi2Char;
typedef const fmi2Char* fmi2String;
typedef char fmi2Byte;
typedef struct {
const fmi2CallbackLogger logger;
const fmi2CallbackAllocateMemory allocateMemory;
const fmi2CallbackFreeMemory freeMemory;
const fmi2StepFinished stepFinished;
const fmi2ComponentEnvironment componentEnvironment;
} fmi2CallbackFunctions;
typedef enum {
fmi2ModelExchange,
fmi2CoSimulation
} fmi2Type;
typedef void (*fmi2CallbackLogger) (fmi2ComponentEnvironment, fmi2String, fmi2Status, fmi2String, fmi2String, ...);
typedef void* (*fmi2CallbackAllocateMemory)(size_t, size_t);
typedef void (*fmi2CallbackFreeMemory) (void*);
typedef void (*fmi2StepFinished) (fmi2ComponentEnvironment, fmi2Status);
对于回调,我为每个回调制作了包含类和委托的回调。
public class FMICallbackFunctions
{
public delegate void FMILibrary_FMICallbackLogger(IntPtr fmiComponent, string instanceName, int status, string category, string message, IntPtr parameters);
public delegate IntPtr FMILibrary_FMICallbackAllocateMemory(int numberOfObjects, int size);
public delegate void FMILibrary_FMICallbackFreeMemory(IntPtr @object);
public delegate void FMILibrary_FMIStepFinished(IntPtr fmiComponent, int status);
public FMICallbackFunctions(FMILibrary_FMICallbackLogger logger, FMILibrary_FMICallbackAllocateMemory allocateMemory, FMILibrary_FMICallbackFreeMemory freeMemory, FMILibrary_FMIStepFinished stepFinished)
: base()
{
this.logger = logger;
this.allocateMemory = allocateMemory;
this.freeMemory = freeMemory;
this.stepFinished = stepFinished;
}
}
然后初始化该类并尝试将其传递给非托管方法调用
FMICallbackFunctions callbackFunctions = new FMICallbackFunctions(logger, allocateMemory, freeMemory, stepFinished);
但是收到错误:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。
我认为它导致错误的回调参数。有没有人提示可能有什么问题?