thank you Idael,your article and code really helped me!!
but my trouble doesn't disappear....:(
the struct i used is more complex,but i think it's the same.the code is
following.
now ,it doesn't throw exception,and can pass in the function well,but the
function still
return false,i am sure the dll which contains the function is fine,because
i've tested it
in vc++.
-------Unmanaged
Code------------------------------------------------------------------
typedef struct
{
char m_username[16];
char m_password[8];
char m_userIP[16];
}USERPARA,*PUSERPARA;
typedef struct{
char m_starthour;
char m_startmin;
char m_stophour;
char m_stopmin;
}RECORDTIME,*PRECORDTIME;
typedef struct{
ULONG m_alarmsenstive;
ULONG m_detect[18][22];
}DETECTSCOPE,*PDETECTSCOPE;
typedef struct{
RECORDTIME m_recordtime[7][4];
ULONG m_workmode[7][4];
BOOL m_bAllDayRecord[7];
}RECORDPLAN,*PRECORDPLAN;
typedef struct
{
BYTE m_channelName[USER_LEN];
ULONG m_streamType;
ULONG m_encodeType;
ULONG m_videobitrate;
ULONG m_videoFrameRate;
ULONG m_picturequality;
ULONG m_bitratetype;
ULONG m_Iinterval;
ULONG m_brightness;
ULONG m_contrast;
ULONG m_tonality;
ULONG m_saturation;
BOOL m_bRecord;
RECORDPLAN m_recordplan;
DETECTSCOPE m_detectscope;
BOOL m_bSetcommonuser;
ULONG m_delay;
BOOL m_bOsd;
BOOL m_bLogo;
USERPARA m_userpara[6];
ULONG m_userdefinedrate;
}CHANNELPARA,*PCHANNELPARA;
-------------------------------------------------------------------------
--------------Managed
Code-----------------------------------------------------------
[ StructLayout( LayoutKind.Sequential)]
public class USERPARA
{
[MarshalAs(UnmanagedType.ByValArray,SizeConst=GlobalDefine.USER_LEN)]
public char[] m_username=new char[GlobalDefine.USER_LEN];
[MarshalAs(UnmanagedType.ByValArray,SizeConst=8)]
public char[] m_password=new char[8];
[MarshalAs(UnmanagedType.ByValArray,SizeConst=16)]
public char[] m_userIP=new char[16];
}
[ StructLayout( LayoutKind.Sequential)]
public class UserParaList
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6*8)]
private byte[] Data;
public USERPARA this [int index]
{
get
{
if (index<0||index>6)
{
throw new IndexOutOfRangeException();
}
USERPARA res;
GCHandle handle = GCHandle.Alloc(Data, GCHandleType.Pinned);
try
{
IntPtr buffer = handle.AddrOfPinnedObject();
buffer = (IntPtr)(buffer.ToInt32() +
(index*Marshal.SizeOf(typeof(USERPARA))));
res = (USERPARA)Marshal.PtrToStructure(buffer, typeof(USERPARA));
}
finally
{
handle.Free();
}
return res;
}
}
public UserParaList()
{
Data = new byte[6*Marshal.SizeOf(typeof(USERPARA))];
}
}
[ StructLayout( LayoutKind.Sequential)]
public class DETECTSCOPE
{
public int m_alarmsenstive;
[MarshalAs(UnmanagedType.ByValArray,SizeConst=396)]
public int[] m_detect=new int[396];
}
[ StructLayout( LayoutKind.Sequential)]
public class RECORDTIME
{
public char m_starthour;
public char m_startmin;
public char m_stophour;
public char m_stopmin;
}
[ StructLayout( LayoutKind.Sequential)]
public class RecordTimeList2
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=28*8)]
private byte[] Data;
public RECORDTIME this [int x,int y]
{
get
{
if ( (x < 0) | (y < 0)| (x >= 7)| (y >= 4))
{
throw new IndexOutOfRangeException();
}
RECORDTIME res;
GCHandle handle = GCHandle.Alloc(Data, GCHandleType.Pinned);
try
{
IntPtr buffer = handle.AddrOfPinnedObject();
buffer = (IntPtr)(buffer.ToInt32() +
((x*4+y)*Marshal.SizeOf(typeof(RECORDTIME))));
res = (RECORDTIME)Marshal.PtrToStructure(buffer, typeof(RECORDTIME));
}
finally
{
handle.Free();
}
return res;
}
}
public RecordTimeList2()
{
Data = new byte[28*Marshal.SizeOf(typeof(RECORDTIME))];
}
}
[ StructLayout( LayoutKind.Sequential)]
public class RECORDPLAN
{
public RecordTimeList2 m_recordtime=new RecordTimeList2();
[MarshalAs(UnmanagedType.ByValArray,SizeConst=28)]
public int[] m_workmode=new int[28];
[MarshalAs(UnmanagedType.ByValArray,SizeConst=7)]
public bool[] m_bAllDayRecord=new bool[7];
}
[ StructLayout( LayoutKind.Sequential)]
public class CHANNELPARA
{
[MarshalAs(UnmanagedType.ByValArray,SizeConst=GlobalDefine.USER_LEN)]
public byte[] m_channelName=new byte[GlobalDefine.USER_LEN];
public int m_streamType;
public int m_encodeType;
public int m_videobitrate;
public int m_videoFrameRate;
public int m_picturequality;
public int m_bitratetype;
public int m_Iinterval;
public int m_brightness;
public int m_contrast;
public int m_tonality;
public int m_saturation;
public bool m_bRecord;
public RECORDPLAN m_recordplan= new RECORDPLAN();
public DETECTSCOPE m_detectscope=new DETECTSCOPE();
public bool m_bSetcommonuser; public int m_delay;
public bool m_bOsd;
public bool m_bLogo;
public UserParaList m_userpara =new UserParaList();
public int m_userdefinedrate;
}
-------------------------------------------------------------------------
Hi Idael,
I made some mistake in my last post,so the function returns false.And now
I've solved it using your solution!
Thank you!!!!
Regards,
chaor