如何获取带有超时的窗口文本

本文关键字:超时 窗口 文本 何获取 获取 | 更新日期: 2023-09-27 18:18:53

我目前正在将C++应用程序移植到C#,并且在转换此特定功能时遇到麻烦。这个函数获取给定窗口句柄的文本/标题。

我发现重要的部分是对SendMessageTimeoutW的2个调用,它得到了我假设的窗口的文本,但我无法弄清楚其余的。

我也找不到UTF-16函数SendMessageTimeoutW的任何p/Invoke签名,那么对SendMessageTimeout的调用是否等效,如本文所示?

public static unsafe string GetWindowText(IntPtr hWnd)
{
    // THIS PART MAY NOT BE NEEDED
    string str2 = null;
    WinHookEx* exPtr3;
    WinHookEx* exPtr = @new(4);
    try
    {
        exPtr3 = (exPtr == null) ? null : WinHookEx.{ctor}(exPtr);
    }
    fault
    {
        delete((void*) exPtr);
    }
    WinHookEx* exPtr2 = exPtr3;
    *((int*) exPtr2) = hWnd.ToPointer();
    HWND__* hwnd__Ptr = (HWND__*) hWnd.ToPointer();
    uint modopt(IsLong) num = 0;
    delete((void*) exPtr2);

    // 1st call to SendMessageTimeoutW
    if (SendMessageTimeoutW(hwnd__Ptr, 14, 0, 0, 2, 0x3e8, &num) == 0)
    {
        return null;
    }
    // whats happening here?
    num++;
    uint modopt(IsLong) num2 = num;
    char* chPtr = @new((num2 > 0x7fffffff) ? uint.MaxValue : ((uint) (num2 * 2)));
    chPtr[0] = ''0';
    // 2nd call to SendMessageTimeoutW
    if (SendMessageTimeoutW(hwnd__Ptr, 13, num, (int modopt(IsLong)) chPtr, 2, 0x3e8, &num) == 0)
    {
        return null;
    }
    str2 = new string(chPtr);
    delete((void*) chPtr);
    return str2;
}
我暂时将这个函数移植到C#,但它总是返回一个空白字符串。我甚至尝试用new StringBuilder(256)初始化字符串生成器,但它仍然不起作用。

我做错了什么?

public static unsafe string GetWindowText(IntPtr hWnd){
    // send WM_GETTEXTLENGTH 
    if (SendMessageTimeout(hWnd, 14, 0, 0, 2, 0x3e8, IntPtr.Zero) == 0){
        return null;
    }
    // send WM_GETTEXT
    StringBuilder sb = new StringBuilder();
    if (SendMessageTimeout(hWnd, 13, 0, sb, 2, 0x3e8, IntPtr.Zero) == 0){
        return null;
    }
    return sb.ToString();
}

如何获取带有超时的窗口文本

对于WM_GETTEXT, wParam (SendMessageTimeout的第三个参数)为

要复制的最大字符数,包括结束的null字符。

你过零了

此外,您正在调用WM_GETTEXTLENGTH,但不使用返回值:

返回值是文本的长度,以字符为单位,不包括结束的空字符。

用来指定StringBuilder的初始大小。我刚刚确认这是工作:

    static void Main(string[] args) {
        var p = Process.GetProcessById(3484);
        var h = p.MainWindowHandle;
        string s = GetWindowTextTimeout(h, 100 /*msec*/);
    }

    [DllImport("User32.dll", SetLastError = true)]
    public unsafe static extern int SendMessageTimeout(
      IntPtr hWnd,
      uint uMsg,
      uint wParam,
      StringBuilder lParam,
      uint fuFlags,
      uint uTimeout,
      void* lpdwResult);
    const int WM_GETTEXT = 0x000D;
    const int WM_GETTEXTLENGTH = 0x000E;
    public static unsafe string GetWindowTextTimeout(IntPtr hWnd, uint timeout)
    {
        int length;
        if (SendMessageTimeout(hWnd, WM_GETTEXTLENGTH, 0, null, 2, timeout, &length) == 0) {
            return null;
        }
        if (length == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder(length + 1);  // leave room for null-terminator
        if (SendMessageTimeout(hWnd, WM_GETTEXT, (uint)sb.Capacity, sb, 2, timeout, null) == 0) {
            return null;
        }
        return sb.ToString();
    }

需要传递缓冲区的长度

int size;
SendMessageTimeout((int)hWnd, WM_GETTEXTLENGTH, 0, 0, 2, 0x3e8, &size).ToInt32();
if (size > 0)
{
    StringBuilder sb = new StringBuilder(size + 1);
    SendMessageTimeout(hWnd, WM_GETTEXT, sb.Capacity, sb, 2, 0x3e8, IntPtr.Zero)
}