1

i have one 'C' 'DLL' which can take structures as input, i have to call that dll from my c# program

fallowing are the sample structures i have in c, i have to marshal those structures in my c# code

is i'm doing correct or wrong?

Actual C structures:

typedef struct procedure { char code[8]; }procedure; typedef struct datefield { char date[10]; }datefield; typedef struct p45_clsgs { procedure p45_clsg; datefield p45_clsgdte; }p45_clsgs; 

C#:

[StructLayout(LayoutKind.Sequential), Serializable] struct procedure { //char code[]; [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 8)] public string code; } [StructLayout(LayoutKind.Sequential), Serializable] struct datefield { //char date[10]; [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 10)] public string date; } struct p45_clsgs { public procedure p45_clsg; public datefield p45_clsgdte; } 

Is my C# structs are correct for the C structs?

What is the equivalent C# structure for the fallowing c structure

typedef struct p45_cldxed24 { procedure p45_cldxed[8]; }p45_cldxed24; 
7
  • that is a big process, i have to create at least 100 structures in c#, so before starting i want to confirm myself Commented Feb 8, 2012 at 6:14
  • Best advice in that case, is to write each struct 1 by one, and compare the sizes in both C and C#. Commented Feb 8, 2012 at 6:27
  • 1
    Heck no. Wing it. Once it breaks, then you can start going crazy. Commented Feb 8, 2012 at 6:43
  • 1
    @Zenexer: And then you have to do that whole exercise anyways... Commented Feb 8, 2012 at 6:51
  • @leppie Yeah, but you feel productive for a good hour. Commented Feb 8, 2012 at 6:55

2 Answers 2

4

(Scratch my original answer)

You need to have this at the top of each struct:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 

Note the CharSet named parameter. That's important, or you'll end up with wchar_t arrays instead of char arrays.

Sign up to request clarification or add additional context in comments.

7 Comments

is my p45_clsgs structure is correct? if yes, if i have to declare array of procedure in some another struct how should i do
I lied, it isn't correct! I updated my answer accordingly. You should make a new question with an example of a C structure that you need translated for that.
CharSet.Ansi is key. In addition, Marshal.SizeOf(typeof(x)) where x is one of your structs should return the right size - 8, 10 and 18 respectively for your examples. Without CharSet.Ansi they will probably be 16, 20 and 36.
As jeffora said, you can use Marsha.SizeOf to get the real size. Don't use sizeof in an unsafe context--I'm pretty sure it will see the strings as pointers, though you shouldn't take my word for that. (It simply won't work at all in a safe context.)
What is the equivalent C# structure for the fallowing c structure typedef struct p45_cldxed24 { procedure p45_cldxed[8]; }p45_cldxed24;
|
0

It's difficult to say for certain, given that you don't know if it's working or not! Regardless, it looks correct to me.

My recommendation would be to simply test out that structure you've shown here, make sure that it seems to be operating as you'd expect. Then begin to create the other 100 or so structures.

If it helps, take a look at Marshal C++ struct array into C# and the links therein.

2 Comments

What is the equivalent C# structure for the fallowing c structure typedef struct p45_cldxed24 { procedure p45_cldxed[8]; }p45_cldxed24;
link Try this one, it's one that's helped me with similar problems

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.