简体   繁体   English

将C ++结构转换为结构C#

[英]Convert c++ struct to struct c#

i have a problem with convert a struct c++ to c# struct, i will leave the following code: 我将struct c ++转换为c#struct时遇到问题,我将保留以下代码:

C++ C ++

struct SDK_ALARM_INPUTCONFIG
{
bool    bEnable;        
int     iSensorType;    
SDK_EventHandler hEvent;    
};

struct SDK_EventHandler
{
unsigned int    dwRecord;               
int     iRecordLatch;               
unsigned int    dwTour;                     
unsigned int    dwSnapShot;             
unsigned int    dwAlarmOut;             
unsigned int    dwMatrix;               
int     iEventLatch;            
int     iAOLatch;                 
SDK_PtzLinkConfig PtzLink[NET_MAX_CHANNUM];     
SDK_CONFIG_WORKSHEET schedule;      

bool    bRecordEn;              
bool    bTourEn;                 
bool    bSnapEn;                    
bool    bAlarmOutEn;            
bool    bPtzEn;


bool    bTip;                       
bool    bMail;                      
bool    bMessage;                   
bool    bBeep;                      
bool    bVoice;                         
bool    bFTP;                   
bool    bMatrixEn;              
bool    bLog;                   
bool    bMessagetoNet;           

bool    bShowInfo;              
unsigned int    dwShowInfoMask;         
char    pAlarmInfo[8];

bool    bShortMsg;              
bool    bMultimediaMsg;         
};

struct SDK_PtzLinkConfig
{
int iType;      
int iValue;      
};


struct SDK_CONFIG_WORKSHEET
{   
SDK_TIMESECTION tsSchedule[6][7];
};

 struct SDK_TIMESECTION
    {

        int enable;
        int startHour;
        int startMinute;
        int startSecond;
        int endHour;
        int endMinute;
        int endSecond;
    };

C#: C#:

[StructLayout(LayoutKind.Sequential)]
public struct SDK_ALARM_INPUTCONFIG
{
    public bool bEnable;
    public int iSensorType;
    public SDK_EventHandler hEvent;
};

   [StructLayout(LayoutKind.Sequential)]
   public struct SDK_EventHandler
{
   public ushort dwRecord;
   public int iRecordLatch;
   public ushort dwTour;
   public ushort dwSnapShot;
   public ushort dwAlarmOut;
   public ushort dwMatrix;
   public int iEventLatch;
   public int iAOLatch;

   [MarshalAs(UnmanagedType.ByValArray, ArraySubType=UnmanagedType.Struct, SizeConst = 32)]
   public SDK_PtzLinkConfig[] PtzLink;

   public SDK_CONFIG_WORKSHEET schedule;

   public bool bRecordEn;
   public bool bTourEn;
   public bool bSnapEn;
   public bool bAlarmOutEn;
   public bool bPtzEn;
   public bool bTip;
   public bool bMail;
   public bool bMessage;
   public bool bBeep;
   public bool bVoice;
   public bool bFTP;
   public bool bMatrixEn;
   public bool bLog;
   public bool bMessagetoNet;

   public bool bShowInfo;
   public ushort dwShowInfoMask;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
   public string pAlarmInfo;

   //public bool bShortMsg;
   //public bool bMultimediaMsg;         
};
[StructLayout(LayoutKind.Sequential)]
public struct SDK_PtzLinkConfig
{
    public int iType;
    public int iValue;  
};
[StructLayout(LayoutKind.Sequential)]
public struct SDK_CONFIG_WORKSHEET
{
    [MarshalAs(UnmanagedType.ByValArray, ArraySubType=UnmanagedType.Struct, SizeConst = 6*7)]
    public SDK_TIMESECTION[] tsSchedule;
};

[StructLayout(LayoutKind.Sequential)]
 struct SDK_TIMESECTION
    {

        public int enable;
        public int startHour;
        public int startMinute;
        public int startSecond;
        public int endHour;
        public int endMinute;
        public int endSecond;
    };

I'm calling this method: 我正在调用此方法:

C++: C ++:

long  H264_DVR_GetDevConfig(long lLoginID, unsigned long dwCommand, int nChannelNO, char * lpOutBuffer, unsigned long dwOutBufferSize, unsigned long* lpBytesReturned,int waittime = 1000);

C#: C#:

 [DllImport("NetSdk.dll")]
        public static extern int H264_DVR_GetDevConfig(int lLoginID, uint dwCommand, int nChannelNO, IntPtr lpOutBuffer, 
            uint dwOutBufferSize, ref uint lpBytesReturned, int waittime);

I'm calling this way: 我这样称呼:

C#: C#:

var vAlarmConfig = new SDK_ALARM_INPUTCONFIG();

        IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SDK_ALARM_INPUTCONFIG)));

        Marshal.StructureToPtr(vAlarmConfig, ptr, true);
        uint lpBytesReturned = (uint)Marshal.SizeOf(typeof(SDK_ALARM_INPUTCONFIG));
        int CHANNEL = 2;

        int result = XMSDK.H264_DVR_GetDevConfig(lLoginID, (uint)SDK_CONFIG_TYPE.E_SDK_CONFIG_ALARM_IN, CHANNEL, ptr,
            (uint)Marshal.SizeOf(typeof(SDK_ALARM_INPUTCONFIG)), ref lpBytesReturned, 10000);

Seems everything work fine, but when i run the program, the method return an illegal paramater. 似乎一切正常,但是当我运行程序时,该方法返回非法参数。

What i'm doing wrong?. 我在做什么错? I think the problem is the conversion. 我认为问题是转换。

Regards. 问候。

Your C++ bool is a single byte. 您的C ++ bool是一个字节。 But the C# bool default marshaling is as the 4 byte Winapi BOOL type. 但是C# bool 默认封送处理是4字节Winapi BOOL类型。 You'll need to add [MarshalAs(UnmanagedType.U1)] for every bool in your structs. 您需要为结构中的每个bool添加[MarshalAs(UnmanagedType.U1)]

Your use of ushort is wrong. 您使用ushort是错误的。 On the C++ side you have unsigned int . 在C ++方面,您已经对unsigned int进行了unsigned int That is uint on the C# side. 这是uint在C#的一面。

You will also need to translate the structs accurately. 您还需要准确翻译结构。 Perhaps while you were debugging you made pAlarmInfo have length 64 rather than 8. Obviously you need to go back an do that correctly. 也许在调试时,您使pAlarmInfo长度为64而不是8。显然,您需要返回正确的位置。

I guess you made that change because the struct size was incorrect. 我猜您是因为结构大小不正确而进行了此更改。 That should have been a tell-tale sign that there was a more serious problem. 这本来是一个说明性的信号,表明存在一个更严重的问题。 Your structs have to line up for each and every member. 您的结构必须为每个成员排队。 You can't just add a load of padding at the end to get the size right. 您不能只是在最后添加一堆填充来获得正确的大小。 If the sizes don't match then the layout will be wrong. 如果大小不匹配,则布局将是错误的。 You make the sizes match as a consequence of making the layouts match. 使尺寸匹配是使布局匹配的结果。 When the latter is done correctly, the former happens as a consequence. 如果正确完成了后者,则前者就会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM