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 4 1969 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
--
Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
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.
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
FishingScout wrote:
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
by: vijaya |
last post by:
I've to invoke a unmanaged dll fucntion in C# which uses
a callback fucntion.The unmanaged dll fucntion is as
follows
****************************************
The Original Fucntion in the dll...
|
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...
|
by: Juande |
last post by:
Hello,
I've a working application made with Visual Studio .Net 2003 and SQL Server
2000 SP3, this application runs on several pc stations with Windows XP SP2
installed.
When an user is using...
|
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...
| |
by: cada0310 |
last post by:
Hi there,
I'm new to C# (but not to C++), and I'm trying to create a web service that
calls some of our older software, located in a DLL. I'm getting most the
calls to work ok, but one of them...
|
by: Frav |
last post by:
The Reps team have been experiencing that Access 2002 unexpectedly
quits
while working and also lots of Corruption Failures and "Record lock
can not
update" messages since the upgrade from...
|
by: stillh2os |
last post by:
Hello.
I'm new to .NET, and I'm trying to implement a callback function. I want my managed C++ code to call an unmanaged function, passing in a callback function that the unmanaged C/C++ code...
|
by: =?Utf-8?B?RWR3YXJkUw==?= |
last post by:
I would greatly appreciate some help on passing managed object into unmanaged
code.
I need to pass a reference (address of) of a managed class into unmanaged
code (written by a thrid party). The...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |