C#和C++传递回调 我在调用C++的DLL,要设置一个回掉函数;我使用的时候,单独窗体的时候没问题;但当我打开两个以上窗体时,发现回掉通知窗体消息时,有些混乱;谁知道问题在那的? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 您需要注意几点1。回掉的窗体句柄handle2.消息的类型3.参数您说的问题应该是第一种 回掉函数没有需要句柄handle,C++的DLL中有个void*类型的参数,对应到C#这边传递个委托过去,这个时候及没得句炳、消息类型和参数设置这么说是DLL有问题了? public class Channel { public UInt32 Handler; //设置回调函数 private EsrFunc esr; public Channel() { this.esr = new EsrFunc(ESR); } /// <summary> /// 消息接受函数 /// </summary> public static void ESR(uint esrParam) { TBarMessage.LogAdd("收到事件" + System.DateTime.Now.ToString("yyyyMMdd mm:hh:ss") + "--" + esrParam.ToString()); } /// <summary> /// 建立连接 /// </summary> public int OpenStream() { int iState; iState = CommLib.OpenStream(ref Handler); if (iState == 0) { this.SetESR(Handler); } return iState; } /// <summary> /// 设置回调 /// </summary> public void SetESR(uint esrParam) { if (this.esr != null) { int iState = CommLib.SetESR(Handler, this.esr, esrParam); return iState; } } } //自定义委托 [UnmanagedFunctionPointer(CallingConvention.Winapi)] public delegate void EsrFunc(uint esrParam); public static class CommLib { [DllImport("ApiSelf.dll", CharSet = CharSet.Ansi, EntryPoint = "OpenStream")] public static extern int OpenStream(ref uint Handle); [DllImport("ApiSelf.dll", CharSet = CharSet.Ansi, EntryPoint = "SetESR")] public static extern int SetESR(uint acsHandle, EsrFunc esr, uint esrParam); }这是一部分代码C++typedef void (*EsrFunc) (unsigned long esrParam);typedef unsigned long Handle_t;RetCode_t __stdcall OpenStream (Handle_t *acsHandle);RetCode_t __stdcall acsSetESR ( Handle_t acsHandle, EsrFunc esr, unsigned long esrParam); 大家帮我看看,下面的代码怎么错了啊? 在线等!!! 单元测试执行后,有没有什么好方法将测试数据删除 关于使用正则表达式的初级问题 winform怎么实现文字的上下滚动 关于打印,郁闷啊 老问题:请教打包程序中.NET框架的安装 object转换ToString()报错 使用Tessnet2_32.dll但是老是报错 在向服务器发送请求时发生传输级错误。 (provider: 共享内存提供程序, error: 0 - Shared Memory Provider: ) [关注]这也太暴力了吧? 线程问题 怎样避免两个TextBox的TextChanged事件相互触发?
1。回掉的窗体句柄handle
2.消息的类型
3.参数您说的问题应该是第一种
回掉函数没有需要句柄handle,C++的DLL中有个void*类型的参数,对应到C#这边传递个委托过去,这个时候及没得句炳、消息类型和参数设置这么说是DLL有问题了?
{
public UInt32 Handler; //设置回调函数
private EsrFunc esr; public Channel()
{
this.esr = new EsrFunc(ESR);
} /// <summary>
/// 消息接受函数
/// </summary>
public static void ESR(uint esrParam)
{
TBarMessage.LogAdd("收到事件" + System.DateTime.Now.ToString("yyyyMMdd mm:hh:ss") + "--" + esrParam.ToString());
}
/// <summary>
/// 建立连接
/// </summary>
public int OpenStream()
{
int iState; iState = CommLib.OpenStream(ref Handler); if (iState == 0)
{
this.SetESR(Handler);
}
return iState;
} /// <summary>
/// 设置回调
/// </summary>
public void SetESR(uint esrParam)
{
if (this.esr != null)
{
int iState = CommLib.SetESR(Handler, this.esr, esrParam);
return iState;
}
}
} //自定义委托
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate void EsrFunc(uint esrParam); public static class CommLib
{
[DllImport("ApiSelf.dll", CharSet = CharSet.Ansi, EntryPoint = "OpenStream")]
public static extern int OpenStream(ref uint Handle);
[DllImport("ApiSelf.dll", CharSet = CharSet.Ansi, EntryPoint = "SetESR")]
public static extern int SetESR(uint acsHandle, EsrFunc esr, uint esrParam);
}这是一部分代码
C++typedef void (*EsrFunc) (unsigned long esrParam);typedef unsigned long Handle_t;
RetCode_t __stdcall OpenStream (Handle_t *acsHandle);
RetCode_t __stdcall acsSetESR (
Handle_t acsHandle,
EsrFunc esr,
unsigned long esrParam);