Jump to content
Sign in to follow this  
Ascend4nt

Fix for unreadable '?' text from WinGetText, ControlGetText

Recommended Posts

_WinGetTextFixed & _StringFixANSIInWide

 

Update: Text problems no longer exist as of v3.3.9.11

 

Sick of getting rows of '?' from WinGetText, ControlGetText, and other AutoIt functions? The problem lies in the way the text is encoded - AutoIt 'sees' a Windows wide string (UTF-16), but in reality it grabs a string of 8-bit ANSI characters and shoves them into a 16-bit wide string. Here's a small example:

Original string "x4" with null-term as an ANSI string sequence:

0x78 0x34 0x00

Same string interpreted as Unicode (reversal is due to little-endian encoding):

0x3478 0x00

The latter string produces the Unicode character "㑸" (hopefully that shows up properly as an asian character)

So, as you can see, from the software side, "x4" compacted into a UTF-16 string still produces an acceptable string, even though it wasn't intended to be an asian character.

Now, where the '?'s show up is during conversion of the same string back to ANSI. You can see the result of this by doing a ConsoleWrite(), which itself causes an implicit Wide-ANSI conversion to occur. Also, pasting text into another text editor that's using an ANSI code page will give the same results. (A workaround for the conversion is to set the encoding to Unicode)

Of course, even if you can display the wide characters correctly, it's not the intended result - you want what the window in question produces. Plus, there are many invalid UTF-16 code points that can be produced, and certain values will be interpreted as a pair of UTF-16 code points.. which just gets messy.

So how do we fix this?

The easiest workaround, when the format of the string is known to be ANSI beforehand is:

$sStr = BinaryToString(StringToBinary($sStr, 2), 1)

However, determining whether a window or control will give you ANSI or Wide characters is another issue.. I had written the _StringFixANSIInWide function below to try and detect ANSI-in-Wide situations, and return the corrected string.. Unfortunately its a bit naive in its implementation (see update next paragraph), but it works okay for most situations. I can see it failing only when a Wide string contains nothing but >8-bit characters (usually there is a lot of 7-bit ASCII characters encoded in the lower part of true Wide strings). In theory that is an extremely unlikely situation (unless there are no ASCII characters in a string).

Update - _WinGetTextFixed() alternative:

Now I've identified the core issue is with the call to SendMessage (using WM_GETTEXT): The function will return either ANSI or Unicode (wide) characters, and its up to our code to determine, based on the length, whether it is an ANSI or wide string. We can then return the correct string. See _WinGetTextFixed() below.

Note that AutoIt's WinGetText() function returns text for more than just the window - it returns text for various controls also. While this can be beneficial, it also introduces issues when different controls return Wide or ANSI characters. The result is basically a contaminated string, meaning it packs ANSI characters into UTF-16 sequences and includes legitimate Wide characters in the same string. This is unacceptable and really hard to work with. That's a reason there's now a 'force conversion' parameter in _StringFixANSIInWide. I would really really emphasize that it's best to not use WinGetText() and instead opt for _WinGetTextFixed instead.

I may suggest this behavior be fixed in the AutoIt source code, but it looks like all the Devs are gone :o

Anyway, enjoy!

Example: Notepad 2

$hWnd = WinGetHandle("[CLASS:Notepad2]")
$hControl = ControlGetHandle($hWnd,"","[CLASS:Scintilla]")
; Previous fix:
;~ $sText = _StringFixANSIInWide(ControlGetText($hWnd,"",$hControl))
$sText = _WinGetTextFixed($hControl)
ConsoleWrite("Text:"&$sText&@CRLF)

Example: SciTE

$hWnd = WinGetHandle("[CLASS:SciTEWindow]")
$hControl=ControlGetHandle($hWnd,"","[CLASS:Scintilla; INSTANCE:1]")
; Previous fix:
;~ $sText = _StringFixANSIInWide(ControlGetText($hWnd,"",$hControl))
$sText = _WinGetTextFixed($hControl)
ConsoleWrite("Text:"&$sText&@CRLF)

Example: Programmer's Notepad 2

$hWnd = WinGetHandle("[REGEXPTITLE:Programmer's Notepad]")
$hControl = ControlGetHandle($hWnd, "", "[CLASS:ScintillaWindowImpl; INSTANCE:1]")
; Previous fix:
;~ $sText = _StringFixANSIInWide(ControlGetText($hWnd,"",$hControl))
$sText = _WinGetTextFixed($hControl)

