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(RFIDCallbackFuncti on)
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