title: callback function from unmanaged dll in VB .NET
body: <p>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:</p>
  
  <pre><code>&lt;DllImport("AlertMan.dll")&gt; _
  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) 
  </code></pre>
  
  <p>Original VB6 example code:</p>
  
  <pre><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)
  </code></pre>
  
  <p>Original dll header</p>
  
  <pre><code>typedef void TACBFUNC(char *);
  int AlertManC(TACBFUNC *WriteCaller cHANDLEPARM);
  </code></pre>