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

.Net技术 码拜 8年前 (2016-09-27) 2206次浏览
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#中该怎么写?

解决方案

10

10

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

80

再帮你把结构体用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;
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#怎么调用C++dll中带有回调函数的方法
喜欢 (0)
[1034331897@qq.com]
分享 (0)