Unmanaged DLL Callback - Program Unexpectantly Quits

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FishingScout

    #1

    Unmanaged DLL Callback - Program Unexpectantly Quits

    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
    RFIDRegisterCal lback 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(L ayoutKind.Seque ntial, CharSet:=CharSe t.Ansi)> _
    Public Structure RFIDInitStructu re
    Public MaximumNumberOf Readers As Integer
    Public TagSerialNumber Length As Integer
    Public TagMemorySize As Integer
    Public BoardInfoFile As String
    End Structure

    <StructLayout(L ayoutKind.Seque ntial)> _
    Public Structure ReaderLocationS tructure
    Public ReaderNumber As Integer
    Public ChipIndex As Integer
    End Structure

    <StructLayout(L ayoutKind.Seque ntial)> _
    Public Structure ReaderLocationA rray
    <MarshalAs(Unma nagedType.ByVal Array, SizeConst:=500) > _
    Public vals As ReaderLocationS tructure()
    End Structure

    <StructLayout(L ayoutKind.Seque ntial)> _
    Public Structure TagSerialNumber Structure
    <MarshalAs(Unma nagedType.ByVal Array, SizeConst:=9)> _
    Public Bytes As System.Byte()
    End Structure

    <StructLayout(L ayoutKind.Seque ntial)> _
    Public Structure TagDataStructur e
    <MarshalAs(Unma nagedType.ByVal Array, SizeConst:=1025 )> _
    Public Bytes As System.Byte()
    End Structure


    Public Delegate Sub CallbackDelegat e( _
    ByVal ReaderLocation As ReaderLocationS tructure, _
    ByVal ReaderStatus As Integer, _
    ByVal TagSerialNumber As TagSerialNumber Structure, _
    ByVal TagData As TagDataStructur e)


    Public Class RFIDReaderClass

    Public Declare Function RFIDRegisterCal lback Lib "rfidreader.dll " _
    (ByVal AddressOfSub As CallbackDelegat e) As Integer

    Public Declare Function RFIDDllInitiali ze Lib "rfidreader.dll " _
    (ByRef InitData As RFIDInitStructu re) As Integer

    Public Declare Function RFIDDiscoverRea ders Lib "rfidreader.dll " _
    (ByRef ReaderLocations As ReaderLocationA rray, _
    ByRef NumberOfReaders Found As Integer) As Integer

    End Class


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

    Here is the Form1.vb

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

    Imports System.Runtime. InteropServices

    Public Class Form1

    Dim RFIDReader As RFIDReaderClass
    Dim RFIDCallbackFun ction As New CallbackDelegat e(AddressOf
    RFIDReaderCallb ack)

    Dim NumberOfReaders Found As Integer
    Dim ReaderLocations As ReaderLocationA rray

    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load

    Dim InitData As RFIDInitStructu re

    InitData.Maximu mNumberOfReader s = 111
    InitData.TagMem orySize = 1024
    InitData.TagSer ialNumberLength = 8
    InitData.BoardI nfoFile = "c:/NTcommgr.csv"

    Dim Result As Integer

    Result = RFIDReader.RFID RegisterCallbac k(RFIDCallbackF unction)

    Result = RFIDReader.RFID DllInitialize(I nitData)

    Result = RFIDReader.RFID DiscoverReaders (ReaderLocation s,
    NumberOfReaders Found)

    End Sub

    Private Sub RFIDReaderCallb ack(ByVal ReaderLocation As
    ReaderLocationS tructure, _
    ByVal ReaderStatus As Integer, _
    ByVal TagSerialNumber As TagSerialNumber Structure, _
    ByVal TagData As TagDataStructur e)

    Dim d As New updateLabel(Add ressOf updateLabelHand ler)
    Me.Invoke(d, New Object() {ReaderLocation , ReaderStatus,
    TagSerialNumber , TagData})

    End Sub

    Private Delegate Sub updateLabel(ByV al ReaderLocation As
    ReaderLocationS tructure, _
    ByVal ReaderStatus As Integer, _
    ByVal TagSerialNumber As TagSerialNumber Structure, _
    ByVal TagData As TagDataStructur e)

    Private Sub updateLabelHand ler(ByVal ReaderLocation As
    ReaderLocationS tructure, _
    ByVal ReaderStatus As Integer, _
    ByVal TagSerialNumber As TagSerialNumber Structure, _
    ByVal TagData As TagDataStructur e)
    Dim DisplayString As String

    DisplayString = Now.ToShortTime String() & " Slot " & _
    ReaderLocation. ReaderNumber.To String() & _
    " Index " & ReaderLocation. ChipIndex.ToStr ing() & _
    " Status " & ReaderStatus.To String()

    DisplayListBox. Items.Add(Displ ayString)
    End Sub

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

Working...