简体   繁体   中英

GetWindowText() Doesn't Work For Greek

I'm using this code to get a list of open windows:

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

Usage: EnumWindows(Callback, IntPtr.Zero)

It works BUT if a window with a greek title is opened ex. 'Μουσική' this code outputs 'œ¿Åùº® '. As you can see something is wrong. Is there a way to fix this?

PS Sorry for my bad English :)

Ok I think i found the solution: Use GetWindowTextA instead of GetWindowText.

@Trevor suggested this in the comments of the question and I tried it but it didn't work. Somehow I managed to make it work.

Thanks to everybody who tried to help :). Here is the working code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM