Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

bazee

Visual Basic and Function Pointers

This topic is 5922 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Using Visual Basic 6: I know how to get a pointer to a function using the AddressOf operator, but now I need to CALL the function using the pointer. Is this possible? I can''t find anything relevant on MSDN. Thanks if anyone knows the answer.

Share this post


Link to post
Share on other sites
Advertisement
Its interesting that you didnt find anything on MSDN...
quote:
This is straight out of MSDN
Although you can use AddressOf to pass procedure pointers among Basic procedures, you can''t call a function through such a pointer from within Basic.



You may want to look into CallByName; it''s allowed me to write some clever(and dirty) replacements for function pointers.

____________________________________________________________
Direct3D vs. OpenGL

Share this post


Link to post
Share on other sites
The MSDN is lying

There is an API call you can make which will execute a function pointer for you. It isn''t actually designed to be used this way, but you still can:


Dim Result As Long
Dim hWnd As Long
Dim Message As Long
Dim Param1 As Long, Param2 As Long

Result = CallWindowProc(AddressOf MyFunction, hWnd, Message, Param1, Param2)


I don''t have the declare handy, but it is in the API viewer and at www.vbapi.com

The values you pass (hWnd, Message, Param1, Param2) are all longs, and will be passed to the function:


Function MyFunction(hWnd As Long, Message As Long, Param1 As Long, Param2 As Long) As Long

End Function


They are not interpreted at all by CallWindowProc, so if you don''t need them, you can pass 0s.

I don''t think you need to change anything to ByVal, but if you do get a problem, then that is probably what it is. I also haven''t investigated returning values from the function, so I''m not sure if that works or not.

Trying is the first step towards failure.

Share this post


Link to post
Share on other sites
Oh, one other thing - you can use this method to do inline ASM . Fill a byte array with the opcodes for your ASM, use VarPtr to get a pointer to the first element in this array, and then off you go.

Oh - and it is very easy to crash VB when playing around with this stuff. Make sure you save your work

Trying is the first step towards failure.

[edited by - ragonastick on May 31, 2002 3:38:18 AM]

Share this post


Link to post
Share on other sites
In my opinion, u would better move to C++, because all this iline ASM, pointers (!) in VB looks ridiculuos...

Sorry, if I hurt someone

Lekha

Share this post


Link to post
Share on other sites
Thanks for the replies

Promit, that might be a good alternative, although the reason I''m doing this is to increase performance, and I suspect CallByName is slow. I''ll test and see.

Ragonastick, thats a clever idea! I''ll try that too. I''m not sure about inline ASM, it would be messy and I don''t know much about it. Anyway, my project must specifically be written in VB, so that also eliminates C++.

If neither work, I can still write my program without the function pointer, but it would involve a huge case statement in my per-pixel software rendering function... not good!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!