我有一个奇怪的问题:
MFC结构
#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;
相应的C#结构是:
[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;
} ;
正确 - 我可以使用以下签名通过DLL传递此结构:
[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);
问题出在ActiveX中。 (MFC)
我将参数设为VARIANT。 它在C#方面显示为对象。 但是尝试将相同的结构传递给对象失败:
object c = new CAMERA_CAP_API();
Camera1.GetCamCaps(ref c); // Camera1 is the ActiveX control
我收到以下错误: mscorlib.dll
中发生了未处理的“System.ArgumentException”类型异常其他信息:价值不在预期范围内。
我也试过了:
IntPtr ptr = Marshal.AllocCoTaskMem(200);
GetNativeVariantForObject...
我得到了同样的错误。
有什么问题?