Interoperability between C ++ and C #

I have this one 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 posting as part of the message COPYDATASTRUCT

from unmanaged to managed code. What would be the correct construct struct

on the C # side, as C # doesn't wchar_t

? I've tried string and so on, but of course errors appear!

Can someone give me some ideas on how to marshal this and also am I new to this?

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

      

+1


a source to share


2 answers


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TEXTMSGSTR
{
    public IntPtr Sender;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
    public string Text;
}

      



+2


a source


Try System.Runtime.InteropServices.UnmanagedType LPTStr

or ByValTStr

.



Also look at my answer to this question

0


a source







All Articles