C#怎么调用C++dll中带有回调函数的方法

时间:2022-11-06 19:01:06
C++中的函数为
int WINAPI ecwOpenCallback( ECW_CALLBACK fCallback,
const char *szSvrIP=NULL,
long nPort = 7901,
const char *szAppName=NULL);

回调函数声明如下
typedef long (__stdcall *ECW_CALLBACK)(WORD wEvent, WORD wIndex,const
LPECWATCHDATA lpecwData);

LPECWATCHDATA的定义为
typedef struct {
 WORD     nState;               //状态
 WORD     nIdx;                 //索引
 WORD     wDataCnt;             //数据(lpData)项数  
 char     szTime[20];           //时间
 char* lpIDString;     //设备号码
 char* lpNameString;   //设备/用户名称
 char* lpData[8];
}ECWATCHDATA,*LPECWATCHDATA;


我在C#中该怎么写?

4 个解决方案

#2


在c++/c中是回调,在c#中 对应的是委托
根据c++定义的类型对应定义 就可以了

#3



typedef long (__stdcall *ECW_CALLBACK)(WORD wEvent, WORD wIndex,const
LPECWATCHDATA lpecwData);

对应C#委托

[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)]
public delegate int ECW_CALLBACK(ushort wEvent, ushort wIndex, ref ECWATCHDATA lpecwData);

#4


再帮你把结构体用C#定义出来吧

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
public struct ECWATCHDATA {
    
    /// WORD->unsigned short
    public ushort nState;
    
    /// WORD->unsigned short
    public ushort nIdx;
    
    /// WORD->unsigned short
    public ushort wDataCnt;
    
    /// char[20]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=20)]
    public string szTime;
    
    /// char*
    public System.IntPtr lpIDString;
    
    /// char*
    public System.IntPtr lpNameString;
    
    /// char*[8]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=8, ArraySubType=System.Runtime.InteropServices.UnmanagedType.SysUInt)]
    public System.IntPtr[] lpData;
}

#1


#2


在c++/c中是回调,在c#中 对应的是委托
根据c++定义的类型对应定义 就可以了

#3



typedef long (__stdcall *ECW_CALLBACK)(WORD wEvent, WORD wIndex,const
LPECWATCHDATA lpecwData);

对应C#委托

[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)]
public delegate int ECW_CALLBACK(ushort wEvent, ushort wIndex, ref ECWATCHDATA lpecwData);

#4


再帮你把结构体用C#定义出来吧

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
public struct ECWATCHDATA {
    
    /// WORD->unsigned short
    public ushort nState;
    
    /// WORD->unsigned short
    public ushort nIdx;
    
    /// WORD->unsigned short
    public ushort wDataCnt;
    
    /// char[20]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=20)]
    public string szTime;
    
    /// char*
    public System.IntPtr lpIDString;
    
    /// char*
    public System.IntPtr lpNameString;
    
    /// char*[8]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=8, ArraySubType=System.Runtime.InteropServices.UnmanagedType.SysUInt)]
    public System.IntPtr[] lpData;
}