1

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

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

我也找不到 UTF-16 函数的任何 P/Invoke 签名SendMessageTimeoutW,所以调用是否等效,如本文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();
}
4

2 回答 2

1

For WM_GETTEXT, wParam (the third parameter to SendMessageTimeout) is

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

You're passing zero.

Also, you're calling WM_GETTEXTLENGTH but not using the return value:

The return value is the length of the text in characters, not including the terminating null character.

Use that to specify the initial size of the StringBuilder. I just confirmed this to work:

    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();
    }
于 2013-06-17T09:08:23.533 回答
0

您需要传递缓冲区的长度。

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)
}
于 2013-06-17T09:14:04.040 回答