M
Mingyi
Hi,
I have the following question regarding communicating with a OCX control
using C#. Idon't have access to the ocx code, so my c# code is actually mimic
the behavior of C++ counterparts. Basically, I have problem passing a
structure to a function.
Here is the C++ part code:
//****************************************
typedef struct
{
char TypeID;
char UserName[20];
char Password[20];
char UrlAddress[255];
int iStreamChannel;
bool bDirectDrawMode;
}CONNECT_DATA;
CONNECT_DATA m_ConnectData;
// after initializing m_ConnectData
int ret= InitConnectData((PBYTE)&m_ConnectData);
//****************************************
In C# when I drop this active control to my app, the interface for the
function
InitConnectData has be given as follows
InitConnectData(ref byte)
So the following is what I did in the C# part.
//****************************************
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi,Pack=8)]
public unsafe struct CONNECT_DATA
{
public byte TypeID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public string UserName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public string Password;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
public string UrlAddress;
public int iStreamChannel;
public bool bDirectDrawMode;
}
CONNECT_DATA connect_data = new CONNECT_DATA();
// After initializing all the fields
IntPtr pnt = Marshal.AllocCoTaskMem(Marshal.SizeOf(connect_data));
try
{
Marshal.StructureToPtr(connect_data, pnt, false);
byte[] buffer = new byte[Marshal.SizeOf(connect_data)];
Marshal.Copy(pnt, buffer, 0, Marshal.SizeOf(connect_data));
int ret = axNetworkCameraLink2.InitConnectData(ref buffer[0]);
}
//****************************************
But it seems that the function never gets the structure right, for it always
gives the wrong return value.
I tried to "Tlbimp" the original DLL, change the IL code so that the
"InitConnectData(uint8&)" becames "InitConnectData(native int)", and in C#
the interface becames "InitConnectData(System.IntPtr)", but error occurs,
stating that
"System.Runtime.InteropServices.COMException
(0x80020005): Type mismatch. at
System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags
flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)"
I guess it is because uint8 is 8 bit and native int is 32 bit...I really
don't know
what to do now, and I think I must do something wrong...Please help, thanks!
I have the following question regarding communicating with a OCX control
using C#. Idon't have access to the ocx code, so my c# code is actually mimic
the behavior of C++ counterparts. Basically, I have problem passing a
structure to a function.
Here is the C++ part code:
//****************************************
typedef struct
{
char TypeID;
char UserName[20];
char Password[20];
char UrlAddress[255];
int iStreamChannel;
bool bDirectDrawMode;
}CONNECT_DATA;
CONNECT_DATA m_ConnectData;
// after initializing m_ConnectData
int ret= InitConnectData((PBYTE)&m_ConnectData);
//****************************************
In C# when I drop this active control to my app, the interface for the
function
InitConnectData has be given as follows
InitConnectData(ref byte)
So the following is what I did in the C# part.
//****************************************
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi,Pack=8)]
public unsafe struct CONNECT_DATA
{
public byte TypeID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public string UserName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public string Password;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
public string UrlAddress;
public int iStreamChannel;
public bool bDirectDrawMode;
}
CONNECT_DATA connect_data = new CONNECT_DATA();
// After initializing all the fields
IntPtr pnt = Marshal.AllocCoTaskMem(Marshal.SizeOf(connect_data));
try
{
Marshal.StructureToPtr(connect_data, pnt, false);
byte[] buffer = new byte[Marshal.SizeOf(connect_data)];
Marshal.Copy(pnt, buffer, 0, Marshal.SizeOf(connect_data));
int ret = axNetworkCameraLink2.InitConnectData(ref buffer[0]);
}
//****************************************
But it seems that the function never gets the structure right, for it always
gives the wrong return value.
I tried to "Tlbimp" the original DLL, change the IL code so that the
"InitConnectData(uint8&)" becames "InitConnectData(native int)", and in C#
the interface becames "InitConnectData(System.IntPtr)", but error occurs,
stating that
"System.Runtime.InteropServices.COMException
(0x80020005): Type mismatch. at
System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags
flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)"
I guess it is because uint8 is 8 bit and native int is 32 bit...I really
don't know
what to do now, and I think I must do something wrong...Please help, thanks!