Jump to content

Recommended Posts

Posted (edited)

I haven't touched AutoIt in over a year, so I'm a bit rusty. 
Here is an attempt to incorporate the Get/Set property into @trancexx ObjectFromTag code, similar to the AutoItObject UDF.

Example 1:

#include "ObjFromTag_v1a UDF.au3"

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

__Example1()
Func __Example1()
    ConsoleWrite("> __Example4() " & @CRLF)
    Local $t_TestObj
    Local $o_TestObj = __ObjectFromTag("__MyInterface_", '', $t_TestObj, 'int Ln;char Msg[30];')

    __SetProperty($o_TestObj, "Ln", 555)
    __SetProperty($o_TestObj, "Msg", 'Is it working.')

    ConsoleWrite("+>>> Ln         :   " & __GetProperty($o_TestObj, "Ln") & @CRLF)
    ConsoleWrite("+>>> Msg        :   " & __GetProperty($o_TestObj, "Msg") & @CRLF)

    If IsObj($o_TestObj) Then $o_TestObj = 0
    __DeleteObjectFromTag($t_TestObj)
    ConsoleWrite("----The End----" & @CRLF)
EndFunc


Func __MyInterface_QueryInterface($pSelf, $pRIID, $pObj)
    Return __QueryInterface($pSelf, $pRIID, $pObj)
EndFunc

Func __MyInterface_AddRef($pSelf)
    Return __AddRef($pSelf)
EndFunc

Func __MyInterface_Release($pSelf)
    Return __Release($pSelf)
EndFunc

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

 

Example 2:

#include "ObjFromTag_v1a UDF.au3"

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

__Example2()
Func __Example2()
    ConsoleWrite("> __Example2() " & @CRLF)
    Local $t_TestObj
    Local $o_TestObj = __ObjectFromTag("__MyInterface_", 'Open hresult();Close hresult();', $t_TestObj, 'handle WinHnd;', False)
    If Not IsObj($o_TestObj) Then Return

    $o_TestObj.Open()
    MsgBox(0, '', 'Clicked to closed Notepad....')
    $o_TestObj.Close()

    If IsObj($o_TestObj) Then $o_TestObj = 0
    __DeleteObjectFromTag($t_TestObj)
EndFunc

Func __MyInterface_Open($pSelf)
    Run("notepad.exe")
    WinWait("[CLASS:Notepad]", "", 10)
    Local $hWnd = WinGetHandle("[CLASS:Notepad]")
    __SetProperty($pSelf, "WinHnd", $hWnd)
    Return 0
EndFunc

Func __MyInterface_Close($pSelf)
    WinClose(__GetProperty($pSelf, "WinHnd"))
    Return 0
EndFunc

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

 

Example 3:

#include "ObjFromTag_v1a UDF.au3"

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

__Example3()
Func __Example3()
    ConsoleWrite("> __Example3() " & @CRLF)
    Local $t_TestObj
    Local $o_TestObj = __ObjectFromTag("__MyInterface_", 'MsgBox hresult();', $t_TestObj, 'char Title[10];char Text[30];int Flag;', False)
    If Not IsObj($o_TestObj) Then Return

    __SetProperty($o_TestObj, "Title", "Something")
    __SetProperty($o_TestObj, "Text", 'Is it working.')
    __SetProperty($o_TestObj, "Flag", 64 + 262144)

    $o_TestObj.MsgBox()

    If IsObj($o_TestObj) Then $o_TestObj = 0
    __DeleteObjectFromTag($t_TestObj)
    ConsoleWrite("----The End----" & @CRLF)
EndFunc

Func __MyInterface_MsgBox($pSelf)
    MsgBox(__GetProperty($pSelf, "Flag"), __GetProperty($pSelf, "Title"), __GetProperty($pSelf, "Text"))
EndFunc

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

 

Example 4:

#include "ObjFromTag_v1a UDF.au3"

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

__Example3()
Func __Example3()
    ConsoleWrite("> __Example3() " & @CRLF)
    Local $t_TestObj
   Local $o_TestObj = __ObjectFromTag("", 'DisplayBook str();', $t_TestObj, 'char book[50];char author[15];int year;', False)
    If Not IsObj($o_TestObj) Then Return

    __SetProperty($o_TestObj, "book", "Harry Potter and the Sorcerer's Stone")
    __SetProperty($o_TestObj, "author", 'J. K. Rowling')
    __SetProperty($o_TestObj, "year", 1997)

    MsgBox(0, 'Book', $o_TestObj.DisplayBook)

    If IsObj($o_TestObj) Then $o_TestObj = 0
    __DeleteObjectFromTag($t_TestObj)
    ConsoleWrite("----The End----" & @CRLF)
EndFunc

