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
  • 98,240
  • 88
  • 296
  • 433
flavour404
  • 6,184
  • 30
  • 105
  • 136

2 Answers2

2
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TEXTMSGSTR
{
    public IntPtr Sender;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
    public string Text;
}
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
0

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

Also look at my answer to that question

Community
  • 1
  • 1
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 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