3

Is it possible to import in C# structs definitions from C dlls?

In all the examples I saw were to redefine the structs in C#. What if the struct changes? I need to alter in two locations in my project...

struct MyCStruct { unsigned long A; unsigned long B; unsigned long C; }; 

and in C#:

 [StructLayout(LayoutKind.Sequential)] internal struct MyCStructAgain { public uint A; public uint B; public uint C; } 

If It's not possible to reuse the definitions, why is that?

Thanks

5
  • 2
    "If It's not possible to reuse the definitions, why is that?" - because it's a different language? :-) Commented Jul 30, 2012 at 8:46
  • Because C and C# are two different languages! Commented Jul 30, 2012 at 8:48
  • Yeah, but .NET makes all languages work together seamlessly... Commented Jul 30, 2012 at 8:48
  • Short answer, no. The only thing that can be seen in an unmanaged .dll are the exported function definitions. Commented Jul 30, 2012 at 8:52
  • .Net makes all languages work together seamlessly only if language is .net compliant. Whereas C is not. Commented Jul 30, 2012 at 8:57

1 Answer 1

2

You could compile your structs as C++/CLI where the compiler generates the managed counterparts for you and you can reference them then. You would need an ifdef to prepend value to make it a .NET struct.

#ifdef CLIEXPORT #define value #endif CLIEXPORT struct MyCStruct { unsigned long A; unsigned long B; unsigned long C; }; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.