开发者

How to pass structure from C# to ActiveX function created in C++ (MFC)

开发者 https://www.devze.com 2023-01-18 11:39 出处:网络
I\'ve strange problem: the MFC structure is #define SENSOR_DESC_LEN30 #define CAM_ID_LEN20 typedef struct

I've strange problem:

the MFC structure is

#define SENSOR_DESC_LEN   30
#define CAM_ID_LEN        20  
typedef struct
{
 unsigned int SensorType;
 char         Desc[SENSOR_DESC_LEN];
 unsigned int CommType;  // USB or TCP - COMM_TYPE
 float        FirmwareVersion;
 float        HardwareVersion;
 int          Width;
 int          Height;
 int          ActiveStartX;
 int          ActiveStartY;
 char         CameraID[CAM_ID_LEN];
 unsigned int pCam;
 char         Color;
} CAMERA_CAP_API; 

the corresponding C# structure is:

[StructLayout(LayoutKind.Sequential)]
  public struct CAMERA_CAP_API
  {
    public uint SensorType;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
    public byte[] Desc;
    public uint CommType;
    public float FirmwareVersion;
    public float HardwareVersion;
    public int Width;
    public int Height;
    public int ActiveStartX;
    public int ActiveStartY;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
    public byte[] CameraID;
    public uint pCam;
    public sbyte Color;
  } ;

it is correct - I can pass this structure through DLL using the following signature:

[System.Runtime.InteropServices.DllImport("Camelot.dll", EntryPoint = "CamGetCamCaps", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
    public static extern int CamGetCamCaps(int nCamNum, ref CAMERA_CAP_API CameraCap);

The problem is in ActiveX. (MFC)

I make the parameter as VARIANT. And it shows as object in C# side. however trying to pass the same structure to object fails:

object c = new CAMERA_CAP_API();
Camera1.GetCamCaps(ref c);  // Camera1 is the ActiveX control

I get the following error: An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: Value does not fall within the expected range.

I've also tr开发者_StackOverflowied:

IntPtr ptr = Marshal.AllocCoTaskMem(200);
GetNativeVariantForObject...
  • without success.

I get the same error.

What is the problem?

0

精彩评论

暂无评论...
验证码 换一张
取 消