There’s a giant grey rat outside of my window and a bunch of non-union workers laboring in the rain. The union guy who’s guarding the rat and giving away flyers got too cold and went inside the building. But I digress, the post is about working with Unions in Java Native Access (JNA).
Preamble
I was trying to retrieve Active Directory forest trust information via DsGetForestTrustInformationW. The function takes a pointer to a PLSA_FOREST_TRUST_INFORMATION, a pointer to a pointer to an LSA_FOREST_TRUST_INFORMATION structure. So far so good, we just need to pay attention to the several levels of indirection: whenever we want the value of a pointer to something, it’s a ByReference.
LSA_FOREST_TRUST_INFORMATION is a structure that contains a RecordCount number of PLSA_FOREST_TRUST_RECORD items. Those are pointers, so Entries is an array of pointers. Since we want the value of a pointer, we use ByReference again.
A pointer to a record is simply a structure that contains a pointer to the record.
Union inside a Structure?
Still with me? The record is declared like this:
Note that MSDN has a mistake here, missing the Time field, which gave me lots of headache and wasted hours of my time. Got to use definitions in platform SDK.
This is a union. How do you declare this in JNA?
A union is just like a structure, except that every field lives at an offset zero. In JNA, you must tell the union which field to use before reading the value.
In our case we override read() and set the type depending on the ForestTrustType value. Then re-read the union from memory. Voila.
Notes
Committed to JNA under com.sun.jna.platform.win32.