Using PostMessage to send Unicode characters

纵然是瞬间 提交于 2019-12-02 04:03:26

问题


I am using PostMessage to send input to a flash object in another application. It works fine until I try to send a unicode character. In this example:

Michael’s Book

The apostrophe is not really that, it is not an ASCII 39, but rather a unicode U+2019. By the time it is sent across 1 character at a time, it is lost as a unicode value and lands as the raw characters making up the unicode

Michael’s Book

If I copy and paste into that window it moves fine, and if I load a text file into that window it loads fine. So the receiving window is able to receive unicode, but the way I am sending it must not be correct. Any help would be greatly appreciated.

  private void SendKeysToForm(string Message)
    {
        for (int i = 0; i < Message.Length; i++)
        {
            PostMessage(hwnd, WM_CHAR, (IntPtr)Message[i], IntPtr.Zero);
        }
    }

回答1:


Per the MSDN documentation, to send Unicode, you need to use PostMessageW.

It's the same method signature, just import the name PostMessageW and execute that.

UPDATE

As Hans very well stated, an even better approach would be to set the CharSet of the DllImport:

[DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
private static extern bool PostMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

This should cause the framework to ultimately import PostMessageW.

Thanks a lot Hans!



来源:https://stackoverflow.com/questions/38223948/using-postmessage-to-send-unicode-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!