0

我在 C# 中开发了一个应用程序,它从外部应用程序的文本框中提取文本,我正在使用 user32.dll,该应用程序运行良好,但我的问题是 - 外部应用程序的文本框包含 unicode 格式的文本,所以每当我在我的应用程序显示“??????” 文本。我尝试设置 charset.unicode ,并且还使用 RichTextBox 在我的应用程序中显示文本。请让我知道如何从外部应用程序中提取 unicode 文本。

这是我正在使用的代码

 private void button1_Click(object sender, EventArgs e)
    { IntPtr MytestHandle = new IntPtr(0x00060342);

        HandleRef hrefHWndTarget = new HandleRef(null, MytestHandle);

     // encode text into 
        richTextBox1.Text = ModApi.GetText(hrefHWndTarget.Handle);
     }

公共静态类 ModApi {
[DllImport("user32.dll", EntryPoint = "SendMessageTimeout", SetLastError = true, CharSet = CharSet.Unicode)] 公共静态外部 uint SendMessageTimeoutText(IntPtr hWnd, int Msg, int countOfChars, StringBuilder text, uint标志,uint uTImeoutj,uint 结果);

        public static string GetText(IntPtr hwnd)
        {
            var text = new StringBuilder(1024);

            if (SendMessageTimeoutText(hwnd, 0xd, 1024, text, 0x2, 1000, 0) != 0)
            {
                return text.ToString();
            }

            MessageBox.Show(text.ToString());
            return "";
        }
    }
4

1 回答 1

0

您对 WN_GETTEXT 的使用不正确,请阅读文档:http: //msdn.microsoft.com/en-us/library/windows/desktop/ms632627%28v=vs.85%29.aspx

wParam

The maximum number of characters to be copied, including the terminating null character. 

或使用正确的功能:http ://www.pinvoke.net/default.aspx/user32/GetWindowText.html

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam);

public static string GetWindowTextRaw(IntPtr hwnd)
{
    // Allocate correct string length first
    int length = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
    StringBuilder sb = new StringBuilder(length + 1);
    SendMessage(hwnd, WM_GETTEXT, (IntPtr)sb.Capacity, sb);
    return sb.ToString();
}

使其适应 SendMessageTimeOut

于 2013-08-14T09:40:58.560 回答