VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Function Pointers in VB


Category: Miscellaneous
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (5 Votes)

This code makes it possible to use function pointers (no, not OBJPtr) basic funciton pointers, in VB.
It uses two API calls. Basically, it uses a window callback as the entry point for your new function, redirecting the window call to the function supplied.
A little out of the norm, but a handy thing to have when callbacks to non-objects is needed.
If you are attempting to call objects back, I recommend either the undocumented ObjPtr() or CallByName (when you know the name of the object function to call)

Inputs
No inputs
Assumes
Nothing, absolutely nothing.
Code Returns
No Returns, just reap the benefits.
Side Effects
None that I know of, been using this for two years.
API Declarations
Declare Function CallWindowProc& Lib "user32" Alias "CallWindowProcA" (ByVal
lpPrevWndFunc&, ByVal hWnd&, ByVal Msg&, ByVal wParam&, ByVal lParam&)
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any,
lpvSource As Any, ByVal cbCopy As Long)

Rate Function Pointers in VB

Option Explicit
Private Function StripStringFromPointer$(ByVal lpString&, ByVal nStrLen&)
  Dim Info$
  Info = String$(nStrLen, vbNullChar)
  CopyMemory ByVal StrPtr(Info), ByVal lpString, nStrLen * 2
  StripStringFromPointer = Info
End Function
Private Function GetAddress(Addr&)
  GetAddress = Addr
End Function
Private Function MyFunction&(ByVal lpString&, ByVal nStrLen&, ByVal param3&,
ByVal param4&)
  Debug.Print StripStringFromPointer(lpString, nStrLen)
End Function
Public Sub Main()
  Dim FunctAddr&, Info$
  Info = "Holy Smoke"
  FunctAddr = GetAddress(AddressOf MyFunction)
  CallWindowProc FunctAddr, StrPtr(Info), CLng(Len(Info)), 0&, 0&
  End
End Sub

Download this snippet    Add to My Saved Code

Function Pointers in VB Comments

No comments have been posted about Function Pointers in VB. Why not be the first to post a comment about Function Pointers in VB.

Post your comment

Subject:
Message:
0/1000 characters