Unmanaged DLL Callback - Program Unexpectedly Quits

F

FishingScout

I am re-writing an MS VC++ 6.0 application in Visual Studio 2005
VB.NET. In order for my new application to communicate with some
hardware (an RFID reader) I need to communicate with a DLL that was
written in MS VC++ 6.0. I have found some excellent discussions that
have helped me define the structures to marshal the data between the
unmanaged and managed code. My problem is that my application is not
working correctly. The result is that the program suddenly quits
without any indication of why.

The flow of the code is this: Initialize the DLL, Ask the DLL to
discover all available RFID readers, The DLL will then "callback" my
application when anything significant occurs (such as an RFID tag is
read).

I wrapped the DLL in a class called RFIDReaderClass. To reduce
bandwidth I have only included a partial class definition here. I can
call all the methods of the DLL without any errors.

My test application is a form with a list view. Whenever a callback is
made the list view is updated with some information using an
"Invoke" call to a method that adds an item to the list view. When
I run the application, in the debugger, I can see a few messages
displayed in the list view then suddenly the program goes away. No
error messages are displayed and I am able to "start debugging"
again.

I suspect that this may be a garbage collection issue but I am stumped.
I have tried KeepAlive and GCHandle.Alloc without success, but I am
probably making the calls incorrectly.

By the way, if I don't register the callback using
RFIDRegisterCallback method of the DLL then the program doesn't
disappear and seems to run correctly. Unfortunately I need to register
the callback in order to do any useful processing in my application.

Here is the RFIDReaderClass.vb

----------------------------------------------------------------------------------------

Imports System.Runtime.InteropServices

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure RFIDInitStructure
Public MaximumNumberOfReaders As Integer
Public TagSerialNumberLength As Integer
Public TagMemorySize As Integer
Public BoardInfoFile As String
End Structure

<StructLayout(LayoutKind.Sequential)> _
Public Structure ReaderLocationStructure
Public ReaderNumber As Integer
Public ChipIndex As Integer
End Structure

<StructLayout(LayoutKind.Sequential)> _
Public Structure ReaderLocationArray
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=500)> _
Public vals As ReaderLocationStructure()
End Structure

<StructLayout(LayoutKind.Sequential)> _
Public Structure TagSerialNumberStructure
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=9)> _
Public Bytes As System.Byte()
End Structure

<StructLayout(LayoutKind.Sequential)> _
Public Structure TagDataStructure
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=1025)> _
Public Bytes As System.Byte()
End Structure


Public Delegate Sub CallbackDelegate( _
ByVal ReaderLocation As ReaderLocationStructure, _
ByVal ReaderStatus As Integer, _
ByVal TagSerialNumber As TagSerialNumberStructure, _
ByVal TagData As TagDataStructure)


Public Class RFIDReaderClass

Public Declare Function RFIDRegisterCallback Lib "rfidreader.dll" _
(ByVal AddressOfSub As CallbackDelegate) As Integer

Public Declare Function RFIDDllInitialize Lib "rfidreader.dll" _
(ByRef InitData As RFIDInitStructure) As Integer

Public Declare Function RFIDDiscoverReaders Lib "rfidreader.dll" _
(ByRef ReaderLocations As ReaderLocationArray, _
ByRef NumberOfReadersFound As Integer) As Integer

End Class


----------------------------------------------------------------------------------------

Here is the Form1.vb

----------------------------------------------------------------------------------------

Imports System.Runtime.InteropServices

Public Class Form1

Dim RFIDReader As RFIDReaderClass
Dim RFIDCallbackFunction As New CallbackDelegate(AddressOf
RFIDReaderCallback)

Dim NumberOfReadersFound As Integer
Dim ReaderLocations As ReaderLocationArray

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim InitData As RFIDInitStructure

InitData.MaximumNumberOfReaders = 111
InitData.TagMemorySize = 1024
InitData.TagSerialNumberLength = 8
InitData.BoardInfoFile = "c:/NTcommgr.csv"

Dim Result As Integer

Result = RFIDReader.RFIDRegisterCallback(RFIDCallbackFunction)

Result = RFIDReader.RFIDDllInitialize(InitData)

Result = RFIDReader.RFIDDiscoverReaders(ReaderLocations,
NumberOfReadersFound)

End Sub

Private Sub RFIDReaderCallback(ByVal ReaderLocation As
ReaderLocationStructure, _
ByVal ReaderStatus As Integer, _
ByVal TagSerialNumber As TagSerialNumberStructure, _
ByVal TagData As TagDataStructure)

Dim d As New updateLabel(AddressOf updateLabelHandler)
Me.Invoke(d, New Object() {ReaderLocation, ReaderStatus,
TagSerialNumber, TagData})

End Sub

Private Delegate Sub updateLabel(ByVal ReaderLocation As
ReaderLocationStructure, _
ByVal ReaderStatus As Integer, _
ByVal TagSerialNumber As TagSerialNumberStructure, _
ByVal TagData As TagDataStructure)

Private Sub updateLabelHandler(ByVal ReaderLocation As
ReaderLocationStructure, _
ByVal ReaderStatus As Integer, _
ByVal TagSerialNumber As TagSerialNumberStructure, _
ByVal TagData As TagDataStructure)
Dim DisplayString As String

DisplayString = Now.ToShortTimeString() & " Slot " & _
ReaderLocation.ReaderNumber.ToString() & _
" Index " & ReaderLocation.ChipIndex.ToString() & _
" Status " & ReaderStatus.ToString()

DisplayListBox.Items.Add(DisplayString)
End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
 
M

Mattias Sjögren

Public Delegate Sub CallbackDelegate( _
ByVal ReaderLocation As ReaderLocationStructure, _
ByVal ReaderStatus As Integer, _
ByVal TagSerialNumber As TagSerialNumberStructure, _
ByVal TagData As TagDataStructure)

Are you sure the structs are to be passed ByVal?



Mattias
 
F

FishingScout

Actually I am not sure that they should be passed ByVal.
I don't know how to determine which members of an un-
managed callback should be ByVal or ByRef.

There are three successful callbacks which get displayed
in the listbox before the program vanished. I assume that
this would indicate the marshalling is okay, but I tried a
few quick tests ...

I changed them to all ByRef and an exception is caught
and displayed by the DLL. I also tried changing just the
TagSerialNumber and TagData to ByRef and the DLL again
caught and displayed an exception.

I wonder if the program is vanishing, without exception and
without description, because the delegate or the
RFIDReaderClass itself is being garbage collected?

I don't know how to ensure that the delegate doesn't get
garbage collected.
 
R

R. MacDonald

Hello, FishingScout,

Re: "...the program suddenly quits without any indication of why."

I had a similar problem when using a Fortran DLL that was doing
callbacks into a VB.Net host. The callbacks seemed to be working, and
then the application would silently exit -- very frustrating. I looked
at the callbacks for a long time without making any progress because the
problem turned out to be unrelated to the callback. (It was caused by
the DLL trying to output an error message to the "standard error device"
which hadn't been created for the DLL.)

Your case is probably different, but I wonder if the problem is
(likewise) somewhere other than the callback mechanism itself.

Re:
I don't know how to ensure that the delegate doesn't get
garbage collected.

As long as a reference to the delegate remains "in scope" during the
processing then the delegate shouldn't be garbage collected. I keep it
as a module scope variable within the class that contains both the
invocation of the DLL and the callback procedure, and (so far) haven't
had any problem with this.

But I DO worry about what happens if garbage collection of other things
causes the object containing the callback procedure to be moved between
the invocation and completion of the DLL. It seems that it is not
possible to "pin" either the delegate or the object. My understanding
of how .Net (and these delegates in particular) operate is inadequate to
give me confidence that this will NOT be a problem.

Please let us know what the solution is when you find it.

Cheers,
Randy
 
F

FishingScout

Randy,

Great thoughts. I just stumbled onto the concept of pinning the
delegate because of a C++ article that discussed pin_ptr. I was
searching for something similar in vb.net.

Thanks, I will post whatever solution ends up working ( if any do ).

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top