日期:2014-05-17  浏览次数:20965 次

调用非托管代码:
dll原型:

typedef struct _DEVICE_LOCATION
 {
  ULONG DeviceId; //Device ID
  ULONG VendorId; //Vendor ID
  ULONG SubsystemId; //Subsystem ID
  ULONG RevisionId; //Revision ID
  CHAR DeviceCode[16]; //采集卡型号
 } DEVICE_LOCATION, *PDEVICE_LOCATION;


 UINT WINAPI lvGetDeviceInfo(PDEVICE_LOCATION Device, RETURN_CODE *rc);


调用:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  public struct DeviceLocation
  {
  public ulong DeviceId;
  public ulong VendorId;
  public ulong SubsystemId;
  public ulong RevisionId;
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  public char DeviceCode;
  }
 
  //查询PCI采集卡
  [DllImport("lvpci.dll", EntryPoint = "_lvGetDeviceInfo@8", CharSet = CharSet.Auto)]
  public static extern int lvGetDeviceInfo(DeviceLocation dl, ref ReturnCode.returnCode rc);


在我调用lvGetDeviceInfo时,出现这样的错误:
  [System.TypeLoadException] = {"无法封送处理类型为“DeviceLocation”的字段“DeviceCode”: 无效的托管/非托管类型组合(字符必须与 I1、I2、U1 或 U2 成对出现)。":""}

求高手解答!!

------解决方案--------------------
public char DeviceCode;
改为
 public char[] DeviceCode;