我目前正在将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();
}