tags:

views:

603

answers:

2

I know how to extern methods in the .dll, how do I extern structs?

I want to create a C method such as

extern __declspec(dllexport) myStructure getStruct();

where myStructure is something like

typedef struct
{
   int A;
   int B;
   char C;
} myStructure;

How can I call getStruct() from a piece of C# code without first defining the same exact struct in C#? I want to keep a centralized declaration of myStructure so that I only have to make code changes in one place.

Thanks a bunch!

+2  A: 

What about doing it another way? Define the struct in C# in T4 and then cross gen to the new format in each of the projects:

ie. base.tt

// C# definition of struct

in gencsharp.tt in the C# project include "base.tt" ... output the C#

in gencplusplus.tt in the CPP project include "base.tt" ... use C# to gen a cpp .h file

Preet Sangha
A: 

I've done a bit of P/Invoke with C# and I've always had to define structs in C# that correspond to the Win32 struct by definition. The runtime uses the struct definition to marshal the data from unmanaged to managed. Preet's answer is probably the best.

Chris Thompson