Func DisplayBook($pSelf)
    Local $sString = 'The novel ' & __GetProperty($pSelf, "book") & ' ,' & _
                     ' written by author ' & __GetProperty($pSelf, "author") & _
                     ' and published in ' & __GetProperty($pSelf, "year") & '.'
    Local Static $tString = DllStructCreate(StringFormat("char str[%d]", StringLen($sString) + 1)) ;+1 to null terminate!
    $tString.str = $sString
    Return DllStructGetPtr($tString)
EndFunc

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

 

Example 5:

#include "ObjFromTag_v1a UDF.au3"

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

__Example3()
Func __Example3()
    ConsoleWrite("> __Example3() " & @CRLF)
    Local $t_TestObj
   Local $o_TestObj = __ObjectFromTag("", 'Call_DisplayBook hresult();DisplayBook str();', $t_TestObj, 'char book[50];char author[15];int year;', False)
    If Not IsObj($o_TestObj) Then Return

    __SetProperty($o_TestObj, "book", "Harry Potter and the Sorcerer's Stone")
    __SetProperty($o_TestObj, "author", 'J. K. Rowling')
    __SetProperty($o_TestObj, "year", 1997)

    $o_TestObj.Call_DisplayBook

    If IsObj($o_TestObj) Then $o_TestObj = 0
    __DeleteObjectFromTag($t_TestObj)
    ConsoleWrite("----The End----" & @CRLF)
EndFunc

Func Call_DisplayBook($pSelf)
    Local $o_Tag = 'Call_DisplayBook hresult();DisplayBook str();'
    Local $o__TestObj = ObjCreateInterface($pSelf, '{00000000-0000-0000-C000-000000000046}', $o_Tag, False)
    MsgBox(0, "", $o__TestObj.DisplayBook)
    $o__TestObj = 0
    Return 0
EndFunc

Func DisplayBook($pSelf)
    Local $sString = 'The novel ' & __GetProperty($pSelf, "book") & ' ,' & _
                     ' written by author ' & __GetProperty($pSelf, "author") & _
                     ' and published in ' & __GetProperty($pSelf, "year") & '.'
    Local Static $tString = DllStructCreate(StringFormat("char str[%d]", StringLen($sString) + 1)) ;+1 to null terminate!
    $tString.str = $sString
    Return DllStructGetPtr($tString)
EndFunc

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

 

Example 6:

#include "ObjFromTag_v1a UDF.au3"

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

__Example3()
Func __Example3()
    ConsoleWrite("> __Example3() " & @CRLF)
    Local $o_Dict = ObjCreate( "Scripting.Dictionary" )
    $o_Dict.add("book", "Harry Potter and the Sorcerer's Stone")
    $o_Dict.add("author", "J. K. Rowling")
    $o_Dict.add("year", 1997)

    Local $t_TestObj
    Local $o_TestObj = __ObjectFromTag("", 'DisplayBook str();', $t_TestObj, 'ptr oDist;', False)
    If Not IsObj($o_TestObj) Then Return

    __SetProperty($o_TestObj, "oDist", $o_Dict)

    MsgBox(0, 'Book', $o_TestObj.DisplayBook)

    If IsObj($o_Dict) Then $o_Dict = 0
    If IsObj($o_TestObj) Then $o_TestObj = 0
    __DeleteObjectFromTag($t_TestObj)
    ConsoleWrite("----The End----" & @CRLF)
EndFunc

Func DisplayBook($pSelf)
    Local $oo_Dict = __ConvertPtrToIDispatch(__GetProperty($pSelf, "oDist"))
    Local $sString = 'The novel ' & $oo_Dict.Item("book") & ' ,' & _
                     ' written by author ' & $oo_Dict.Item("author") & _
                     ' and published in ' & $oo_Dict.Item("year") & '.'
    Local Static $tString = DllStructCreate(StringFormat("char str[%d]", StringLen($sString) + 1)) ;+1 to null terminate!
    $tString.str = $sString

    $oo_Dict = 0
    Return DllStructGetPtr($tString)
EndFunc

Func __ConvertPtrToIDispatch($pIDispatch)
    ; https://www.autoitscript.com/forum/topic/107678-hooking-into-the-idispatch-interface/#comments
    ; This would have been 10000x easier if autoit had supported the idispatch* type in dllstructs...
    ; Fortunetely memcpy can copy the pointer into a idispatch*, lucky us.
    Local $aCall = DllCall("kernel32.dll", "none", "RtlMoveMemory", _
            "idispatch*", 0, _
            "ptr*", $pIDispatch, _
            "dword", 4)
    If @error Then Return SetError(1, 0, 0)
    Return $aCall[1]
EndFunc ;==>ConvertPtrToIDispatch

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

 

Example 7:

#include "ObjFromTag_v1a UDF.au3"

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

