Wednesday, February 3, 2010

Sharing a struct definition between managed and native code

By defining a struct in IDL the struct definition can be used in managed and native code.

When the IDL file is compiled the C++ bindings are automatically generated in a header file.  This allows you to use the struct definition from C++.

When the resulting type library is imported by the type library importer the struct gets created along with any other visible IDL type definitions (e.g., interfaces, coclasses, enums, etc…).  This creation involves translating the IDL into IL metadata (you can see the metadata with ildasm).

Troubleshooting:

  1. Make sure the native code builds a type library and that the type library is registered.
  2. Use OleView to see the types included in the type library.  The struct should show up in this list of types.
  3. Don’t typedef the struct in the IDL; although this will compile the type library importer will ignore the resulting alias.  If you want the typedef for use in native code create it manually.
  4. Use the Object Browser or ILDASM to verify that the type library imported the struct.

No comments :

Post a Comment