Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing structs from C in C#

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

like image 520
user1562343 Avatar asked Dec 29 '25 04:12

user1562343


1 Answers

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;
};
like image 187
Alois Kraus Avatar answered Dec 30 '25 19:12

Alois Kraus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!