3

I have seen this question asked a few times on this site, and others, and have yet to see a legitimate and actual answer aside from posting links to the Ruby docs that don't answer the question either.

Is it possible, and if so how, to have a union of structs within another struct using Ruby Fiddle? Everything within the documentation simply indicates how to create structs/unions using primitive types, but not how it would be possible to nest them, as is a common convention to do.

The Fiddle::CParser cannot parse anything other than primitive types, as well as manual creation using signatures.

I have attempted to simply use TYPE_VOIDP and use that pointer to as a location to the address create the struct, which I was fairly sure should work, but I get only junk, as if the address is incorrect. I imagine this is because not enough memory is allocated, but I since the structs within the union are different sizes, I cannot allocate it ahead of time, leading me in circles.

The basic format is something like this: (this is psuedo-code just to give idea)

struct1 = [float, float, float, int]
struct2 = [int, int]
struct3 = [float, enum, enum, float, double, float]
structMaster = [
    int, // Determines the type of the within the union
    char[16],
    char[16],
    union[struct1, struct2, struct3]
]

I have looked extensively over all the Fiddle documentation, and it never indicates if this is even possible. I am familiar with Fiddle::CStructBuilder and related classes, and posting links to it is not the answer, as I have seen in other posts asking a similar question.

I have successfully done it accomplished this with old Win32API and using binary blobs, but am now trying to accomplish this with Fiddle, and am getting myself very frustrated.

EDIT: I did manage to find some success by calculating the offset and reading the memory directly, and casting the pointer to the proper type, but I would still love to know if there is a way to do this cleaner with a nested struct, instead of the "hackish" way I am accomplishing it.

ForeverZer0
  • 2,379
  • 1
  • 24
  • 32

0 Answers0