Marshalling C++ struct with a int* member to C#
I have a C++ structure below:
struct CTMDeviceError {
struct CTMDeviceInfo sDeviceInfo;
int32_t iResultCode;
int32_t iExtendedResultCode;
int32_t * piDenomination;
int32_t * piChangeDue;
};
I have created an equivalent c# structure but I'm having trouble marshalling the int32 * type.
[StructLayout(LayoutKind.Sequential)]
public struct CTMDeviceError
{
public CTMDeviceInfo deviceInfo;
[MarshalAs(UnmanagedType.I4)]
public Int32 resultCode;
[MarshalAs(UnmanagedType.I4)]
public Int32 extendedResultCode;
public ??? denomination;
public ??? changeDue;
};
I have tried using IntPtr or Int32[] but it Visual Studio shows mismatch errors. Can I please get some advise? Thanks!
This is the rest of the struct details:
c++
struct CTMDeviceInfo {
enum CTMDeviceType eDeviceType;
char * szDeviceModel;
char * szDeviceSubModel;
int32_t * piDeviceID;
};
enum CTMDeviceType {
CTM_DEVICETYPE_CASHCHANGER = 5,
CTM_DEVICETYPE_CASHACCEPTOR = 15,
CTM_DEVICETYPE_COINACCEPTOR = 16,
CTM_DEVICETYPE_OTHER = 17
};
c#
[StructLayout(LayoutKind.Sequential)]
public struct CTMDeviceInfo
{
public CTMDeviceType deviceType;
[MarshalAs(UnmanagedType.LPStr)]
public string deviceModel;
[MarshalAs(UnmanagedType.LPStr)]
public string deviceSubModel;
public ??? deviceId;
};
public enum CTMDeviceType
{
CTM_DEVICETYPE_CASHCHANGER = 5,
CTM_DEVICETYPE_CASHACCEPTOR = 15,
CTM_DEVICETYPE_COINACCEPTOR = 16,
CTM_DEVICETYPE_OTHER = 17
};
指针的类型应该是System.IntPtr
。
上一篇: 用C#编写固定大小的2D char数组的C ++ Struct
下一篇: 用int *成员将C ++结构编组为C#