Specify the character set and try
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
public struct person
{
[UnmanagedType.ByValTStr, SizeConst=20)]
public string name;
public int id;
}
> Hi,
>
[quoted text clipped - 28 lines]
>
> Thanks.
Hi,
> Hi,
>
[quoted text clipped - 17 lines]
> public int id;
> }
Using struct:
---
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public struct person
{
[UnmanagedType.ByValTStr, SizeConst=20)]
public string name;
public int id;
}
[DllImport("TestDLL.dll")]
public static extern void SetStructData( ref person pp ); // you can see
changes made to struct
Using class:
---
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public class person
{
[UnmanagedType.ByValTStr, SizeConst=20)]
public string name;
public int id;
}
[DllImport("TestDLL.dll")]
public static extern void SetStructData( [In,Out] person pp ); // here
[In,Out] is required if you want to see changes!
HTH,
greetings
> When I pass the struct to unmanaged code, the data is all junk.
>
> Can someone tell me what I am doing wrong.
>
> Thanks.
Thomas - 26 Jul 2004 11:24 GMT
Thanks for your reply.
It did not work. I just cannot figure out why char[] is all junk data.
> Hi,
>
[quoted text clipped - 58 lines]
> >
> > Thanks.
BMermuys - 26 Jul 2004 12:25 GMT
> Thanks for your reply.
> > > [StructLayout(LayoutKind.Sequential)]
> > > public class person
[quoted text clipped - 3 lines]
> > > public int id;
> > > }
> It did not work. I just cannot figure out why char[] is all junk data.
The marshalling of such a simple struct itself is no problem, I use it all
the time.
Something else must be wrong, maybe you're translating the wrong struct or
the function isn't working properly.
HTH,
greetings
> > Hi,
> >
[quoted text clipped - 58 lines]
> > >
> > > Thanks.