int Set(CANMsg &CANObj)
我必须从 c# 调用上面的方法。直到现在我已经定义了一个包装器:
extern "C" __declspec(dllexport) int SetWrapper(CANMsg CANObj);
CANMsg CANObj --- 这个参数可以吗?或者我应该使用 CANMsg *CANObj 吗?
在这里我实现了包装器:
extern "C" __declspec(dllexport) int SetWrapper(CANMsg CANObj)
{
return Set(CANObj);
}
我正在创建这个包装器,因为这是函数的重载版本,我必须以某种方式有所作为。
这是 CANMsg 类:
class CANMsg
{
public:
CANMsg();
~CANMsg();
void AddRef() const;
void Release() const;
unsigned int MsgId;
unsigned int DLC;
unsigned int Handle;
unsigned int Interval;
unsigned int TimeStamp;
unsigned char Data0;
unsigned char Data1;
unsigned char Data2;
unsigned char Data3;
unsigned char Data4;
unsigned char Data5;
unsigned char Data6;
unsigned char Data7;
protected:
mutable int refCount;
};
现在,在 C# 中,我有以下内容:
[StructLayout(LayoutKind.Sequential)]
public class CANmsg
{
public int MsgId;
public int DLC;
public int Handle;
public int Interval;
public int TimeStamp;
public char Data0;
public char Data1;
public char Data2;
public char Data3;
public char Data4;
public char Data5;
public char Data6;
public char Data7;
}
导入是这样的:
[DllImport("engine.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I4)]
public static extern int SetWrapper(IntPtr canMSGObject);
我对那个 CANMsg 对象有点困惑,我是在将它声明为 IntPtr,marshal 还可以,还是类型?如果我让它这样,使用 IntPtr,我应该在那里执行什么样的实例化?如果我发送一个 CANMsg 对象,我会收到关于一些无效参数的错误。
如果您需要有关此的更多详细信息,请告诉我。