GetWindowText()对希腊语不起作用

时间:2017-02-26 16:48:27

标签: vb.net pinvoke

我正在使用此代码获取打开的窗口列表:

Delegate Function EnumWindowDelegate(ByVal hWnd As IntPtr, ByVal Lparam As IntPtr) As Boolean
Private Callback As EnumWindowDelegate = New EnumWindowDelegate(AddressOf EnumWindowProc)
Private Function EnumWindowProc(ByVal hWnd As IntPtr, ByVal Lparam As IntPtr) As Boolean
    If IsWindowVisible(hWnd) Then
        Dim TheLength As Integer = GetWindowTextLengthA(hWnd)
        Dim TheReturn(TheLength * 2) As Byte
        GetWindowText(hWnd, TheReturn, TheLength + 1)
        Dim TheText As String = ""
        For x = 0 To (TheLength - 1) * 2
            If TheReturn(x) <> 0 Then
                TheText &= Chr(TheReturn(x))
            End If
        Next
        If TheText <> "" Then
            ListBox1.Items.Add(TheText & " (" & CStr(hWnd) & ")")
        End If
    End If
    Return True
End Function

Private Declare Function EnumWindows Lib "User32.dll" (ByVal WNDENUMPROC As EnumWindowDelegate, ByVal lparam As IntPtr) As Boolean
Private Declare Auto Function GetWindowText Lib "User32.dll" (ByVal Hwnd As IntPtr, ByVal Txt As Byte(), ByVal Lng As Integer) As Integer
Private Declare Function IsWindowVisible Lib "User32.dll" (ByVal hwnd As IntPtr) As Boolean
Private Declare Function GetWindowTextLengthA Lib "User32.dll" (ByVal hwnd As IntPtr) As Integer

用法:EnumWindows(Callback, IntPtr.Zero)

如果打开带有希腊标题的窗口,它可以。 'Μουσική'此代码输出'¿¿Åùº®'。你可以看到一些错误。有办法解决这个问题吗?

P.S。抱歉我的英语不好:)

1 个答案:

答案 0 :(得分:0)

好的我认为我找到了解决方案:使用GetWindowTextA而不是GetWindowText。

@Trevor 在问题的评论中提出了这一点,我尝试了但是它没有用。不知何故,我设法让它工作。

感谢所有试图帮助的人:)。这是工作代码:

Delegate Function EnumWindowDelegate(ByVal hWnd As IntPtr, ByVal Lparam As IntPtr) As Boolean
Private Callback As EnumWindowDelegate = New EnumWindowDelegate(AddressOf EnumWindowProc)
Private Function EnumWindowProc(ByVal hWnd As IntPtr, ByVal Lparam As IntPtr) As Boolean
    If IsWindowVisible(hWnd) Then
        Dim TheLength As Integer = GetWindowTextLengthA(hWnd)
        Dim TheReturn(TheLength * 2) As Byte
        GetWindowText(hWnd, TheReturn, TheLength + 1)
        Dim TheText As String = ""
        For x = 0 To (TheLength - 1) * 2
            If TheReturn(x) <> 0 Then
                TheText &= Chr(TheReturn(x))
            End If
        Next
        If Not TheText = "" Then
            ListBox1.Items.Add(TheText & " (" & CStr(hWnd) & ")")
        End If
    End If
    Return True
End Function

Private Declare Function EnumWindows Lib "User32.dll" (ByVal WNDENUMPROC As EnumWindowDelegate, ByVal lparam As IntPtr) As Boolean
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal Hwnd As IntPtr, ByVal Txt As Byte(), ByVal Lng As Integer) As Integer
Private Declare Function IsWindowVisible Lib "User32.dll" (ByVal hwnd As IntPtr) As Boolean
Private Declare Function GetWindowTextLengthA Lib "User32.dll" (ByVal hwnd As IntPtr) As Integer