i basically want to take int name and string age from user in c# and send it to dll method written in c which take int and char[50] arguments in it and return string .i crea
Within a struct, to marshal char arrays defined as char[] you should use the UnmanagedType.ByValTStr instead.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Argument
{
public int age;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string name;
}
You need to change your struct definition of Argument to either
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Argument
{
public int age;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 50)]
public string name;
}
- or -
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
unsafe public struct Argument
{
public int age;
fixed char name[50];
}
You might also find the article Default Marshaling for Strings helpful.