Search code examples
vb.netpinvoke

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?

P.S. Sorry for my bad English :)


Solution

  • 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