1

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));
abatishchev
  • 95,331
  • 80
  • 293
  • 426
flavour404
  • 5,964
  • 29
  • 99
  • 134

2 Answers2

2
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TEXTMSGSTR
{
    public IntPtr Sender;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
    public string Text;
}
mmx
  • 402,675
  • 87
  • 836
  • 780
0

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

Also look at my answer to that question

Community
  • 1
  • 1
abatishchev
  • 95,331
  • 80
  • 293
  • 426
  • 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 May 19 '09 at 21:57