C# 将C++结构转换为结构C 我有一个问题,把结构C++转换成C结构,我会留下下面的代码:
C++ C: 我将此方法称为: C++: C: 我这样叫: C: 看起来一切正常,但当我运行程序时,该方法返回非法参数 我做错了什么?。我认为问题在于转换 问候语< p>你的C++布尔是一个字节。但是C bool是4字节的Winapi bool类型。您需要为结构中的每个bool添加[MarshalAsUnmanagedType.U1] 你使用ushort是错误的。在C++方面,您有未签名的int。C端是uint。< /p> 您还需要准确地转换结构。也许在调试时,您使pAlarmInfo的长度为64而不是8。显然,你需要回到过去,正确地去做 我猜你做了这样的更改是因为结构大小不正确。这本应该是一个说明问题更严重的信号。您的结构必须为每个成员排列。你不能只是在末尾加上一堆填充物来调整尺寸。如果尺寸不匹配,则布局将出错。通过使布局匹配,可以使大小匹配。当后者正确地完成时,前者会发生。< P>你的C++布尔是一个字节。但是C bool是4字节的Winapi bool类型。您需要为结构中的每个bool添加[MarshalAsUnmanagedType.U1] 你使用ushort是错误的。在C++方面,您有未签名的int。C端是uint。< /p> 您还需要准确地转换结构。也许在调试时,您使pAlarmInfo的长度为64而不是8。显然,你需要回到过去,正确地去做C# 将C++结构转换为结构C 我有一个问题,把结构C++转换成C结构,我会留下下面的代码:,c#,c++,pinvoke,C#,C++,Pinvoke,C++ C: 我将此方法称为: C++: C: 我这样叫: C: 看起来一切正常,但当我运行程序时,该方法返回非法参数 我做错了什么?。我认为问题在于转换 问候语< p>你的C++布尔是一个字节。但是C bool是4字节的Winapi bool类型。您需要为结构中的每个bool添加[MarshalAsUnmanagedType.U1] 你使用ushort是错误的。在C++方面,您有未签名的int。C端是uint。< /p> 您还需要准确地转换结构。也许在调试时,您使pAlarmInfo的长度为6
我猜你做了这样的更改是因为结构大小不正确。这本应该是一个说明问题更严重的信号。您的结构必须为每个成员排列。你不能只是在末尾加上一堆填充物来调整尺寸。如果尺寸不匹配,则布局将出错。通过使布局匹配,可以使大小匹配。如果正确地执行了后一种操作,则会出现前一种情况。int的含义可能会有所不同,具体取决于您的C平台/编译器/设置。你需要从这里来理解它的含义。我从一个例子中得到代码,它们有INTC++作为INTC,我在C代码中证明了一些方法,除了这个之外,其他的都是有效的。@ Windows上的JoaaNehanWo.No在C和C++中是相同的。使用C结构类型而不是IntPtr来声明P/调用将更容易。然后,您可以让封送处理程序来完成这项工作,并且可以避免所有这些封送处理方法调用。int的含义可能会有所不同,具体取决于您的C平台/编译器/设置。你需要从这里来理解它的含义。我从一个例子中得到代码,它们有INTC++作为INTC,我在C代码中证明了一些方法,除了这个之外,其他的都是有效的。@ Windows上的JoaaNehanWo.No在C和C++中是相同的。使用C结构类型而不是IntPtr来声明P/调用将更容易。然后你可以让封送员来做这项工作,你可以避免所有的封送方法调用。好的,我会做这些更改,顺便说一下pAlarmInfo是64而不是8,我的错误。谢谢你的帮助,你是对的,我为uint更改了ushort,我做了一些小的更改,代码正在运行!!!:好的,我会修改的,顺便说一句,pAlarmInfo是64而不是8,我错了。谢谢你的帮助,你是对的,我为uint修改了ushort,我做了一些修改,代码正常!!!:
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;
};
[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;
};
long H264_DVR_GetDevConfig(long lLoginID, unsigned long dwCommand, int nChannelNO, char * lpOutBuffer, unsigned long dwOutBufferSize, unsigned long* lpBytesReturned,int waittime = 1000);
[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);
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);