|
-
Jan 27th, 2012, 12:22 PM
#1
Thread Starter
Lively Member
AddressOf
Hello
I have this code:
HTML Code:
Sub OnScanCallback(ByVal sData As String, ByVal wParam As Integer, ByVal lParam As Integer)
FEISC_ScanSample.txtBox.Text = FEISC_ScanSample.txtBox.Text + sData
End Sub
Function ReturnAddress(ByVal address As Integer) As Integer
ReturnAddress = address
End Function
when i tried to call it in the form
HTML Code:
dim iMethod as integer
imethod = ReturnAddress(AddressOf OnScanCallback)
then i received this error
"Error 1 'AddressOf' expression cannot be converted to 'Integer' because 'Integer' is not a delegate type."
any suggestions?
Thanks
-
Jan 27th, 2012, 01:26 PM
#2
Re: AddressOf
It's easy to see what is wrong, however, it is hard to see what you are actually expecting that code to do. Can you explain more what you are trying to do?
-
Jan 27th, 2012, 01:34 PM
#3
Re: AddressOf
It looks like you are trying to get the memory address of a method, is that right?
My usual boring signature: Nothing
 
-
Jan 27th, 2012, 01:50 PM
#4
Re: AddressOf
If you are trying to do what shaggy said:
Code:
System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate
-
Jan 27th, 2012, 02:03 PM
#5
Thread Starter
Lively Member
Re: AddressOf
Hello Forum account and Shaggy
What I'm trying to do is to get the memory address of the method "OnScanCallback" so "imethod" will receive the value.
thanks
-
Jan 27th, 2012, 02:32 PM
#6
Thread Starter
Lively Member
Re: AddressOf
 Originally Posted by BlindSniper
If you are trying to do what shaggy said:
Code:
System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate
I put that code in the upper part are like this
HTML Code:
Imports System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate
but theres an error because GetFunctionPointerForDelegate is not a member of marshal when i press "." after Marshal there was no dropdown list of members
sorry I'm kinda new to VB2010 I Was a fun of VB6 4 yrs ago 
thanks
-
Jan 27th, 2012, 02:35 PM
#7
Re: AddressOf
You don't have to import it, but if you want to.
Code:
Imports System.Runtime.InteropServices
Dim pointer as IntPtr = Marshal.GetFunctionPointerForDelegate(ADelegate)
-
Jan 27th, 2012, 02:55 PM
#8
Re: AddressOf
Instead of focusing on what you think is the solution, let's focus on the problem. What are you actually trying to accomplish here? The big picture?
-
Jan 27th, 2012, 03:17 PM
#9
Thread Starter
Lively Member
Re: AddressOf
 Originally Posted by minitech
Instead of focusing on what you think is the solution, let's focus on the problem. What are you actually trying to accomplish here? The big picture?
I'm trying to get the memory address of the method.
-
Jan 27th, 2012, 03:20 PM
#10
Re: AddressOf
 Originally Posted by magnitude
I'm trying to get the memory address of the method.
Yes. You said that. To what end?
-
Jan 27th, 2012, 04:03 PM
#11
Thread Starter
Lively Member
Re: AddressOf
 Originally Posted by minitech
Yes. You said that. To what end?
Sorry what do you mean?
-
Jan 27th, 2012, 04:04 PM
#12
Re: AddressOf
 Originally Posted by magnitude
Sorry  what do you mean?
What do you want to do with said address? What is the point of calling that function? When you wrote that code, what did you expect should happen?
-
Jan 27th, 2012, 04:50 PM
#13
Re: AddressOf
If the purpose is that you want to be able to call the function through the address of the function, as a function pointer, then that would be one thing, whereas if you are just interested in the function address as a point of interest, that would be another. I think that's what minitech is getting at. In other words, what would you do with the address, as there is quite likely to be a better way to accomplish the same result?
My usual boring signature: Nothing
 
-
Jan 27th, 2012, 05:17 PM
#14
Thread Starter
Lively Member
Re: AddressOf
 Originally Posted by Shaggy Hiker
If the purpose is that you want to be able to call the function through the address of the function, as a function pointer, then that would be one thing, whereas if you are just interested in the function address as a point of interest, that would be another. I think that's what minitech is getting at. In other words, what would you do with the address, as there is quite likely to be a better way to accomplish the same result?
Shaggy and minitech,
Basically Im trying to test this code provided by our manufacturer. this program will scan the RFID tags and will be listed is textbox. Originally the program is written in VB6 and in someway i managed to transfer the code from vb6 to vb2010. but i having an issue with the "addressof" below is the complete code.
mainform----
HTML Code:
Public Class FEISC_ScanSample
Public m_iPortHnd As Integer
Public m_iReaderHnd As Integer
Public m_sBaud As String
Public m_sFrame As String
Private Sub Form_Initialize()
txtBox.Text = ""
End Sub
Private Sub FEISC_ScanSample_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Dim Init As FEISC_EVENT_INIT
If m_iPortHnd > 0 Then
' close the serial port
FECOM_ClosePort(m_iPortHnd)
End If
If m_iReaderHnd > 0 Then
' first: delete event handler in modul FEISC for scanner event
Init.uiFlag = FEISC_CALLBACK_2
Init.uiUse = FEISC_SCANNER_EVENT
Init.Method =Init.Method = ReturnAddress(addressof OnScanCallback)
' second: delete reader object in modul FEISC
FEISC_DeleteReader(m_iReaderHnd)
End If
End Sub
Private Sub FEISC_ScanSample_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Init As FEISC_EVENT_INIT
' open a serial port
m_iPortHnd = FECOM_OpenPort("1")
'initialize port
FECOM_SetPortPara(m_iPortHnd, "Baud", "38400")
FECOM_SetPortPara(m_iPortHnd, "Frame", "8E1")
'create reader object in modul FEISC
m_iReaderHnd = FEISC_NewReader(m_iPortHnd)
' enable the following code line, if reader sends scan data as
' unformatted hex data
'FEISC_SetReaderPara m_iReaderHnd, "ConvHexToString", "1"
' add event handler in modul FEISC for scanner event
Init.uiFlag = FEISC_CALLBACK_2
Init.uiUse = FEISC_SCANNER_EVENT
Init.Method = ReturnAddress(addressof OnScanCallback) ' This is the line where the error generated during the form load
FEISC_AddEventHandler(m_iReaderHnd, Init)
End Sub
End Class
HTML Code:
Option Explicit On
Module PubFunctions
'Attribute VB_Name = "PubFunctions"
'-----------------------------------------------------------------
'Copyright © 2000-2003 FEIG ELECTRONIC GmbH, All Rights Reserved.
' Lange Strasse 4
' D-35781 Weilburg
' Federal Republic of Germany
' phone : +49 6471 31090
' fax : +49 6471 310999
' e -mail: info@ feig.de
' Internet : http://www.feig.de'
'
'OBID i-scan is a registered trademark of FEIG ELECTRONIC GmbH
'------------------------------------------------------------------
Sub OnScanCallback(ByVal sData As String, ByVal wParam As Integer, ByVal lParam As Integer)
' do not set a breakpoint at the following line
' a breakpoint will cause a crash in Visual Basic
FEISC_ScanSample.txtBox.Text += sData
End Sub
Function ReturnAddress(ByVal address As Integer) As Integer
ReturnAddress= Address
End Function
End Module
HTML Code:
Option Explicit On
Module FECOM
'Attribute VB_Name = "FECOM"
'---------------------------------------------------------
'| |
'| FECOM.BAS |
'| |
'---------------------------------------------------------
'
'Copyright © 2000-2005 FEIG ELECTRONIC GmbH, All Rights Reserved.
' Lange Strasse 4
' D-35781 Weilburg
' Federal Republic of Germany
' phone : +49 6471 31090
' fax : +49 6471 310999
' e-mail : [email protected]
' Internet : http://www.feig.de
'
'Version : 02.06.08 / 08.03.2005 / M. Hultsch
'
'Operation Systems : Windows 9x/ME/NT/2000/XP
'
'
'This File contains the constants, datatypes and function declarations of E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll
' constants for uiFlag in FECOM_MSG_INIT
Public Const FECOM_THREAD_ID = 1 'not useable in Visual Basic
Public Const FECOM_WND_HWND = 2 'not useable in Visual Basic
Public Const FECOM_CALLBACK = 3
Public Const FECOM_EVENT = 4
' defines for uiUse in FECOM_MSG_INIT
Public Const FECOM_CTS_EVENT = 1
Public Const FECOM_DCD_EVENT = 2
Public Const FECOM_DSR_EVENT = 3
Public Const FECOM_RTS_EVENT = 4
Public Const FECOM_DTR_EVENT = 5
' structure for transfering callback functions
Structure FECOM_EVENT_INIT
Dim uiUse As Integer 'integer ' defines the event (e.g. FECOM_CTS_EVENT)
Dim uiMsg As Integer 'integer ' set always to 0 - in Visual Basic not used !!
Dim uiFlag As Integer 'integer ' specifies the use of the Variant (FECOM_CALLBACK or FECOM_EVENT)
Dim Method As Integer 'integer ' for callback-function or event-handler
End Structure
Public Declare Sub FECOM_GetDLLVersion Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal cVersion As String)
Public Declare Function FECOM_OpenPort Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal cPortNr As String) As Integer
Public Declare Function FECOM_ClosePort Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal iPortHnd As Integer) As Integer
Public Declare Function FECOM_DetectPort Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal iPortNr As Integer) As Integer
Public Declare Function FECOM_GetErrorText Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal iErrorCode As Integer, ByVal cErrorText As String) As Integer
Public Declare Function FECOM_GetLastError Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal iPortHnd As Integer, ByVal iErrorCode As Integer, ByVal cErrorText As String) As Integer
Public Declare Function FECOM_AddEventHandler Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal iPortHnd As Integer, ByVal pInit As FECOM_EVENT_INIT) As Integer
Public Declare Function FECOM_DelEventHandler Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal iPortHnd As Integer, ByVal pInit As FECOM_EVENT_INIT) As Integer
Public Declare Function FECOM_GetPortList Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal iNext As Integer) As Integer
Public Declare Function FECOM_GetPortPara Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal iPortHnd As Integer, ByVal cPara As String, ByVal cValue As String) As Integer
Public Declare Function FECOM_SetPortPara Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal iPortHnd As Integer, ByVal cPara As String, ByVal cValue As String) As Integer
Public Declare Function FECOM_DoPortCmd Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal iPortHnd As Integer, ByVal cCmd As String, ByVal cValue As String) As Integer
Public Declare Function FECOM_GetPortHnd Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal cPortNr As String) As Integer
Public Declare Function FECOM_Transceive Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal iPortHnd As Integer, ByVal cSendProt As String, ByVal iSendLen As Integer, ByVal cRecProt As String, ByVal iRecLen As Integer) As Integer
Public Declare Function FECOM_Transmit Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal iPortHnd As Integer, ByVal cSendProt As String, ByVal iSendLen As Integer) As Integer
Public Declare Function FECOM_Receive Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\Fecom.dll" (ByVal iPortHnd As Integer, ByVal cRecProt As String, ByVal iRecLen As Integer) As Integer
End Module
Thanks
-
Jan 27th, 2012, 05:32 PM
#15
Thread Starter
Lively Member
Re: AddressOf
i forgot this could code: another module
HTML Code:
Module FEISC
' constants for uiFlag in FEISC_MSG_INIT
Public Const FEISC_THREAD_ID = 1
Public Const FEISC_WND_HWND = 2
Public Const FEISC_CALLBACK = 3
Public Const FEISC_EVENT = 4
Public Const FEISC_CALLBACK_2 = 5
' defines for uiUse in FEISC_MSG_INIT
Public Const FEISC_PRT_EVENT = 1
Public Const FEISC_SNDPRT_EVENT = 2
Public Const FEISC_RECPRT_EVENT = 3
Public Const FEISC_SCANNER_EVENT = 4
' structure for transfering callback functions
Structure FEISC_EVENT_INIT
Dim uiUse As Int32 ' defines the event (e.g. FEISC_PRT_EVENT)
Dim uiMsg As Int32 ' set always to 0 - in Visual Basic not necessary !!
Dim uiFlag As Int32 ' set always to FEISC_EVENT
Dim Method As Int32 ' for callback-function or event-handle
End Structure
' miscellaneous functions
Public Declare Sub FEISC_GetDLLVersion Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal cVersion As String)
Public Declare Function FEISC_GetErrorText Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iErrorCode As Int32, ByVal cErrorText As String) As Int32
Public Declare Function FEISC_GetStatusText Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal ucStatus As Byte, ByVal cStatusText As String) As Int32
' functions for event notification
Public Declare Function FEISC_AddEventHandler Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iReaderHnd As Int32, ByVal pInit As FEISC_EVENT_INIT) As Int32
Public Declare Function FEISC_DelEventHandler Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iReaderHnd As Int32, ByVal pInit As FEISC_EVENT_INIT) As Int32
' administration functions
Public Declare Function FEISC_NewReader Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iPortHnd As Int32) As Int32
Public Declare Function FEISC_DeleteReader Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iReaderHnd As Int32) As Int32
Public Declare Function FEISC_GetReaderList Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iNext As Int32) As Int32
Public Declare Function FEISC_GetReaderPara Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iReaderHnd As Int32, ByVal cPara As String, ByVal cValue As String) As Int32
Public Declare Function FEISC_SetReaderPara Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iReaderHnd As Int32, ByVal cPara As String, ByVal cValue As String) As Int32
' functions for protocol frame
Public Declare Function FEISC_BuildSendProtocol Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iReaderHnd As Int32, ByVal cBusAdr As Byte, ByVal cCmdByte As Byte, ByVal cSendData As String, ByVal iDataLen As Int32, ByVal cSendProt As String, ByVal iDataType As Int32) As Int32
Public Declare Function FEISC_SplitRecProtocol Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iReaderHnd As Int32, ByVal cRecProt As String, ByVal iRecLen As Int32, ByVal cBusAdr As Byte, ByVal cCmdByte As Byte, ByVal cRecData As String, ByVal iDataLen As Int32, ByVal iDataType As Int32) As Int32
' query functions
Public Declare Function FEISC_GetLastSendProt Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iReaderHnd As Int32, ByVal cSendProt As String, ByVal iDataType As Int32) As Int32
Public Declare Function FEISC_GetLastRecProt Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iReaderHnd As Int32, ByVal cRecProt As String, ByVal iDataType As Int32) As Int32
Public Declare Function FEISC_GetLastState Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iReaderHnd As Int32, ByVal cStateText As String) As Int32
Public Declare Function FEISC_GetLastRecProtLen Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iReaderHnd As Int32) As Int32
Public Declare Function FEISC_GetLastError Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iReaderHnd As Int32, ByVal iErrorCode As Int32, ByVal cErrorText As String) As Int32
' common communication functions
Public Declare Function FEISC_SendTransparent Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iReaderHnd As Int32, ByVal cSendProt As String, ByVal iSendLen As Int32, ByVal cRecProt As String, ByVal iRecLen As Int32, ByVal iCheckSum As Int32, ByVal iDataType As Int32) As Int32
Public Declare Function FEISC_Transmit Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iReaderHnd As Int32, ByVal cSendProt As String, ByVal iSendLen As Int32, ByVal iCheckSum As Int32, ByVal iDataType As Int32) As Int32
Public Declare Function FEISC_Receive Lib "E:\Drill\sdk2\WindowsApplication1\WindowsApplication1\FeIsc.dll" (ByVal iReaderHnd As Int32, ByVal cRecProt As String, ByVal iRecLen As Int32, ByVal iDataType As Int32) As Int32
End Module
-
Jan 30th, 2012, 11:33 AM
#16
Thread Starter
Lively Member
-
Jan 30th, 2012, 11:43 AM
#17
Re: AddressOf
Is this a typo:
Init.Method =Init.Method = ReturnAddress(addressof OnScanCallback)
Also, what type is Init.Method? It appears that it might be a callback function. Do you have any further information on it?
My usual boring signature: Nothing
 
-
Jan 30th, 2012, 11:58 AM
#18
Thread Starter
Lively Member
Re: AddressOf
 Originally Posted by Shaggy Hiker
Is this a typo:
Init.Method =Init.Method = ReturnAddress(addressof OnScanCallback)
Also, what type is Init.Method? It appears that it might be a callback function. Do you have any further information on it?
Yah sorry its a typo it should
HTML Code:
Init.Method =ReturnAddress(addressof OnScanCallback)
Init.method is a member of the structure FEISC_EVENT_INIT
i declared init as FEISC_EVENT_INIT type
Thanks.
-
Jan 30th, 2012, 11:59 AM
#19
Thread Starter
Lively Member
Re: AddressOf
Init.method is a Int32 type I changed it to Integer got the same error.
-
Jan 30th, 2012, 06:17 PM
#20
Re: AddressOf
The Method field should be typed as "Action(Of String, Integer, Integer)", not "Integer". Assign it the value "AddressOf OnScanCallback" directly, and forget the ReturnAddress function.
This change will doubtless require knock-on changes downstream, but this is the correct way to handle function pointers in .NET, you cannot store such things in Integers.
[Edit: Looks like you'll be needing some InterOp code in there as well. Oh, you're in for some fun. ]
[Edit2: And if the function pointer is being passed to the external DLL (which it appears to be), it'll probably need to be an IntPtr, not an Action(Of ). Uhhh, this means you'll need to add some hints to the memory manager not to move things around... start with this article: http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx ]
[Edit3: Reading a little further, P/Invoke should handle that for you, so start by trying it with Action(...), and if that doesn't work try defining a delegate (something like "Public Delegate ScanCallback(sData As String, wParam as Integer, lParam as Integer)") ]
Last edited by Evil_Giraffe; Jan 30th, 2012 at 06:27 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|