Click to See Complete Forum and Search --> : Pointer to function ....HELP!!!!


Miguel
September 20th, 1999, 11:54 AM
Hi:
I would like to load into a structure element a function pointer (callback function). Iīm tring it with AddressOf operator but donīt work fine ... What matter?
How can I make it?

Thank for your help.... Bye

Ravi Kiran
September 20th, 1999, 11:49 PM
Addressof works mainly with APIs only. No VB-VB calls, (atleast in VB 5. In 6.0 IDK) . So, how exaclty you are using? provide more info.

RK

Lothar Haensler
September 21st, 1999, 01:38 AM
what do you mean "Addressof doesn't work". It sure does.
Better post your code and error message.
You can use Addressof only for functions that reside in a standard module AFAIK.

Miguel
September 21st, 1999, 03:11 AM
Hi:
Following is the code which I am tring to load the address function into my structure element (ncb_post).
The error is: Compile error Syntax error.

What is the meaning of standard module AFAIK?


Declare Function Netbios Lib "netapi32.dll" (pncb as NCB) as Byte

public Const NCBNAMSZ = 16 ' absolute length of a net name
public Const NCBDGRECV = &H21 ' NCB RECEIVE DATAGRAM

Type NCB
ncb_command as Integer
ncb_retcode as Integer
ncb_lsn as Integer
ncb_num as Integer
ncb_buffer as string
ncb_length as Integer
ncb_callname as string * NCBNAMSZ
ncb_name as string * NCBNAMSZ
ncb_rto as Integer
ncb_sto as Integer
ncb_post as Long 'address of the routine to call
ncb_lana_num as Integer
ncb_cmd_cplt as Integer
ncb_reserve(10) as Byte ' Reserved, must be 0
ncb_event as Long
End Type

Sub Main()
Dim Est as NCB
Est.ncb_command = NCBDGRECV
Est.ncb_post = AddressOf Respuesta

Ret = Netbios(Est)
End Sub

Function Respuesta(pncb as NCB)
'Here I will write my code

End Function





If I can use only AddressOf into a VB-API, How can I get the address of a function into a VB-VB and after to load it in the structure element?
Thanks for your help
Bye

Miguel
September 21st, 1999, 03:31 AM
Hi:
Following is the code which I am tring to load the address function into my structure element (ncb_post).
The error is: Compile error Syntax error.


Declare Function Netbios Lib "netapi32.dll" (pncb as NCB) as Byte
public Const NCBDGRECV = &H21 ' NCB RECEIVE DATAGRAM
public Const NCBNAMSZ = 16 ' absolute length of a net name

Type NCB
ncb_command as Integer
ncb_retcode as Integer
ncb_lsn as Integer
ncb_num as Integer
ncb_buffer as string
ncb_length as Integer
ncb_callname as string * NCBNAMSZ
ncb_name as string * NCBNAMSZ
ncb_rto as Integer
ncb_sto as Integer
ncb_post as Long 'Address of the routine to call
ncb_lana_num as Integer
ncb_cmd_cplt as Integer
ncb_reserve(10) as Byte ' Reserved, must be 0
ncb_event as Long
End Type


Sub Main()
Dim Est as NCB
Est.ncb_command = NCBDGRECV
Est.ncb_post = AddressOf Respuesta

Ret = Netbios(Est)
End Sub
Function Respuesta(pncb as NCB)
'I will write my code here

End Function




If I can use only AddressOf into a VB-API, How can I get the address of a function into a VB-VB?
Thanks for your help
Bye

Lothar Haensler
September 21st, 1999, 03:39 AM
this is what the docs say about addressof:
"AddressOf can only be used immediately preceding an argument in an argument list; "
AFAIK = as far as I know

Obviously, I was wrong and Ravi was right.

Crazy D @ Work
September 21st, 1999, 03:41 AM
To pass the address of a function to a structure, use a function like this:

Function Get_AddressOf_Function(pfFunction as Long) as Long
Get_AddressOf_Function = pfFunction
End Function



You only need to figure if it has to be ByRef or ByVal, I forgot...
Hope this helps

Crazy D @ Work :-)

Lothar Haensler
September 21st, 1999, 03:42 AM
I'm an idiot! :-)
I wasn't wrong:
Check MSDN - search for AddressOf.
You can assign a function pointer to a variable.

from the docs:
"Function FnPtrToLong (ByVal lngFnPtr As Long) As Long
FnPtrToLong = lngFnPtr
End Function

To use the function, you first declare the type, then call FnPtrToLong. You pass AddressOf plus your callback function name for the second argument.

Dim mt as MyType
mt.MyPtr = FnPtrToLong(AddressOf MyCallBackFunction)

"

Ravi Kiran
September 21st, 1999, 04:37 AM
I think the problem is just plain simple:
Change this line

Est.ncb_post = AddressOf Respuesta



to

Est.ncb_post = AddressOf(Respuesta)




It is the trouble with the function call being called with = and no brackets.

RK

Miguel
September 21st, 1999, 08:30 AM
Hi Ravi:
I had tried this solution but I received the follow error:

Compile error. Expected expression.

Have you got another idea?

Thanks for your help. Bye...

Crazy D @ Work
September 21st, 1999, 08:40 AM
Try what i said in my other post... AddressOf ONLY works when you call a function. It won't work outside the () of calling a function... (oh I still don't know if that function is ByRef or byVal, but the idea of passing the adres of a function to another function, which returns it, i remember that i read that in MSDN a long time ago...)

Crazy D @ Work :-)

Miguel
September 21st, 1999, 08:53 AM
Hi:
Sorry I donīt understand very well your message.
Where Do I get the pointer to my function into your Get_AddressOf_Function?
I cannīt see clearly your idea.

Thanks for your help. Bye....

Crazy D @ Work
September 21st, 1999, 09:13 AM
Call it like this:
Get_Adressof(Adress Of The_Function)
the return value contains the pointer to the function you have to put in the structure you were talking about.
Play with it, it's either ByRef or ByVal (in the function declaration)
I've used it for the callback of the browseforfolders dialog, and it works fine (but I dont have that code here...)

Crazy D @ Work :-)

Miguel
September 21st, 1999, 10:13 AM
Hi:
I have just tested your solution a work fine.
I find your solution very ingenious. Congratulation for your idea.

Thank for your help. Bye...

Miguel
September 22nd, 1999, 05:41 AM
Hi:
I would not like to disturb with my E-Mail.
I send you this message because I donīt know how can I begin my development and perhaps you know any thing about this subject.
In advance thank for your help and patience.

I would like to develop an Ethernet Sniffer Analyzer and I need fundamentally the information after TCP/IP protocol.

1š) I think that I must to configure my Ethernet card in promiscuous mode. How can I make it?

2š) After configure Ethernet card I must to get the data across the net. How can I make it?
Which are the API functions that I must to use?

3š) For an easiest work, can I use WinSock.ocx for this purpose?
I believe that I can not work with Winsock.ock for this purpose because I need a previous connection but I am not sure....

Thank you very much for your help. Bye...

Miguel
September 22nd, 1999, 06:09 AM
Hi:
I would not like to disturb with my E-Mail.
I send you this message because I donīt know how can I begin my development and perhaps you know any thing about this subject.
In advance thank for your help and patience.

I would like to develop an Ethernet Sniffer Analyzer and I need fundamentally the information after TCP/IP protocol.

1š) I think that I must to configure my Ethernet card in promiscuous mode. How can I make it?

2š) After configure Ethernet card I must to get the data across the net. How can I make it?
Which are the API functions that I must to use?

3š) For an easiest work, can I use WinSock.ocx for this purpose?
I believe that I can not work with Winsock.ock for this purpose because I need a previous connection but I am not sure....

Could you give me your E-Mail address? For me is better to send you my message by E-Mail I have problems with private message in Codeguru Web.

Thank you very much for your help. Bye...

Para que vayas practicando tu Castellano (Idioma Oficial en Espaņa), te envio estas lineas.Espero que hayas prosperado mucho. Adios...

June 14th, 2000, 08:34 AM
Hi!

I have the same problem like you had:
I need to program a sniffer.

if you can tell me how to begin with this - how to configure the ethernet card so I will get al the messages on the net

thanks in advance!

bye

Shawn South
June 14th, 2000, 02:36 PM
This will not work, because AddressOf is an operator, not a function. According to my study guide (for VB6), you are correct: The AddressOf operator is provided only for use with APIs.

While it should be possible to copy the address to another variable (it is, after all, just a Long), my understanding is that VB provides no methodology for calling a function using it's address.


- Shawn
Game Programmer
Humongous Entertainment, Inc.