__Example3()
Func __Example3()
    ConsoleWrite("> __Example3() " & @CRLF)
    Local $t_BookObj, $t_AuthorObj
    Local $o_BookObj = __ObjectFromTag("", 'BookList hresult(bstr;bstr);', $t_BookObj, 'char book[50];int year;', False)
    Local $o_AuthorObj = __ObjectFromTag("", 'DisplayBook str(bstr);', $t_AuthorObj, 'char author[15];ptr oBook;', False)

    __SetProperty($o_AuthorObj, "oBook", $o_BookObj())

    $o_BookObj.BookList("Harry Potter and the Sorcerer's Stone", 1997)
    MsgBox(0, 'Book', $o_AuthorObj.DisplayBook('J. K. Rowling'))

    If IsObj($o_AuthorObj) Then $o_AuthorObj = 0
    If IsObj($o_BookObj) Then $o_BookObj = 0
    __DeleteObjectFromTag($t_AuthorObj)
    __DeleteObjectFromTag($t_BookObj)
    ConsoleWrite("----The End----" & @CRLF)
EndFunc

Func BookList($pSelf, $pStrA, $pStrB)
    __SetProperty($pSelf, "book", __Bstr_ToString($pStrA))
    __SetProperty($pSelf, "year", __Bstr_ToString($pStrB))
EndFunc

Func DisplayBook($pSelf, $pStrA)
    __SetProperty($pSelf, "author", __Bstr_ToString($pStrA))
    Local $pBookList = __GetProperty($pSelf, "oBook")
    Local $sString = 'The novel ' & __GetProperty($pBookList, "book") & ' ,' & _
                     ' written by author ' & __GetProperty($pSelf, "author") & _
                     ' and published in ' & __GetProperty($pBookList, "year") & '.'
    Local Static $tString = DllStructCreate(StringFormat("char str[%d]", StringLen($sString) + 1)) ;+1 to null terminate!
    $tString.str = $sString
    Return DllStructGetPtr($tString)
EndFunc

Func __Bstr_ToString($pString)
    Local $o_Data = DllStructGetData(DllStructCreate("wchar[" & _
                    DllStructGetData(DllStructCreate("dword", $pString - 4), 1) / 2 & "]", $pString), 1)
    Return $o_Data
EndFunc

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

 

Example 8:

#include "ObjFromTag_v1a UDF.au3"

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

__Example3()
Func __Example3()
    ConsoleWrite("> __Example3() " & @CRLF)
    Local $t_TestObj
   Local $o_TestObj = __ObjectFromTag("", 'Call_DisplayBook hresult();DisplayBook str();', $t_TestObj, 'char book[50];char author[15];int year;', False)
    If Not IsObj($o_TestObj) Then Return

    __SetProperty($o_TestObj, "book", "Harry Potter and the Sorcerer's Stone")
    __SetProperty($o_TestObj, "author", 'J. K. Rowling')
    __SetProperty($o_TestObj, "year", 1997)

    $o_TestObj.Call_DisplayBook

    If IsObj($o_TestObj) Then $o_TestObj = 0
    __DeleteObjectFromTag($t_TestObj)
    ConsoleWrite("----The End----" & @CRLF)
EndFunc

Func Call_DisplayBook($pSelf)
    Local $pEntryPoint  = __DereferencePointer($pSelf)
    Local $pDisplayBook = __DereferencePointer($pEntryPoint + 1 * 4)
    Local $aRet = DllCallAddress("str", $pDisplayBook, "ptr", $pSelf)
    MsgBox(0, "", $aRet[0])
    Return 0
EndFunc

Func DisplayBook($pSelf)
    Local $sString = 'The novel ' & __GetProperty($pSelf, "book") & ' ,' & _
                     ' written by author ' & __GetProperty($pSelf, "author") & _
                     ' and published in ' & __GetProperty($pSelf, "year") & '.'
    Local Static $tString = DllStructCreate(StringFormat("char str[%d]", StringLen($sString) + 1)) ;+1 to null terminate!
    $tString.str = $sString
    Return DllStructGetPtr($tString)
EndFunc

Func __DereferencePointer($ptr)
    Local $tempstruct = DllStructCreate("ptr", $ptr)
    Return DllStructGetData($tempstruct, 1)
EndFunc

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

 

for more about objectfromtag you can follow this thread created by @LarsJ 
https://www.autoitscript.com/forum/topic/205154-using-objcreateinterface-and-objectfromtag-functions/ 

and do check out code posted by @trancexx  @ProgAndy  @monoceres  @wolf9228  @LarsJ  @Danyfirex and @Bilgus. on this topic.

 

Note:
You can also register this object, created using __ObjectFromTag, within the Running Object Table (ROT).

 

ObjFromTag_v1a UDF.au3

Edited by jugador

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   0 members

    • No registered users viewing this page.
×
×
  • Create New...