Byte Ptr -> Function

BlitzMax Forums/BlitzMax Programming/Byte Ptr -> Function

N3m(Posted 2005) [#1]
Blitzmax converts a Function tu a Byte Ptr automaticly, but how can i convert a Byte Ptr back to a Function?


ImaginaryHuman(Posted 2005) [#2]
Well, I found that I couldn't get it to work directly. There might be a way but from what I could tell BlitzMax won't let you put a BytePointer or any other kind of pointer (except another function pointer) in a function pointer. Maybe this is a features that they can add in a future release.

I did however figure out a way to sneak behind Blitz's back (with regards to type checking)...

Local mytempfunction() 'function pointer
Local mybytepointer:Byte Ptr=WhereverYouWant

'then to turn the machine code at the Byte Ptr into a callable function:
Local where:Int Ptr=Int Ptr(Byte Ptr(VarPtr(mytempfunction)))
mytempbank:TBank=StaticBank(where,4) 'not sure on the syntax - make it look at the function pointer memory 4-bytes
PokeInt where,Int(Int Ptr(mybytepointer)) 'not sure on syntax - overwrite the function pointer content with a byte pointer
mytempfunction() 'call it

Something like that - I don't remember the syntax for creating a static bank or doing the PokeInt off-hand. Basically what you're trying to do here is get the location of the function pointer using VarPtr (which actually works) - and btw you have to first turn a functionpointer into a bytepointer and then into an intpointer (that's the only way it would accept it for me anyway). Then you cludge a static bank onto the memory area of the function pointer variable itself, and write to that memory the pointer that you want the function to represent. Then you call the function. It should then jump to whatever routine you are pointing your byte pointer at - so it better be something executable. You can point it at the byte-pointer version of another function's function pointer if you like, but then you might as well do myfunction()=otherfunction. So I hope whatever you're pointing it at is valid executable code that can run and return to Blitz okay.

I think that's pretty much how I found to do it. BlitzMax wouldn't let me convert the function pointer to anything other than a byte pointer (if I remember rightly), and certainly wouldn't let something be put into the function pointer except for other function pointers. You might need to play with the type casting a little bit if this doesn't work exactly as is.

Hey Mark & Co, whatever happened to good old Peek and Poke without needing a bank? Trying to encourage good programming practice? ;-)


BlitzSupport(Posted 2005) [#3]

whatever happened to good old Peek and Poke without needing a bank?


Memory protection on modern OSes will slap your app down if you try to access anything other than the app's own memory -- banks are just a way of allocating that memory.


ImaginaryHuman(Posted 2005) [#4]
Well ya, we knew that ;-) I was about to say why can't you let them work with some memory that the coder allocates, but then again - that's what a bank is :-D *doh* AHhhhhhh, the good old days.

How about a PokeVar and PeekVar commands then, that specifically let you write to variable content memory? That memory is already allocated. It'd be more elegant than having to define a bank just to change 4 bytes. ???


N3m(Posted 2005) [#5]
the reason why i wan't to do this is:

I want to create somthing like an eventsystem, so i have a function called AddEventHandler wich takes the event name and the callback function as parameters. But not each event gives the same information to the callback function, and so the functions don't have the same parameters. And the problem is i cant create a function pointer as parameter for AddEventHandler wich takes every function. So i tried to take a Byte Ptr and cast it to the relevant function (depending on the event name). Maybe someone got another solution for this problem?

(sorry for my crappy english)


ImaginaryHuman(Posted 2005) [#6]
Umm, yea, it is a problem if you want to use function pointers because you can only copy the pointer to another function defined with the same number of parameters.

(I'm also seeming to find that Int(Int) is how BlitzMax refers to a function pointer - an int pointer)

Maybe you can have seperate versions of your function, one each for a given number of parameters, and call the appropriate one.

Or maybe instead of having varying parameters being passed, you can set up an array of parameters and just pass two paramaters to the function - array pointer and number of elments. That way the function can always take 2 parameters - needing only one function. You would put all your variables in the array and pass the pointer to it which the function then deciphers.


N3m(Posted 2005) [#7]
hmm yes. i think i will do it thisway. thanks