Joacim Andersson
Jun 13th, 2005, 01:54 AM
In a recent thread in the Classic VB forum a question about using pointers was asked. The thread started wanted to know how to get the address of a function and use that address to call the function. Well, most of us know that you can use the AddressOf keyword to get the address of a function that resides in a BAS module, but I first of course replied that you can't call your own VB functions using that address. I even quoted MSDN:Pointers to Visual Basic functions cannot be passed within Visual Basic itself. Currently, only pointers from Visual Basic to a DLL function are supported.When it comes to creating call backs the MSDN Library states:You can create your own call-back function prototypes in DLLs compiled with Visual C++ (or similar tools). To work with AddressOf, your prototype must use the __stdcall calling convention. The default calling convention (_cdecl) will not work with AddressOf.So since Microsoft says so it must be true, right? Yeah right...
After coming back to that thread I started thinking if it really is impossible or if there could be someway around it. It suddenly hit me that whenever I subclass a Form I must make a call to the origional WndProc (unless I consume the message I've got) which is done using the CallWindowProc API function and the first argument we pass to that function is nothing else but an address to the origional WndProc function. Since CallWindowProc obviously use a pointer to call the function I thought that maybe it can be used to call any function as long as the function signature match (the number and types of arguments). It did!
Instead of rewriting the whole enchalada here's the origional post (http://www.vbforums.com/showpost.php?p=2046053&postcount=5) with further explanations and code examples.
Have a look, it's fun :)
After coming back to that thread I started thinking if it really is impossible or if there could be someway around it. It suddenly hit me that whenever I subclass a Form I must make a call to the origional WndProc (unless I consume the message I've got) which is done using the CallWindowProc API function and the first argument we pass to that function is nothing else but an address to the origional WndProc function. Since CallWindowProc obviously use a pointer to call the function I thought that maybe it can be used to call any function as long as the function signature match (the number and types of arguments). It did!
Instead of rewriting the whole enchalada here's the origional post (http://www.vbforums.com/showpost.php?p=2046053&postcount=5) with further explanations and code examples.
Have a look, it's fun :)