Results 1 to 6 of 6

Thread: [RESOLVED] WM_GETTEXT losing Unicode despite normal retrieval method

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,547

    Resolved [RESOLVED] WM_GETTEXT losing Unicode despite normal retrieval method

    In the other thread, we were talking about retrieving Unicode characters with WM_GETTEXT. Now I have edit controls (API, not VB) where it's very mysteriously failing (switching ? with non-ASCII chars).
    I say very mysteriously because,
    a) The control is displaying them just fine
    b) If I create a control using the same code in another project, it works.
    c) It's the same gettext routine that even works in the same project for the listview labeledit text controls.

    So it's not about this method in general... I don't understand what could be happening to this control to get it to fail to return text in unicode. It and its parent are subclassed, but not for anything that involves text retrieval; and I subclassed the duplicate that works too. If it were because it's subclassed using the self-subclass code (it's in a usercontrol), one would think the also dynamically credit labeledit textboxes would have the same issue, but no.

    So, what could cause this, and what other methods of getting the text could I try, short of just tracking all WM_CHAR and WM_PASTE's.

    These are the current two methods:
    Code:
    Private Function GetEditWindowTextW(hwnd As Long) As String
    If hwnd Then
        Dim lLen As Long
        Dim sText As String
        Dim lRtn As Long
        Dim lErr As Long
        lLen = SendMessageW(hwnd, WM_GETTEXTLENGTH, 0, ByVal 0&)
        sText = String$(lLen, 0)
        lRtn = SendMessageW(hwnd, WM_GETTEXT, lLen + 1, ByVal StrPtr(sText))
        lErr = GetLastError()
        dbg_stringbytes sText 'prints the bytes like text = 0x50 0x60 0x70 etc
        DebugAppend "GetEditWindowTextW return=0x" & Hex$(lRtn) & ",err=0x" & Hex$(lErr) & ",text=" & sText
        GetEditWindowTextW = sText
    End If
    End Function
    'alternate:
    Private Function GetWindowText(ByVal hwnd As Long) As String
        GetWindowText = Space$(GetWindowTextLengthW(hwnd))
        GetWindowTextW hwnd, StrPtr(GetWindowText), Len(GetWindowText) + 1&
    End Function
    No errors. Text is already wrong on the immediate byte readout, default text thats loaded is correct, pasted text is correct, control display is correct... I'm stumped.

  2. #2
    Fanatic Member DrUnicode's Avatar
    Join Date
    Mar 2008
    Location
    Natal, Brazil
    Posts
    631

    Re: WM_GETTEXT losing Unicode despite normal retrieval method

    I had similar problem a while back with API ListView.
    Fixed by changing "Optional ByRef bUnicode As Boolean = False" to "Optional ByRef bUnicode As Boolean = True"

    <Code>Private Function ssc_Subclass(ByVal lng_hWnd As Long, _
    Optional ByVal lParamUser As Long = 0, _
    Optional ByVal nOrdinal As Long = 1, _
    Optional ByVal oCallback As Object = Nothing, _
    Optional ByVal bIdeSafety As Boolean = True, _
    Optional ByRef bUnicode As Boolean = True, _
    Optional ByVal bIsAPIwindow As Boolean = False) As Boolean</Code>

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,547

    Re: WM_GETTEXT losing Unicode despite normal retrieval method

    Yup that got it working, thanks! Was thrown by it displaying the chars and receiving pastes ok (and, the ListView didn't have that unicode flag set either); the test I was talking about used the normal subclassing method.

  4. #4
    Lively Member jj2007's Avatar
    Join Date
    Dec 2015
    Posts
    126

    Re: [RESOLVED] WM_GETTEXT losing Unicode despite normal retrieval method

    Whether you use SendMessage or SendMessageW, WM_GETTEXTLENGTH returns always chars, not bytes. In contrast, WM_GETTEXT retrieves an Ansi string, WM_GETTEXTW a Unicode string.

    Your buffer is chars+1 long, that might cause a little problem with WM_GETTEXTW... ;-)

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,547

    Re: [RESOLVED] WM_GETTEXT losing Unicode despite normal retrieval method

    WM_GETTEXTW? It's not defined in the SDK, and of the handful of places it appears on Google none have it defined either.

    Regular WM_GETTEXT with SendMessageW returns Unicode tho (and without the +1, a char is truncated).

  6. #6
    Lively Member jj2007's Avatar
    Join Date
    Dec 2015
    Posts
    126

    Re: [RESOLVED] WM_GETTEXT losing Unicode despite normal retrieval method

    You are perfectly right, it's SendMessageW, not WM_GETTEXTW. I even tested it and had the right code in front of me. Let's blame old age
    Last edited by jj2007; Apr 20th, 2018 at 10:47 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width