VB.NET中的非托管DLL的回调函数 [英] callback function from unmanaged dll in VB .NET

查看:327
本文介绍了VB.NET中的非托管DLL的回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在VB.NET中使用非托管DLL。 DLL中提供的示例源代码在VB6中,下面是我将其转换为.NET的尝试。当dll尝试做回调时,我得到一个尝试读或写保护内存异常。我真的不在乎实际调用的回调函数。
我的代码:

 < DllImport(AlertMan.dll)> _ 
公共共享函数AlertManC(_
ByVal CallbackAddr As AlertManCallbackDel)As Long
结束函数

公共委托Sub AlertManCallbackDel(ByVal data As Long)

Public Sub AlertManCallback(ByVal data As Long)
End Sub

公共mydel作为新的AlertManCallbackDel(AddressOf AlertManCallback)
'protected memeory exception here
Dim IStat as Long = AlertManC(mydel)

原始VB6示例代码:

 声明函数AlertManC _ 
LibAlertMan.dll_
别名AlertManC(ByVal CallbackAddr As Long)As Long

Private Sub AlertManCallback(ByVal data As Long)
End Sub

'调用代码
Dim IStat As Long
IStat = AlertManC(AddressOf AlertManCallBack)

原始dll标题

  typedef void TACBFUNC(char *); 
int AlertManC(TACBFUNC * WriteCaller cHANDLEPARM);


解决方案

你可以发布AlertManC的原始定义吗?



我的猜测是,回调函数的数据参数实际上是一个整数与一个长。在VB6中,我相信Long的实际上只有32位,而VB.Net是64位。尝试这个

 < DllImport(AlertMan.dll)> _ 
公共共享函数AlertManC(ByVal CallbackAddr As AlertManCallbackDel)As Long
结束函数

公共委派Sub AlertManCallbackDel(ByVal数据作为IntPtr)


Public Sub AlertManCallback(ByVal data As IntPtr)
End Sub

编辑



我根据您发布的本机签名更新了代码。你能试试吗?


I'm trying to use an unmanaged dll in VB.NET. The example source code provided with the dll is in VB6 and below is my attempt to convert it to .NET. When the dll tries to do a callback I get a "Attempted to read or write protected memory" exception. I really don't care about the callback function getting actually called. My code:

<DllImport("AlertMan.dll")> _
Public Shared Function AlertManC( _
    ByVal CallbackAddr As AlertManCallbackDel) As Long
End Function

Public Delegate Sub AlertManCallbackDel(ByVal data As Long)

Public Sub AlertManCallback(ByVal data As Long)       
End Sub

Public mydel As New AlertManCallbackDel(AddressOf AlertManCallback)
'protected memeory exception here
Dim IStat as Long = AlertManC(mydel) 

Original VB6 example code:

Declare Function AlertManC _
    Lib "AlertMan.dll" _
    Alias "AlertManC" (ByVal CallbackAddr As Long) As Long

Private Sub AlertManCallback(ByVal data As Long)
End Sub

' calling code
Dim IStat As Long
IStat = AlertManC(AddressOf AlertManCallBack)

Original dll header

typedef void TACBFUNC(char *);
int AlertManC(TACBFUNC *WriteCaller cHANDLEPARM);

解决方案

Can you post the original native definiton for AlertManC?

My guess though is that the data parameter of the callback function is actually an Integer vs. a Long. In VB6 I believe Long's were actually only 32 bits vs. VB.Net where they are 64 bit. Try this

   <DllImport("AlertMan.dll")> _
   Public Shared Function AlertManC(ByVal CallbackAddr As AlertManCallbackDel) As Long
   End Function

   Public Delegate Sub AlertManCallbackDel(ByVal data As IntPtr)


   Public Sub AlertManCallback(ByVal data As IntPtr)       
   End Sub

Edit

I updated the code based on the native signature you posted. Can you try this out?

这篇关于VB.NET中的非托管DLL的回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