WM_SETTEXT writes Chinese letters

312 Views Asked by At

I want to set the text of my Edit Control. When I do, the new content is Chinese.

For example, this:

[DllImport("user32.dll")]
public static extern int SendMessageW([InAttribute] System.IntPtr hWnd, int Msg, int wParam, string lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern IntPtr GetFocus();

IntPtr c = GetFocus();
SendMessageW(c, 12, 0, "Test"); //Notice that 12 = WM_SETTEXT

sets my Edit Control to this: 敔瑳

1

There are 1 best solutions below

0
Richard Deeming On BEST ANSWER

According to pinvoke.net, you need a MarshalAs attribute on the string parameter:

[DllImport("user32.dll")]
public static extern int SendMessageW(
    [InAttribute] System.IntPtr hWnd, 
    int Msg, 
    int wParam, 
    [MarshalAs(UnmanagedType.LPWStr)] string lParam);