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;
Raghuveer
  • 2,630
  • 3
  • 29
  • 59

2 Answers2

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.

Zenexer
  • 18,788
  • 9
  • 71
  • 77
  • 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 – Raghuveer Feb 08 '12 at 06:38
  • 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. – Zenexer Feb 08 '12 at 06:39
  • 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. – jeffora Feb 08 '12 at 06:44
  • 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.) – Zenexer Feb 08 '12 at 06:57
  • What is the equivalent C# structure for the fallowing c structure typedef struct p45_cldxed24 { procedure p45_cldxed[8]; }p45_cldxed24; – Raghuveer Feb 08 '12 at 07:01
  • Create a new question. That could be useful information for someone else; we wouldn't want to hide it in comments. – Zenexer Feb 08 '12 at 07:13
  • http://stackoverflow.com/questions/9189254/equivalent-c-sharp-structure-for-a-c-structure – Raghuveer Feb 08 '12 at 07:24
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.

Community
  • 1
  • 1
Ancallan
  • 111
  • 4
  • What is the equivalent C# structure for the fallowing c structure typedef struct p45_cldxed24 { procedure p45_cldxed[8]; }p45_cldxed24; – Raghuveer Feb 08 '12 at 07:12
  • [link](http://stackoverflow.com/questions/31854/how-to-marshal-an-array-of-structs-net-c-c) Try this one, it's one that's helped me with similar problems – Ancallan Feb 08 '12 at 07:21