我想在VB.NET中使用非託管的DLL。隨DLL提供的示例源代碼在VB6中,下面是我將它轉換爲.NET的嘗試。當DLL試圖做回調時,我得到一個「試圖讀取或寫入受保護的內存」異常。我真的不關心實際調用的回調函數。 我的代碼:在VB .NET中的非託管DLL的回調函數
<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)
原VB6的示例代碼:
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)
原始DLL頭
typedef void TACBFUNC(char *);
int AlertManC(TACBFUNC *WriteCaller cHANDLEPARM);
謝謝,你搖滾!將長變爲整數是問題!你的原始解決方案也會有效。實際上,爲了簡潔起見,我刪除了另外7個參數(我對此感到羞恥)。把它們切換到整數是個訣竅。 – Michael 2009-02-16 19:16:19