【问题标题】:Get the title of the active window获取活动窗口的标题
【发布时间】:2013-02-06 15:26:33
【问题描述】:

我已经声明了以下 WinAPI 调用

<DllImport("USER32.DLL", EntryPoint:="GetActiveWindow", SetLastError:=True,
    CharSet:=CharSet.Unicode, ExactSpelling:=True,
    CallingConvention:=CallingConvention.StdCall)>
Public Shared Function GetActiveWindowHandle() As System.IntPtr
End Function

<DllImport("USER32.DLL", EntryPoint:="GetWindowText", SetLastError:=True,
    CharSet:=CharSet.Unicode, ExactSpelling:=True,
    CallingConvention:=CallingConvention.StdCall)>
Public Shared Function GetActiveWindowText(ByVal hWnd As System.IntPtr, _
                                            ByVal lpString As System.Text.StringBuilder, _
                                            ByVal cch As Integer) As Integer
End Function

然后,我调用这个子程序来获取活动窗口标题栏中的文本

Public Sub Test()
    Dim caption As New System.Text.StringBuilder(256)
    Dim hWnd As IntPtr = GetActiveWindowHandle()
    GetActiveWindowText(hWnd, caption, caption.Capacity)
    MsgBox(caption.ToString)
End Sub

最后,我收到以下错误

无法在 DLL 中找到名为“GetWindowText”的入口点 'USER32.DLL'

我该如何解决这个问题?

【问题讨论】:

  • 尝试改变 CharSet:=CharSet.Auto
  • this 为我工作。

标签: vb.net winforms winapi dllimport user32


【解决方案1】:
<DllImport("USER32.DLL", EntryPoint:="GetWindowText", SetLastError:=True,
    CharSet:=CharSet.Unicode, ExactSpelling:=True,

您坚持使用 ExactSpelling。问题出在哪里,user32.dll 导出的GetWindowText 有两个 版本。 GetWindowTextA 和 GetWindowTextW。 A 版本使用 ansi 字符串,这是一种传统字符串格式,在 Windows ME 中最后一次使用的默认代码页中编码为 8 位字符。 W 版本使用 Unicode 字符串,编码为原生 Windows 字符串类型 utf-16。 pinvoke marshaller 将根据 CharSet 尝试其中任何一个,但您使用 ExactSpelling := True 阻止了它这样做。所以它找不到GetWindowText,它不存在。

要么使用 EntryPoint := "GetWindowTextW" 要么放弃 ExactSpelling。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-25
    • 2023-04-10
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 2020-01-09
    • 2015-05-03
    • 1970-01-01
    相关资源
    最近更新 更多