Jump to content

Recommended Posts

Posted (edited)

below test code GetMessage int()/GetMessage bool()/GetMessage handle() all working.
but when using GetMessage str()/GetMessage wstr() it's not working why?
is there any way to make it work.

Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc")

__Example1()
Func __Example1()
    Local $t_TestObj
    Local $o_TestObj = _ObjectFromTag("__MyInterface_", "GetMessage str()", $t_TestObj)

    MsgBox(0, 'GetMessage_Test', $o_TestObj.GetMessage())
    If IsObj($o_TestObj) Then $o_TestObj = 0
EndFunc

Func __MyInterface_QueryInterface($pSelf, $pRIID, $pObj)
    Local $tStruct = DllStructCreate("ptr", $pObj)
    DllStructSetData($tStruct, 1, $pSelf)
    Return 0 ; $S_OK
EndFunc

Func __MyInterface_AddRef($pSelf)
    Return 1
EndFunc

Func __MyInterface_Release($pSelf)
    Return 1
EndFunc

Func __MyInterface_GetMessage($pSelf)
    Return "Is this Working?"
EndFunc

; #FUNCTION# =============================================================================
; Name...........: _ObjectFromTag
; ========================================================================================
Func _ObjectFromTag($sFunctionPrefix, $tagInterface, ByRef $tInterface, $bIsUnknown = Default, $sIID = "{00000000-0000-0000-C000-000000000046}", $fPrint = False) ; last param is IID_IUnknown by default
    If $bIsUnknown = Default Then $bIsUnknown = True
    Local $sInterface = $tagInterface ; copy interface description
    Local $tagIUnknown = "QueryInterface hresult(ptr;ptr*);" & _
            "AddRef dword();" & _
            "Release dword();"
    ; Adding IUnknown methods
    If $bIsUnknown Then $tagInterface = $tagIUnknown & $tagInterface
    ; Below line is really simple even though it looks super complex. It's just written weird to fit in one line, not to steal your attention
    Local $aMethods = StringSplit(StringReplace(StringReplace(StringReplace(StringReplace(StringTrimRight(StringReplace(StringRegExpReplace(StringRegExpReplace($tagInterface, "\w+\*", "ptr"), "\h*(\w+)\h*(\w+\*?)\h*(\((.*?)\))\h*(;|;*\z)", "$1\|$2;$4" & @LF), ";" & @LF, @LF), 1), "object", "idispatch"), "hresult", "long"), "bstr", "ptr"), "variant", "ptr"), @LF, 3)
    Local $iUbound = UBound($aMethods)
    Local $sMethod, $aSplit, $sNamePart, $aTagPart, $sTagPart, $sRet, $sParams, $hCallback
    ; Allocation
    $tInterface = DllStructCreate("int RefCount;int Size;ptr Object;ptr Methods[" & $iUbound & "];int_ptr Callbacks[" & $iUbound & "];ulong_ptr Slots[16]") ; 16 pointer sized elements more to create space for possible private props
    If @error Then Return SetError(1, 0, 0)
    For $i = 0 To $iUbound - 1
        $aSplit = StringSplit($aMethods[$i], "|", 2)
        If UBound($aSplit) <> 2 Then ReDim $aSplit[2]
        $sNamePart = $aSplit[0]
        $sTagPart = $aSplit[1]
        $sMethod = $sFunctionPrefix & $sNamePart
        If $fPrint Then
            Local $iPar = StringInStr($sTagPart, ";", 2), $t
            If $iPar Then
                $t = "Ret: " & StringLeft($sTagPart, $iPar - 1) & "  " & _
                        "Par: " & StringRight($sTagPart, StringLen($sTagPart) - $iPar)
            Else
                $t = "Ret: " & $sTagPart
            EndIf
            Local $s = "Func " & $sMethod & _
                    "( $pSelf ) ; " & $t & @CRLF & _
                    "EndFunc" & @CRLF
            ConsoleWrite($s)
        EndIf
        $aTagPart = StringSplit($sTagPart, ";", 2)
        $sRet = $aTagPart[0]
        $sParams = StringReplace($sTagPart, $sRet, "", 1)
        $sParams = "ptr" & $sParams
        $hCallback = DllCallbackRegister($sMethod, $sRet, $sParams)
        If @error Then
            ConsoleWrite('! ' & @error & ' ' & $sMethod & @CRLF & @CRLF)
        EndIf

        DllStructSetData($tInterface, "Methods", DllCallbackGetPtr($hCallback), $i + 1) ; save callback pointer
        DllStructSetData($tInterface, "Callbacks", $hCallback, $i + 1) ; save callback handle
    Next
    DllStructSetData($tInterface, "RefCount", 1) ; initial ref count is 1
    DllStructSetData($tInterface, "Size", $iUbound) ; number of interface methods
    DllStructSetData($tInterface, "Object", DllStructGetPtr($tInterface, "Methods")) ; Interface method pointers
    Return ObjCreateInterface(DllStructGetPtr($tInterface, "Object"), $sIID, $sInterface, $bIsUnknown) ; pointer that's wrapped into object
