AddressOf Operator
This operator is used to pass the address of a subroutine to an API that is expecting a function pointer.
Most of the time you will not need to obtain any low level information about your variables.
However there are a few Windows API functions that require the memory address of a variable.
link - learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/addressof-operator
Public Declare PtrSafe Function EnumFontFamilies Lib "gdi32" Alias _
"EnumFontFamiliesA" _
(ByVal hDC As LongPtr, ByVal lpszFamily As String, _
ByVal lpEnumFontFamProc As LongPtr, ByVal LParam As LongPtr) As Long
Public Declare PtrSafe Function GetDC Lib "user32" (ByVal hWnd As LongPtr) As LongPtr
Public Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hWnd As LongPtr, _
ByVal hDC As LongPtr) As LongPtr
Public Sub FillListWithFonts(LB As ListBox)
Dim hDC As LongPtr
LB.Clear
hDC = GetDC(LB.hWnd)
EnumFontFamilies hDC, vbNullString, AddressOf EnumFontFamProc, ObjPtr(LB)
ReleaseDC LB.hWnd, hDC
End Sub
Public Function EnumFontFamProc( _
ByVal lpelf As LongPtr, _
ByVal lpntm As LongPtr, _
ByVal FontType As Long, _
ByVal lParam As LongPtr) As Long
' This is the function Windows jumps to when collecting fonts.
' Return 1 to tell Windows: "Keep going, find the next font."
EnumFontFamProc = 1
End Function
When you want to use API functions the following functions will prove useful:
VarPtr - returns the address of a variable
VarPtrArray - returns the address of an array
VarPtrStringArray - returns the address of an array string
StrPtr - returns the address of the UNICODE string buffer
ObjPtr - returns the pointer to the interface referenced by an object variable
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrev