views:

206

answers:

2

I have this struct in C++:

struct TEXTMSGSTR
{
    HWND Sender;
    wchar_t Text[255];
    //wchar_t *Text;
};

and in C#:

public struct TEXTMSGSTR
{
    public IntPtr Sender;
    public ? Text;
}

which I am sending as part of a COPYDATASTRUCT message from unmanaged to managed code. What would be the correct construction of the struct on the C# side as C# does not have wchar_t? I have tried string etc. but of course errors appear!

Can anybody give me some ideas about how to marshal this as well as I am new to this stuff?:

TEXTMSGSTR tx = (TEXTMSGSTR)Marshal.PtrToStructure(cds.lpData, typeof(TEXTMSGSTR));
A: 

Try System.Runtime.InteropServices.UnmanagedType LPTStr or ByValTStr.

Also look at my answer to that question

abatishchev
I'll check out the link, I really need to understand this stuff. I'm fine with posting the messages its just when I get to the marshalling stuff I get really confused.Thanks.
flavour404
+2  A: 
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TEXTMSGSTR
{
    public IntPtr Sender;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
    public string Text;
}
Mehrdad Afshari
Thanks Mehrdad, that worked great.I really need to read the articles on this, its not easy! Thanks.
flavour404