I'm attempting to enumerate an MDL StringList obtained using mdlLineStyle_nameGetStringList from VBA. As far as I can tell,I've successfully obtained a StringList, but am stuck on how to use mdlStringList_getMember with VBA.
Here's the MDL function prototype in C...
long mdlStringList_getMember ( char** ppString , long** ppInfoFields , StringList const* pStringList , long memberIndex );
Which I've translated to this function declaration in VBA...
Declare Function mdlStringList_getMember Lib "stdmdlbltin.dll" ( _ ByRef pString As Long, _ ByRef pInfo As Long, _ ByVal list As Long, _ ByVal n As Long) As Long
The problem lies in the first parameter, which in MDL is the address of a char pointer. As you know, VBA doesn't do pointers, so I've passed ByRef a VBA Long. That is, the VBA variable now contains the address of a C-style string. The MDL function is not filling a text buffer, but passing a the address of a text buffer stored in the StringList.
What I can't find is a way to tell VBA to read the C string into a VBA String variable. Does anyone have any ideas on how to make an MDL StringList readable in VBA?
Hi Jon,
I have tested this and started using your declaration example and could get a complete list of the names from line styles available in the active designfile:
Declare Function mdlStringList_getMember Lib "stdmdlbltin.dll" ( _ ByRef pString As Long, _ ByRef pInfo As Long, _ ByVal list As Long, _ ByVal n As Long) As Long Declare Function mdlLineStyle_nameGetStringList Lib "stdmdlbltin.dll" ( _ ByVal pAuxInfo As Long, _ ByVal options As Long) As Long Declare Function SysAllocString Lib "oleaut32" (ByVal CharPtr As Long) As String ' Creates a String from a char * Function CharPtrToString(CharPtr As Long) As String If CharPtr = 0 Then CharPtrToString = "" Else CharPtrToString = SysAllocString(CharPtr) End If End Function Sub StringListMemberTest() Dim i As Long Dim j As Long Dim l As Long Dim s As String Dim lStringlist As Long Dim ppstring As Long Dim ppinfofields As Long lStringlist = mdlLineStyle_nameGetStringList(0, 0) i = 0 Do While (0 = mdlStringList_getMember(ppstring, ppinfofields, lStringlist, i)) s = CharPtrToString(ppstring) j = InStr(1, s, vbNullChar) If j > 0 Then s = Left$(s, InStr(1, s, vbNullChar) - 1) Debug.Print s i = i + 1 Loop End Sub
I hope this helps?
Best regards,Artur
Answer Verified By: Jon Summers
Unknown said:CharPtrToString = SysAllocString(CharPtr)
Thanks for that insight!
We've written an article about line styles and VBA. That article includes a link to download a VBA project. The project includes VBA class clsLineStyleList. That class wraps several MDL functions that deal with line styles, and includes your hint about reading a VBA String from a C pointer..
clsLineStyleList
Regards, Jon Summers LA Solutions