How can I recognize/parse the structure of UDT, returned from C dll, if I get the pointer to this structure in a VBA function?

A C dll returns a pointer (lngPointer) to a big UDT structure (appr. 60 primitive types fields, differently groupped), created by this dll. I don't know exactly, what are the fields of this structure and what size do they have. Can I recognize/specify it from VBA code somehow?
In particular, to find the size of the whole structure, and the sizes of all fields, as the follow each other?
Who is Participating?
>>Or to get the size
I don't believe you'll be able to determine the length of the structure without knowing the structure itself since it'd just be a sequence of bytes for all of the variables. From memory-dumps, that's true for VB and *should* be the same for C.

>>like for BSTR strings
UDTs define the structure so you can easily get the length from combining the bytes used for all of the variables (and possible padding). Strings don't have that method since they can vary in size so they require a method that BSTR implements (or null-termination for C). Because of that, I don't think UDTs have additional overhead just to offer another means to get their lengths. I'm not entirely sure though so there's still hope that somebody knows. :)

>>This question, in fact, was derivated from another thread
You may want to keep with your original plan in finding the correct UDT. I'm not too familiar with C/C++ so I can't offer much help. Sorry.
You may have to contact the author(s) of the DLL for the correct UDT structure if they don't offer documentation. There may be reverse-engineering methods but if you have permission/license(s) to use the DLL, I see no problem with asking for the structure. Once you have it, you can just define it within your project and CopyMemory() from the pointer using the length of the (correctly) defined UDT. An example below.

Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Private Type MyUDT
    var1 As String
    var2 As Long
End Type
Private Sub Form_Load()
    Dim udtIn As MyUDT, udtOut As MyUDT
    Dim lngPtr As Long
    'assign values to udtIn
    udtIn.var1 = "test"
    udtIn.var2 = "1234"
    'get the pointer to udtIn
    lngPtr = VarPtr(udtIn)
    'copy udtIn to udtOut using it's pointer
    Call CopyMemory(ByVal VarPtr(udtOut), ByVal lngPtr, Len(udtOut))
    'test it..
    Call MsgBox(udtOut.var1 & "," & udtOut.var2)
End Sub
inversojvoAuthor Commented:
> You may have to contact the author(s) of the DLL for the correct UDT structure

Yes, this is a possible way, but I'm interested namely in getting this info without documentation and addressing the authors. In my case the addressing the author is a administrative way, and the query will go, I think, 3 days one way, and then 3 days back :).

The idea was to recognize somehow or the end of the structure (f.ex. to read byte after byte untill the end of the structure will be found - but I don't know, how the end of the structure is marked - if it's marked at all). Or to get the size, like for BSTR strings, when the pointer points at the beginning of the string, but just before the beginning the length of the BSTR string is marked.

So, I need or the answer, how to do it technically, or the provement, that it's impossible to do from VBA.

This question, in fact, was derivated from another thread:

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.