ConsoleWrite("Text:"&$sText&@CRLF)
; ===================================================================================================
; Func Func _WinGetTextFixed($hWnd)
;
; Function to get Text of a window or control
;  This is an alternative to AutoIt's 'WinGetTitle', 'WinGetText', and 'ControlGetText',
;  which have issues with reading ANSI text from some windows
;
; Author: Ascend4nt
; ===================================================================================================

Func _WinGetTextFixed($hWnd)
    If Not IsHWnd($hWnd) Then Return SetError(1,0,'')

    Local $aRet, $stWideText, $stANSIText, $sText
    Local $nGetTextLen, $nHalfLen

    ; WM_GETTEXTLENGTH 0x0E
    $aRet = DllCall("user32.dll", "long", "SendMessageW", "hwnd", $hWnd, "uint", 0x0E, "wparam", 0, "lparam", 0)
    If @error Then Return SetError(2, @error, '')
    If Not $aRet[0] Then Return SetError(3, 0, '')

    $nGetTextLen = $aRet[0]

;~     ConsoleWrite("WM_GETTEXTLENGTH return:"&$nGetTextLen&@CRLF)

    ; Create a union structure, add 2 characters - 1 for null-term, 1 to handle odd-count cases
    $stWideText = DllStructCreate("wchar["&$nGetTextLen + 2&"]")
    If @error Then Return SetError(4, 0, '')
    $stANSIText = DllStructCreate("char["&($nGetTextLen+2)*2&"]", DllStructGetPtr($stWideText))

    ; WM_GETTEXT
    $aRet = DllCall("user32.dll", "long", "SendMessageW", "hwnd", $hWnd, "uint", 0x0D, "wparam", $nGetTextLen + 1, "ptr", DllStructGetPtr($stWideText))
    If @error Then Return SetError(2, @error, '')
    If Not $aRet[0] Then Return SetError(3, 0, '')

    $nGetTextLen = $aRet[0]

    ; Get text as WIDE characters 1st
    $sText = DllStructGetData($stWideText, 1)

;~     ConsoleWrite("$nGetTextLen = "&$nGetTextLen&", $nHalfLen = "&$nHalfLen&", StringLen() = "&StringLen($sText)&@CRLF)

    ; Determine if the wide string length is half the supposed returned text length
    ; - If so, it's an ANSI string
    $nHalfLen = ($nGetTextLen + BitAND($nGetTextLen, 1) ) / 2
    If (StringLen($sText) - $nHalfLen < 2) Then
        ; Retrieve text correctly as ANSI
        $sText = DllStructGetData($stANSIText, 1)
    EndIf

    Return $sText
EndFunc
; ======================================================================================================
; Func _StringFixANSIInWide($sStr, $bForceCnvt = False)
;
; Function to fix a common issue where ANSI text is embedded in UTF-16 strings
; Problem occurs in 'WinGetText', 'ControlGetText', 'WinGetTitle'
; and some COM functions using 'bstr' types
;
; Easiest method, when you *know* the text is ANSI:
; BinaryToString(StringToBinary($sStr, 2), 1)
;
; *However*, if it is unknown what the string holds, we need to look
; for null characters (0's) in the string
;
; Alternatives:'WideCharToMultiByte' API call, which does the same replacements as below
; However, on Vista+, WC_ERR_INVALID_CHARS can be used to error-out on illegal characters
;
; Author: Ascend4nt
; ======================================================================================================

Func _StringFixANSIInWide($sStr, $bForceCnvt = False)
    If $sStr = '' Then Return ''
    Local $nLen, $stStrVer, $stBinVer, $sTmp, $nReplacements

    ; This fails to work in many mixed-ANSI/UTF-16 scenarios (as seen in WinGetText):
;~ If $bForceCnvt Then
;~ Return BinaryToString(StringToBinary($sStr, 2), 1)
;~ EndIf

    $nLen = StringLen($sStr)

    ; Struct for string (+1 for null-term)
    $stStrVer = DllStructCreate("wchar [" & $nLen + 1 & "]")
    ; Create a union, granting us binary 1-byte access to the wide chars
    $stBinVer = DllStructCreate("byte [" & $nLen * 2 & "]", DllStructGetPtr($stStrVer))

    ; Set String in structure
    DllStructSetData($stStrVer, 1, $sStr)

    ; Load string as binary data, convert to ANSI string
    ; AND Replace 0's with 0xFFFD (the Unicode 'REPLACEMENT CHARACTER')
    $sTmp = StringReplace(BinaryToString(DllStructGetData($stBinVer, 1)), ChrW(0), ChrW(0xFFFD), 0, 2)
    $nReplacements = @extended

    ; Trim off null-terminator and any other trailing 0's at the end (all converted to 0xFFFD's)
    While (StringRight($sTmp, 1) = ChrW(0xFFFD))
        $sTmp = StringTrimRight($sTmp, 1)
        $nReplacements -= 1
    WEnd

    ; If no replacements remaining, then every byte contains data, so its a safe bet the string is ANSI
    ; Also, in mixed-ANSI/UTF-16 situations (sometimes seen in WinGetText), allow a force
    If ($nReplacements = 0 Or $bForceCnvt) Then
        Return $sTmp
        ; Same result as:
        ;Return BinaryToString(StringToBinary($sStr, 2), 1)
    Else
        Return $sStr
    EndIf
