0

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: 敔瑳

  • What is the value you have in `Clipboard.GetText()` ? – Chetan Jul 23 '18 at 13:52
  • @ChetanRanpariya Sorry, it was "Test", I forgot to change this part of the question. Now with the code above the same thing happens – Markus Rechberger Jul 23 '18 at 13:53
  • @ChetanRanpariya Also I added the declaration of SendMessageW – Markus Rechberger Jul 23 '18 at 14:03
  • What is your `Edit` control? Why do you need to use `SendMessage` to set its text? What is `GetFocus()` doing there? Why `SendMessageW` and not `SendMessage` with the usual: `[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]`. If you give some context and some details on the objects involved, the answer can come quickly. – Jimi Jul 24 '18 at 01:42

1 Answers1

0

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);
Richard Deeming
  • 29,830
  • 10
  • 79
  • 151