EndFunc   ;==>ObjectFromTag

Func _ErrFunc()
    ConsoleWrite("! COM Error !  Number: 0x" & Hex($oError.number, 8) & "   ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF)
    Return
EndFunc   ;==>_ErrFunc

 

Edited by jugador
  • Solution
Posted

Hi juador,

You'll need to return a ptr to a string buffer to get that to work.

Func __MyInterface_GetMessage($pSelf)
    Local $sString = "Is this Working?"
    Local Static $tString = DllStructCreate(StringFormat("char str[%d]", StringLen($sString) + 1)) ;+1 to null terminate!
    $tString.str = $sString
    Return DllStructGetPtr($tString)
EndFunc

Just careful of scoping/where you're storing the buffer.  It still needs to exist on exiting the method!

Posted

@MattyD

I can do all of this and all worked but the problem is why simple GetMessage str() not work is this Autoit limitation? @jpm

Func __MyInterface_GetMessage($pSelf)
    Local $sString = "Is this Working?"
    Local Static $tString = DllStructCreate(StringFormat("uint; char str[%d]", StringLen($sString) + 1)) ;+1 to null terminate!
    DllStructSetData($tString, 1, StringLen($sString))
    DllStructSetData($tString, 2, $sString)
    Return DllStructGetPtr($tString)
EndFunc

or this

Func __MyInterface_GetMessage($pSelf)
    Local $sMessage = "Is this Working?"
    Local $pBStr = SysAllocString($sMessage)
    Return $pBStr
EndFunc

 

Posted (edited)

Nope, its not an autoit thing - its just the way strings are generally passed under the hood...

For Eg: This would actually send a pointer to a wchar array, hence why the doco says use "wstr" for LPWSTR/LPCWSTR datatypes. (Long Pointer to a Wide String, Long Pointer to a Constant Wide String).

DllCall("blah.dll", "int", "MyFunc", "wstr", "MyParameter")

Your scenario is the same, but in this case $o_TestObj.GetMessage() is outputting a string.  So "GetMessage str()" means we expect to see a LPSTR (or equivalent) return type, hence __MyInterface_GetMessage() should return a pointer to a char array...

PS.  just be careful here - this is not quite right for a bstr... sysallocstring makes a bstr though - so if you need one, I'd suggest just use that.

1 hour ago, jugador said:
Func __MyInterface_GetMessage($pSelf)
    Local $sString = "Is this Working?"
    Local Static $tString = DllStructCreate(StringFormat("uint; char str[%d]", StringLen($sString) + 1)) ;+1 to null terminate!
    DllStructSetData($tString, 1, StringLen($sString))
    DllStructSetData($tString, 2, $sString)
    Return DllStructGetPtr($tString)
EndFunc
Edited by MattyD

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
  • Recently Browsing   1 member

×
×
  • Create New...