EndFunc   ;==>_StringFixANSIInWide

*updates:

- Added a 'force' parameter for scenarios where WinGetText() will return a mix of ANSI and Unicode text in the same string. The result will contain some '?'s in these scenarios, but there's really nothing you can do without modifying the AutoIt source code.

update: problem no longer exists as of v3.3.9.11 (see BugTracker ticket # 2362)

Edited by Ascend4nt

Share this post


Link to post
Share on other sites

One of those things I've always noticed and wondered about, but never enough to really ask why :)! Great function, definitely a keeper for the snippet box, much appreciated m8 :)... Despite everything else, this imho might still be worth a bug ticket.

Edited by KaFu

Share this post


Link to post
Share on other sites

Actually, as it turns out, I was a bit wrong in assuming the '?'s were invalid characters. I was able to see what the output really looked like by changing the encoding in Notepad2 and doing a paste..

So basically, a lot of the 2-byte ANSI characters are perfectly valid UTF-16 combinations, and they generally show up as Eastern language characters in a Unicode viewer. The '?'s are what results when converting from those eastern language codes to an ANSI version, which is what you see in most text editors in their default locale settings (and in SciTE's output).

Soo now I'm thinking MultiByteToWideChar may be a better option.. and just 'fool' AutoIt into thinking it is accepting a "wstr" as the 3rd parameter in the DLLCall. The issue I'm a bit uncertain about is deciding what to put as the Locale info.. I'd need to find out the encoding the given window uses - which in most cases would be the system (or user?) code page. Hmm.. I'll have to see if there's a Windows API function for detecting the encoding of a window. If anyone knows it, let me know (otherwise I'll be poking around the API for a bit)

Also, another problem is that MultiByteToWideChar will either drop illegal code points or fail completely (depending on the MB_ERR_INVALID_CHARS flag.. Oh, and of course any mixed ANSI/Wide strings would give some funky results for the Wide string portion.. which is why I'm leaning towards telling people just to avoid WinGetText on problem windows, since you're basically getting a contaminated string by doing that.

Hmm.. for now I suppose I'll leave the function intact, as it works relatively well for Controls, but there's still a minimal chance that embedded ANSI can produce a completely valid UTF-16 string..


Share this post


Link to post
Share on other sites

Maybe it's related to the code page and language defined in the resources?

FileGetVersion($sFile, "DefaultLangCodepage")

From Help-File for FileGetVersion():

"Another special stringname is "DefaultLangCodepage" can be used to retrieve the default language and codepage.

The language and codepage can be used if needed to differentiate the "stringname" i.e. "080904b0Comments" (see MSDN StringFileInfo in VerQueryValue function)."

Share this post


Link to post
Share on other sites

Thanks for the idea KaFu, but I don't know that that would work well with multilanguage apps or text editors that let you change the encoding..

However, I've identified the problem - its the call to SendMessage (with WM_GETTEXT). It returns either an ANSI or Wide string, and the length of the returned string. I've found its pretty easy once we know the string length - we can just compare the wide string length to this and determine if it should be read as ANSI if its approximately half that returned length.

I've updated the first post with the new _WinGetTextFixed() function.


Share this post


Link to post
Share on other sites

Hopefully this is all fixed in 3.3.9.11.

I've fixed my window handling class so that all calls for WM_GETTEXT use it (there were a few places I'd missed).  I've checked WinGetText() and ControlGetText() with it and Scite.

Same fixes for Au3Info too.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By nacerbaaziz
      hello evrybody
      here is an example about how to split your texts using a delimiter with the ability to select how much of delimiters shows in each colum  with $i_number
      e.g
      you have a long text and you want to split it in an array
      that evry colum have a number (n) of lines
      i made a function that do that for you
      just call it with a three params
      $s_text
      your text
      $i_number
      the number that you want to put in each col
      $s_siparator
      the siparator
      default is "|"
      here is the function with example
      i hope that it will be useful for you
       
      ****
       
      #include <Array.au3> $s_txt = "some text1some text2|some text3|some text4|some text5|some text6" $array = splitText($s_txt, 2) _ArrayDisplay($array) Func splitText($s_text, $i_number, $s_siparator = "|") Local $a_TXT = StringSplit($s_text, $s_siparator) Local $a_Return[$a_TXT[0] + 1] If ($a_TXT[0] <= $i_number) Or ($i_number <= 0) Then ReDim $a_Return[2] $a_Return[0] = 1 $a_Return[1] = $s_text Return $a_Return EndIf Local $i_Processed = 1, $i_arrayProcessed = 1 Do For $i = $i_Processed To ($i_Processed + $i_number) - 1 If ($a_TXT[0] < $i) Then ExitLoop If Not ($a_Return[$i_arrayProcessed]) Then $a_Return[$i_arrayProcessed] = $a_TXT[$i] Else $a_Return[$i_arrayProcessed] &= $s_siparator & $a_TXT[$i] EndIf $i_Processed += 1 Next $i_arrayProcessed += 1 Until ($a_TXT[0] < $i_Processed) ReDim $a_Return[$i_arrayProcessed] $a_Return[0] = $i_arrayProcessed - 1 Return $a_Return EndFunc ;==>splitText
      accept my greetings
      thanks to
      @Dan_555
      for his notes
       
    • By cruisepandey
      Hi, 
      I have a string like this : 
      Global $Msga = "urrent directory is /send.  (Submission of file with log number 29381077284 is confirmed)";
      I want to extract the number 29381077284  from the string. I did StringSplit to split based on "(" and then use space to reach there, But it's not a good choice. 
      Can anyone help me with regular expression to find the number from String using AutoIT. TIA
    • By DannyJ
      I have this example string:
       
      2020-06-08 09:23:33 : abcdefghifjklm How to trim just the right part? Or how to trim this text right to left?
      I want to trim this string right to left until ' : '.
      In ohter words: I need the 'abcdefghifklm' text.
      I have tried StringTrimRight, but that requires 
      The number of characters to trim. Unfortunately StringSplit trims the left part of the strings.
       
       
    • By careca
      This is my take on string triggers, triggers on specific strings.
      Able to simple text pasting,
      opening links (as long as there's a www. http:\\ or https:\\ at the beggining)
      and is able to open applications, if there is a parameter in the parameter field, it uses it.
      Shows your external, lan, and gateway ip's.
      Able to change system volume by a set percentage, reading from the inputbox the number the user sets, if 0 or empty uses system default.
      I made this because the existing string trigger applications didn't do it for me.
      I did this for me, but if someone finds it useful, all the better.
    • By Colduction
      Hi guys!, i have a problem to convert Python code to AutoIt code, in fact i had not coded with Python yet!, this code is about permutation a string's case, i will be happy with your comments :)❤;

      Python code:
       
      # Python code to print all permutations # with respect to cases # Function to generate permutations def permute(inp): n = len(inp) # Number of permutations is 2^n mx = 1 << n # Converting string to lower case inp = inp.lower() # Using all subsequences and permuting them for i in range(mx): # If j-th bit is set, we convert it to upper case combination = [k for k in inp] for j in range(n): if (((i >> j) & 1) == 1): combination[j] = inp[j].upper() temp = "" # Printing current combination for i in combination: temp += i print(temp), # Driver code permute("Hello") # This code is contributed by Sachin Bisht
      My code in AutoIt:
      ; https://www.geeksforgeeks.org/permute-string-changing-case/ _PermuteCase("ABC") Func _PermuteCase($sText) If StringRegExp($sText, "^[A-Za-z]{1,}$") Then Local $iLength = StringLen($sText) ; Get length of the text. Local $iMaxPerm = 2 ^ $iLength ; Number of permutations is 2^n Local $sLow_Text = StringLower($sText) ; Converting string to lower case Local $asChrs = StringToASCIIArray($sLow_Text) ; Split the text into array of chars. For $i = 1 To $iMaxPerm Step 1 For $j = 0 To $asChrs[0] ;................................................... Next Next Else Return SetError(-1, 0, "Error: Input is incorrect!") EndIf EndFunc ;==>_PermuteCase  
       
       
       
       
       
      ====================== SOLUTION by @TheXman ======================
       
×
×
  • Create New...