How to implement a pointer to an array of structures for a declare?

Hi,

A declare I’m trying to construct in Xojo uses a MIXERLINECONTROLSA structure. Its last parameter (pamxctrl) is documented as such:

“Pointer to one or more MIXERCONTROL structures to receive the properties of the requested audio line controls.”[…]

If I get it right, I must instruct Xojo to receive an array of structures, where the size (number of items) isn’t known before the declare is executed.

How can this be achieved?

Second question: there’s a union keyword in the declare parameters. Am I safe in defining that as a structure?

TIA

In xojo, the hard way:

// You need a count and the size of ptr (4-8 bytes depending on system bits)
Var mystruct as struct_t

If count > 0 Then
Var ptrOffset As Integer = 0
For i As Integer = 0 To (count -1)
// Now you move the pointer position:
mystruct = myPtrToStructArray.Ptr(ptrOffset).struct_t
ptrOffset = (CType(i, Integer) * 8) 
Next i
End if

Something like this. pseudocode

Thanks, however I fail to see how to link this with my current code (I’m sure your code will help me beyond now, though).

Here’s the declare I’ve just written:

Declare Function mixerGetLineControls lib "Winmm" (hmxobj as Ptr,pmxlc as MixerLineControls,fdwControls as Integer) as Integer

Var i As Integer=mixerGetLineControls(MixerHandle,MixerLineObj,3)

MixerLineControls is my defined structure. The documentation for the API tells me the last field of this structure may be an array of another structure. How can I define the field of MixerLineControls as an array rather than a single instance?

It’s a pointer to an array of structures, not an array itself. So define it as a Ptr in the structure, and then use @DerkJ’s code to iterate over the array being pointed to:

' make the struct parameter byref
Declare Function mixerGetLineControls lib "Winmm" (hmxobj as Ptr, BYREF pmxlc as MixerLineControls,fdwControls as Integer) as Integer 

Var count As Integer=mixerGetLineControls(MixerHandle,MixerLineObj,3)

' Ptr.StructureName converts the Ptr into the named Xojo structure type
Var p As Ptr = MixerLineObj.pamxctrl
Var mystruct as MIXERCONTROL = p.MIXERCONTROL

If count > 0 Then
  Var ptrOffset As Integer = 0
  For i As Integer = 0 To (count -1)
    // Now you move the pointer position:
    mystruct = mystruct.Ptr(ptrOffset).MIXERCONTROL
    ' mystruct is now a copy of the MIXERCONTROL structure from the array

    ' find the offset of the next array element
    ptrOffset = (CType(i, Integer) * 8) 
  Next i
End if
4 Likes

Ah, this is the key, many thanks! Didn’t know one can convert a ptr to a structure. Trying this right now…

1 Like