473,320 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
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

Feb 15 '06 #1
0 1129

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion returns a pointer to a structure to its callback fucntion.The user should collect those...
1
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion returns a pointer to a structure to its callback fucntion.The user should collect those...
1
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion returns a pointer to a structure to its callback fucntion.The user should collect those...
4
by: FishingScout | last post by:
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.