Modify

Opened 3 years ago

Closed 3 years ago

#3847 closed Bug (Fixed)

Type casting error in _WinAPI_MultiByteToWideChar

Reported by: anonymous Owned by: Jpm
Milestone: 3.3.15.5 Component: Standard UDFs
Version: 3.3.14.5 Severity: None
Keywords: Cc:

Description

This little example throws a fatal error.

#include <GUIConstants.au3>
#include <GuiEdit.au3>

crush()

Func crush()
    GUICreate('', 400, 400)
    Local $idEdit = GUICtrlCreateEdit(@ScriptFullPath, 0, 0, 400, 350, -1, 0)
    Local $idButton_ShowLen = GUICtrlCreateButton('Show Length', 10, 360, 380, 30)
    GUISetState()

    While True
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idButton_ShowLen
                _GUICtrlEdit_ShowBalloonTip($idEdit, 'Length', StringLen(GUICtrlRead($idEdit)), $TTI_INFO)
        EndSwitch
    WEnd
EndFunc   ;==>crush

The _GUICtrlEdit_ShowBalloonTip function does not cast its parameters to a string type, so _WinAPI_MultiByteToWideChar misunderstands the input and passes it as "struct*", which results in a fatal error.

Option to solve the error:

Func _WinAPI_MultiByteToWideChar($vText, $iCodePage = 0, $iFlags = 0, $bRetString = False)
;~      Local $sTextType = "str" ; remove
;~      If Not IsString($vText) Then $sTextType = "struct*" ; remove
        Local $sTextType = (IsDllStruct($vText) Or IsPtr($vText)) ? "struct*" : "str"

        ; compute size for the output WideChar
        Local $aResult = DllCall("kernel32.dll", "int", "MultiByteToWideChar", "uint", $iCodePage, "dword", $iFlags, _
                        $sTextType, $vText, "int", -1, "ptr", 0, "int", 0)
        If @error Or Not $aResult[0] Then Return SetError(@error + 10, @extended, 0)

        ; allocate space for output WideChar
        Local $iOut = $aResult[0]
        Local $tOut = DllStructCreate("wchar[" & $iOut & "]")

        $aResult = DllCall("kernel32.dll", "int", "MultiByteToWideChar", "uint", $iCodePage, "dword", $iFlags, $sTextType, $vText, _
                        "int", -1, "struct*", $tOut, "int", $iOut)
        If @error Or Not $aResult[0] Then Return SetError(@error + 20, @extended, 0)

        If $bRetString Then Return DllStructGetData($tOut, 1)
        Return $tOut
EndFunc   ;==>_WinAPI_MultiByteToWideChar

Attachments (0)

Change History (3)

comment:1 Changed 3 years ago by anonymous

By the way, why is _WinAPI_MultiByteToWideChar used in _GUICtrlEdit_ShowBalloonTip? If you just allocate a buffer, everything works.

#include <GUIConstants.au3>
#include <GuiEdit.au3>

crush()

Func crush()
        GUICreate('', 400, 400)
        Local $idEdit = GUICtrlCreateEdit(@ScriptFullPath, 0, 0, 400, 350, -1, 0)
        Local $idButton_ShowLen = GUICtrlCreateButton('Show Length', 10, 360, 380, 30)
        GUISetState()

        While True
                Switch GUIGetMsg()
                        Case $GUI_EVENT_CLOSE
                                ExitLoop
                        Case $idButton_ShowLen
                                _GUICtrlEdit_ShowBalloonTip_v2($idEdit, 'Length', StringLen(GUICtrlRead($idEdit)), $TTI_INFO)
                EndSwitch
        WEnd
EndFunc   ;==>crush

Func _GUICtrlEdit_ShowBalloonTip_v2($hWnd, $sTitle, $sText, $iIcon)
        Local $tBuffer = DllStructCreate('wchar Title[' & StringLen($sTitle) + 1 & '];wchar Text[' & StringLen($sText) + 1 & ']')
        DllStructSetData($tBuffer, 'Title', $sTitle)
        DllStructSetData($tBuffer, 'Text', $sText)
        Local $tTT = DllStructCreate($__tagEDITBALLOONTIP)
        DllStructSetData($tTT, 'Size', DllStructGetSize($tTT))
        DllStructSetData($tTT, 'Title', DllStructGetPtr($tBuffer, 'Title'))
        DllStructSetData($tTT, 'Text', DllStructGetPtr($tBuffer, 'Text'))
        DllStructSetData($tTT, 'Icon', $iIcon)
        Return (IsHWnd($hWnd) ? _SendMessage : GUICtrlSendMsg)($hWnd, $EM_SHOWBALLOONTIP, 0, DllStructGetPtr($tTT)) <> 0
EndFunc   ;==>_GUICtrlEdit_ShowBalloonTip_v2

comment:2 Changed 3 years ago by Jpm

Thanks for this V2 solution, I like it will be integrated

Last edited 3 years ago by Jpm (previous) (diff)

comment:3 Changed 3 years ago by Jpm

  • Milestone set to 3.3.15.5
  • Owner set to Jpm
  • Resolution set to Fixed
  • Status changed from new to closed

Fixed by revision [12588] in version: 3.3.15.5

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The owner will remain Jpm.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.