开发者

JNAerator Unnamed Union Missing in Structure

I'm trying to get JNAerator to generate some JNA backed Java code from a C shared library and everything is fine except that it failed to generate an unnamed union nested inside a structure.

Example:

typedef struct MY_STRUCTURE {
  union {
    My_Type1 var1;
    My_Type2 var2;
  };

}MY_STRUCTURE;

If I change the header to make t开发者_运维问答he union have a name it will work. But for obvious reasons I can't just change the header without breaking the shared library I'm trying to use.

Any solutions other than changing the header file and shared library to named union?


This is a bug in JNAerator. I've entered an issue in JNAerator project's tracker, so if you want to be notified when the bug is fixed you can star it :

http://code.google.com/p/jnaerator/issues/detail?id=60

Cheers

Olivier (author of JNAerator)


After more research I determined that my problem is a problem with unnamed unions not anonymous unions. JNAerator claims support for anonymous unions but I haven't found anything on unnamed unions. Based on my experience I would say it doesn't support unnamed unions.

Side note: Unnamed unions aren't supported in standard C. Some compilers support it but not most. It is standard in C++.

Anonymous Union:

typedef struct MY_STRUCTURE {
  int i;
  char c; 
  union { 
    My_Type1 var1; 
    My_Type2 var2; 
  }UnionName; 

}MY_STRUCTURE;

Anonymous and Unnamed Union:

typedef struct MY_STRUCTURE { 
  int i;
  char c;
  union { 
    My_Type1 var1; 
    My_Type2 var2; 
  }; 

}MY_STRUCTURE;

Conclusion: Marks solution

Change the unnamed union in the header to a named union then JNAerate the Java code and then change the header back to how it was. Like Mark said it won't change the memory layout, so you can change the name.


Adding a name won't change the memory layout, so you can change the name, JNAerate the Java code (which will map the memory/structure appropriately) and then undo your change. JNA's mapping of the structure to your actual library won't be impacted by adding a name.

edit: your results are a little strange, since the JNAerator documentation clearly addresses the anonymous types problem and seems to indicate that it should produce the correct results.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