日期:2014-05-18  浏览次数:20916 次

C#调用设置dll回调函数出错。
c++原型
typedef struct{
  char m_cIP[16];m_cIP
  char m_cMAC[18];m_cMAC
  char m_cMask[16];m_cMask
  char m_Gateway[16];m_Gateway
  char m_cDNS[16];m_cDNS
  int m_iChanNum;m_iChanNum
  int m_iServerPort;m_iServerPort
  int m_iClientPort;m_iClientPort
  int m_iDeviceType;m_iDeviceType
  int m_iHttpPort;m_iHttpPort
  int m_iProductModel;m_iProductModel
  char m_cFactoryID[64];m_cFactoryID
  char m_cKernelVersion[64];m_cKernelVersion
  char m_cOCXVersion[64];m_cOCXVersion
 }TDEVICE, *PTDEVICE;

设置回调函数
int __stdcall NetAdmin_SetNotify(

  FIND_NOTIFY _Notify_Notify
  回调函数

 );

typedef void (

  PTDEVICE _pDevice_pDevice
  [OUT]指向设备信息

 );

改写后
  [StructLayout(LayoutKind.Sequential)]
  struct TDEVICE
  {  
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  public Char[] m_cIP;
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 18)]
  public Char[] m_cMAC;
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  public Char[] m_cMask;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  public Char[] m_cGateway;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  public Char[] m_cDNS;
  public UInt32 m_iChanNum;
  public UInt32 m_iServerPort;
  public UInt32 m_iClientPort;
  public UInt32 m_iDeviceType;
  public UInt32 m_iHttpPort;
  public UInt32 m_iProductModel;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
  public Char[] m_cFactoryID;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
  public Char[] m_cKernelVersion;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
  public Char[] m_cOCXVersion;  
  };

delegate void NotifyCallBack([MarshalAs(UnmanagedType.LPStruct)]IntPtr _Device);

  [DllImport("NetAdmin.dll", CallingConvention = CallingConvention.StdCall)]
  public static extern int NetAdmin_SetNotify(NotifyCallBack callback);

执行后出错提示
无法封送处理“parameter #1”: 无效的托管/非托管类型组合(Int/UInt 必须与 SysInt 或 SysUInt 成对出现)。

------解决方案--------------------
C++的int对应的不是C#的int吗
------解决方案--------------------
里面所有的 Char[] 改成 byte[] ,
delegate void NotifyCallBack([MarshalAs(UnmanagedType.LPStruct)]IntPtr _Device);
改成
delegate void NotifyCallBack(out TDEVICE _Device);
这样试下