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);
     }

public static class ModApi {
[DllImport("user32.dll", EntryPoint = "SendMessageTimeout", SetLastError = true, CharSet = CharSet.Unicode)] public static extern 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 に答える