EnumString Helper (COM Objects)

Just starting out? Need help? Post your questions and find answers here.
User avatar
mk-soft
Always Here
Always Here
Posts: 5387
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

EnumString Helper (COM Objects)

Post by mk-soft »

With the query of the EnumString\Next you pass an array which can be entered with the pointer to the strings LPOLESTR.
But I am not sure how to clear the memory for these strings.

LPOLESTR is not a BSTR and therefore the memory is not cleared with SysFreeString.

A CoTaskFreeMem on the array will not return an error. But this can not be true.

Update
Should be right about now.

LPOLESTR with COM-Objects are deleted with CoTaskFreeMem.
After description CoTaskFreeMem should not have a return value. But if it is zero, you can read the error with GetLastError.

Code: Select all

;- EnumString Helper

Procedure EnumStringToArray(*EnumString.IEnumString, Array Result.s(1), AutoRelease = #True) ; Result Count
  Protected r1, cnt, size, index, offset
  
  size = 100
  Dim pString.i(size)
  
  offset = 0
  *EnumString\Reset()
  Repeat
    r1 = *EnumString\Next(size, @pString(), @cnt)
    If r1 <> #S_OK And r1 <> #S_FALSE
      Debug FormatMessage(r1)
      ProcedureReturn 0
    EndIf
    
    If cnt > 0
      ReDim Result(offset + cnt - 1)
      For index = 0 To cnt - 1
        Result(offset + index) = PeekS(pString(index))
        If CoTaskMemFree_(pString(index)) = 0
          Debug "EnumString Error: " + FormatMessage(GetLastError_())
        EndIf
      Next
      offset + cnt
    EndIf
  Until cnt = 0
  
  If AutoRelease
    *EnumString\Release()
  EndIf
  
  ProcedureReturn ArraySize(Result()) + 1
  
EndProcedure
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive