Link to home
Start Free TrialLog in
Avatar of inversojvo
inversojvoFlag for Germany

asked on

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?
Avatar of zzzzzooc
zzzzzooc

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.

Form1:
----------------------------
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
Avatar of inversojvo

ASKER

> 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: https://www.experts-exchange.com/questions/21161325/Calling-dll-from-VBA-and-getting-a-big-complex-UDT-as-a-return-value-bad-calling-convention-error.html

ASKER CERTIFIED SOLUTION
Avatar of zzzzzooc
zzzzzooc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial