Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array and Varptr? (ColorPicker - 64 bit )

Status
Not open for further replies.

lameid

Programmer
Jan 31, 2001
4,207
0
0
US
I had to convert color picker code from a 32 bit call to 64 bit. Below is my simplified version for testing and discussion here. Normally I would put the array "dim" / declaration inside the function (just trying to avoid those comments).

In reviewing I ran across... "VarPtr" - This seems to be fetching a reference to a variable. However I read something that said VarPtr does not work with arrays but the below does run... This is far down in the weeds for me. Is this a valid way of doing this? If VarPtr does work with arrays, great it works I get it, if not, it is just getting some random memory address... What I have not done is trying to set the custom colors in the box the dialog that pops up. It seems that could corrupt memory if Varptr isn't doing what it is intended and I remote into my 64 bit office machine so if I kill it, it is bad.

Code:
Option Compare Database
Option Explicit

#If VBA7 Then
    Private Type typChooseColor   '20150211 Moved inside Compile directive and updated to use LongPtr where appropriate
        lStructSize As Long
        hwndOwner As LongPtr
        hInstance As LongPtr
        rgbResult As Long
        lpCustColors As LongPtr
        flags As Long
        lCustData As LongPtr
        lpfnHook As LongPtr
        lpTemplateName As String
    End Type
    
    Private Declare PtrSafe Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" (lpcc As typChooseColor) As LongPtr
#Else
    
    Private Type typChooseColor   '20150211 Moved inside Compile directive
        lStructSize As Long
        hwndOwner As Long
        hInstance As Long
        rgbResult As Long
        lpCustColors As Long
        flags As Long
        lCustData As Long
        lpfnHook As Long
        lpTemplateName As String
    End Type
    
    Private Declare Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" (lpcc As typChooseColor) As Long
#End If
Private Const CC_ANYCOLOR As Long = &H100
Private a_lngCustom(0 To 15) As Long 'You can fill this array with up to 16 color constants

Function GetColorFromPicker() As Long
    Dim cc As typChooseColor
    
    With cc
        
        .lStructSize = LenB(cc)
        .flags = CC_ANYCOLOR
        .rgbResult = 0
        .hwndOwner = Application.hWndAccessApp
        '-----------------------
        
        .lpCustColors = VarPtr(a_lngCustom(0)) 'This Runs but is it Right?

        '-----------------------
    End With
    
    If ChooseColor(cc) = 1 Then   ' chossecolor is a API for the color picker popup
        GetColorFromPicker = cc.rgbResult
    End If
End Function
 
I think whatever I read must have been wrong?

From
VarPtr - Variant converter. Returns a LongPtr on 64-bit versions, and a Long on 32-bit versions (4 bytes).

Since an array can be a variant, this should be good. And that descripiton is a bit goofy, as LongPtr is 32 bit on 32 bit systems and 64 bit on 64 bit system and is therefore equivalent to long or longlong respectively.

So then wrapping an array with VarPtr is the right way to pass a pointer for an array in an API call? Anyone?
 
I finally am convinced that the below is the right way to go.

Code:
.lpCustColors = VarPtr(a_lngCustom(0))

and

In a nutshell as I understand it, a VBA array has header information that the C style arrays passed to these functions don't have. So the expected C style array is part of a structure or Type. Getting the pointer to the first element of the array is equivalent to the starting address of the C style array.

So the right way to pass a VBA array to an API is to use the VarPtr of the first element of the array. <-- I can't tell you how many ways I searched for that, hopefully helps someone out later.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top