I've converted a project from VB6 to vb.Net, with visual studio 2008 and I'm experiencing difficulties with the addressOf operator.
I have an application which uses an external dll to communicate with a payment terminal, through callback functions. I understand that in vb.Net, you have to use delegates. Before I continue, here's a part of the VB6 code.
Type PRN_FUNC_TYPE
Status As Long
init As Long
Run As Long
Exit As Long
End Type
Public Function GetProcAddress(ByVal lngAddressOf As Long) As Long
GetProcAddress = lngAddressOf
End Function
Sub signOffPrinter(ByVal hwnd As Long)
Dim PrnFunc As PRN_FUNC_TYPE
PrnFunc.init = GetProcAddress(AddressOf printSignOffInit)
PrnFunc.Run = GetProcAddress(AddressOf printSignOffRun)
PrnFunc.Exit = GetProcAddress(AddressOf printSignOffExit)
PrnFunc.Status = GetProcAddress(AddressOf printerStatus)
changePrinter hwnd, PrnFunc
End Sub
Function printerStatus(ByVal lpVoidPrn As Long) As Long
printerStatus = 1
End Function
Here's the vb.net code (created by converting the vb6 code):
Structure PRN_FUNC_TYPE
Dim Status As Integer
Dim init As Integer
Dim Run As Integer
'UPGRADE_NOTE: Exit was upgraded to Exit_Renamed. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
Dim Exit_Renamed As Integer
End Structure
Public Function GetProcAddress(ByVal lngAddressOf As Integer) As Integer
GetProcAddress = lngAddressOf
End Function
Sub signOffPrinter(ByVal hwnd As Integer)
Dim PrnFunc As PRN_FUNC_TYPE
Dim dlgPrinterStatus = New DelegatePrinterStatus(AddressOf printerStatus)
'UPGRADE_WARNING: Add a delegate for AddressOf printSignOffInit Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="E9E157F7-EF0C-4016-87B7-7D7FBBC6EE08"'
PrnFunc.init = GetProcAddress(AddressOf printSignOffInit)
'UPGRADE_WARNING: Add a delegate for AddressOf printSignOffRun Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="E9E157F7-EF0C-4016-87B7-7D7FBBC6EE08"'
PrnFunc.Run = GetProcAddress(AddressOf printSignOffRun)
'UPGRADE_WARNING: Add a delegate for AddressOf printSignOffExit Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="E9E157F7-EF0C-4016-87B7-7D7FBBC6EE08"'
PrnFunc.Exit_Renamed = GetProcAddress(AddressOf printSignOffExit)
'UPGRADE_WARNING: Add a delegate for AddressOf printerStatus Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="E9E157F7-EF0C-4016-87B7-7D7FBBC6EE08"'
PrnFunc.Status = GetProcAddress(AddressOf printerStatus)
changePrinter(hwnd, PrnFunc)
End Sub
Delegate Function DelegatePrinterStatus(ByVal lpVoidPrn As Integer) As Integer
Function printerStatus(ByVal lpVoidPrn As Integer) As Integer
printerStatus = 1
End Function
The red text is added by me. What I want to do is change PrnFunc.Status = GetProcAddress(AddressOf printerStatus)
so that it uses the delegate. I've searched the net and all the info I found said that I have to use the Invoke method. The problem is that by calling the Invoke method on the delegate, I have to pass an integer value lpVoidPrn to the Invoke method, which I don't have.
I don't know much about delegates, and the code is from an external supplier, so I'm not sure if adjusting the function signature printerStatus(ByVal lpVoidPrn As Integer)
to printerStatus()
would solve my problem. My guess is that the code will not work, because the matching method (plus signature) does not exist in the external dll.
I hope someone can shine a light on this issue, because I'm out of ideas. Thanks in advance.
Jochem