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 4 1987 Public Delegate Sub CallbackDelegat e( _ ByVal ReaderLocation As ReaderLocationS tructure, _ ByVal ReaderStatus As Integer, _ ByVal TagSerialNumber As TagSerialNumber Structure, _ ByVal TagData As TagDataStructur e)
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 structure fields in a buffer.
In my managed code(i.e. in C# program), I've used a
delegate for invoking callback function and I've declared
the structure too. The dll fucntion is executing finely
without any errors but I'm not getting any values...
|
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
****************************************
int PBORCA_LibraryDirectory ( HPBORCA hORCASession, LPSTR
lpszLibName, LPSTR lpszLibComments, int iCmntsBuffSize,
PBORCA_LISTPROC pListProc, LPVOID pUserData );
|
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 structure fields in a buffer.
In my managed code(i.e. in C# program), I've used a
delegate for invoking callback function and I've declared
the structure too. The dll fucntion is executing finely
without any errors but I'm not getting any values...
|
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 the application, it's quits unexpectedly without any
previous advice or error message, I'm trying solve this problem for a long
time but I cannot find a solution.
Please, Anybody had a similar expirience and finally got solve it?, please
let me...
|
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 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...
| |
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 needs a callback into the managed code. I
thought I had it all worked out, but it seems like when the callback is
getting hit, things might be going out of scope and crashing the app.
Here's my little web service function; I hacked out a bunch...
|
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 Windows 2000 and Access 200 to
Windows XP and Access 2002 (XP) was done.
Now, they are working and at anytime in anywhere of the application
access
unexpectedly quits with no error messages and the info that was being
entered
|
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 will call. I'm getting Error 2664: cannot convert parameter 1 from CallBack __gc * to CUSTOM_HOOK_CALLBACK_FUNCTION. Here's what I have...
In my unmanaged DLL -- Header file:
---------------------------------------------------
typedef VOID...
|
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 3rd party unmanaged DLL will pass this
reference into standard Win32 unmanaged static callback function in my code.
Inside this unmanaged callback function I need to cast this unmnaged pointer
that I have received from 3rd party back into the...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |