calling a callback?

Windows specific forum
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by mindplay.

I can use the address-of operator (@) to acquire the address of a procedure - but how can I then CALL this procedure, using the stored address?

example:

Code: Select all

; Callback

Procedure CallMe(Value.l)
  Debug("Called with the value "+Str(Value))
EndProcedure

Structure MyStruct
  MyProc.l ; this will hold the address of a procedure
EndStructure

DefType.MyStruct Test

Test\MyProc = @CallMe()

; ... now what?
now how can I call the procedure whose address I've stored in Test\MyProc ?

this is a dumb example, because I could obviously just call the CallMe procedure instead of storing it's address first, but the point is, I'm in a situation where Test\MyProc could hold the address of a number of different procedures, all with the same parameters.

I could write a dispatch procedure of course, but that isn't possible in the project I'm working on, since it's an include that I want to reuse, and the application using the include must be able to "register" it's own procedures, which will be called by the include...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Code: Select all

CallFunctionFast(Test\MyProc, arg)
Can't test it here






El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

Yes, CallFunctionFast is the right way to go.
There is one thing you must keep in mind:

This Function doesn't check anything, it just puts the Arguments on the Stack, and jumps to the Address. The problem is, if you use a wrong Pointer, or the wrong number of Arguments, you Program will crash.

If you are careful with that, it should work just fine.

Timo
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by mindplay.

I know, I'll be careful :) - thanks, that's what I was looking for :)
Post Reply