Jump to content

ObjGet failed why?


jugador
 Share

Recommended Posts

Vbscript

Set oDict = CreateObject("Scripting.FileSystemObject")
Set oGetobj = GetObject("", "Scripting.FileSystemObject")

WScript.Echo TypeName(oDict)
WScript.Echo TypeName(oGetobj)

Autoit

Local $oDict = ObjCreate("Scripting.FileSystemObject")
If IsObj($oDict) Then
    Local $oGetobj = ObjGet("", "Scripting.FileSystemObject")
    If @error Then
         MsgBox(0, "", "ObjGet failed.... Error code: " & Hex(@error, 8))
         $oDict = 0
         Exit
    Endif

$oGetobj = 0
$oDict = 0
MsgBox(0, "", "ObjGet Test Ok")
Endif

 

Link to comment
Share on other sites

A COM Error Handler may provide additional info :

; COM Error Handler :
Local $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc"), $EventError

Local $oDict, $oGetObj
$oDict = ObjCreate("Scripting.FileSystemObject")
ConsoleWrite("ObjCreate $oDict = " & @error & @CRLF)

If IsObj($oDict) Then
    $oGetObj = ObjGet("", "Scripting.FileSystemObject")
    If @error Then
        MsgBox(0, "", "ObjGet failed.... Error code: " & Hex(@error, 8))
        $oDict = 0
        Exit
    EndIf

    $oGetObj = 0
    $oDict   = 0
    MsgBox(0, "", "ObjGet Test Ok")
EndIf


Func MyErrFunc()
    ConsoleWrite(@LF & "===========================================================" & @LF & _
            "! AutoItCOM Test -- We intercepted a  !" & @LF & @TAB & _
            "err.description    : " & $oMyError.description & @LF & @TAB & _
            "err.windescription : " & $oMyError.windescription & @TAB & _
            "err.number         : " & Hex($oMyError.number, 8) & @LF & @TAB & _
            "err.lastdllerror   : " & $oMyError.lastdllerror & @LF & @TAB & _
            "err.scriptline     : " & $oMyError.scriptline & @LF & @TAB & _
            "err.source         : " & $oMyError.source & @LF & @TAB & _
            "err.helpfile       : " & $oMyError.helpfile & @LF & @TAB & _
            "err.helpcontext    : " & $oMyError.helpcontext & @LF & _
            "===========================================================" & @LF)

    Local $Err = $oMyError.number
    If $Err = 0 Then $Err = -1
    $EventError = $Err ; to check for after this function returns
EndFunc   ;==>MyErrFunc

 

Edited by Musashi
typo

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Probably because the VBScript function GetObject() and the AutoIt function ObjGet() don't work in exactly the same way.

Note that this VBScript code also works: 

Set oGetobj = GetObject("", "Scripting.FileSystemObject")

WScript.Echo TypeName(oGetobj)

GetObject() can thus create a new object if the parameters don't refer to an existing object.

The way you execute GetObject() in your VBScript code, the function creates a new object because the parameters don't match an existing object. In your code, the two objects are different objects but of the same type. 

The AutoIt ObjGet() function doesn't create new objects. It only creates a reference to an existing object through a file name or a line in the Running Object Table (ROT). These two options are both demonstrated in the examples in the help file.

ObjCreate("Scripting.FileSystemObject") in AutoIt doesn't create an object that can be referenced either through a file name or through the ROT. Therefore, ObjGet() fails.

CreateObject("Scripting.FileSystemObject") in VBScript also doesn't create an object that can be referenced either through a file name or through the ROT. But GetObject() doesn't fail because in this situation it creates a new object.

Link to comment
Share on other sites

@LarsJ  @Danyfirex   @Nine

Try to get running Scripting.Dictionary object using ObjGet but failed (like describe in 1st post).

So try with ObjCreateInterface instead of ObjGet to capture running Scripting.Dictionary and it worked.

1)  but getting error at the end  !> AutoIt3.exe ended.rc:-1073741819
2)  Is there any wrong in using ObjCreateInterface instead of ObjGet to capture running object?

#include <WinAPICom.au3>

__Example1()
Func __Example1()
Local $String_A = "Apple"

_WinAPI_CoInitialize( $COINIT_APARTMENTTHREADED )
Local $oDict_A = ObjCreate( "Scripting.Dictionary" )

If IsObj($oDict_A) Then
    $oDict_A.add("$String_A", $String_A)
    ConsoleWrite("----->" & @CRLF)
    ConsoleWrite("[$oDict_A] [$String_A] => " & $oDict_A.Item("$String_A") & @CRLF)

    Local $pObject = Ptr( $oDict_A )
    Local $oDict_B = ObjCreateInterface($pObject, "{42C642C1-97E1-11CF-978F-00A02463E06F}", Default, False)
    If @error Then
        ConsoleWrite("ObjCreateInterface failed.... Error code: " & Hex(@error, 8) & @CRLF)
        $oDict_A = 0
        Exit
    Endif

    Local $String_B = "Orange"
    $oDict_B.add("$String_B", $String_B)

    ConsoleWrite("----->" & @CRLF)
    ConsoleWrite("[$oDict_B] [$String_B] => " & $oDict_B.Item("$String_B") & @CRLF)
    ConsoleWrite("[$oDict_B] [$String_A] => " & $oDict_B.Item("$String_A") & @CRLF)
    ConsoleWrite("----->" & @CRLF)

    ConsoleWrite("[$oDict_A] [$String_B] => " & $oDict_A.Item("$String_B") & @CRLF)
    ConsoleWrite("----->" & @CRLF)
    _WinAPI_CoUninitialize()

    $oDict_B = 0
    $oDict_A = 0
Endif
EndFunc

 

Edited by jugador
Link to comment
Share on other sites

I assume that your expectation of the code line with the ObjCreateInterface() function is that it gives you a new reference to the $oDict_A object. Ie. that $oDict_A and $oDict_B are two different references to the same object. But that's not the case. $oDict_B is a new object apparently created in the same memory block as $oDict_A. That's why you get Memory Access Violation errors. You also assume that your code works. But it doesn't. This is because you have only tested the code that works. You haven't tested the code that doesn't work.

You create two references $oDict_A and $oDict_B to the same object this way. Because they are two references to the same object, this code works.

Example()

Func Example()
  Local $oDict_A = ObjCreate( "Scripting.Dictionary" )                   ; Object reference counter = 1
  $oDict_A( "Test" ) = 123
  ConsoleWrite( "$oDict_A( ""Test"" ) = " & $oDict_A( "Test" ) & @CRLF )
  $oDict_B = $oDict_A ; $oDict_A and $oDict_B references the same object ; Object reference counter = 2
  $oDict_A = 0                                                           ; Object reference counter = 1
  ConsoleWrite( "$oDict_B( ""Test"" ) = " & $oDict_B( "Test" ) & @CRLF )
EndFunc

If you test the same code (deletion of $oDict_A) with your $oDict_A and $oDict_B objects you'll see that the code doesn't work after deletion of $oDict_A.

#include <WinAPICom.au3>

__Example1()
Func __Example1()
  Local $String_A = "Apple"

  _WinAPI_CoInitialize( $COINIT_APARTMENTTHREADED )
  Local $oDict_A = ObjCreate( "Scripting.Dictionary" )

  If IsObj($oDict_A) Then
    $oDict_A.add("$String_A", $String_A)
    ConsoleWrite("----->" & @CRLF)
    ConsoleWrite("[$oDict_A] [$String_A] => " & $oDict_A.Item("$String_A") & @CRLF)

    Local $pObject = Ptr( $oDict_A )
    Local $oDict_B = ObjCreateInterface($pObject, "{42C642C1-97E1-11CF-978F-00A02463E06F}", Default, False)
    If @error Then
      ConsoleWrite("ObjCreateInterface failed.... Error code: " & Hex(@error, 8) & @CRLF)
      $oDict_A = 0
      Exit
    Endif
    $oDict_A = 0 ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    ConsoleWrite("----->" & @CRLF)
    ConsoleWrite("$oDict_A has been deleted" & @CRLF)

    Local $String_B = "Orange"
    $oDict_B.add("$String_B", $String_B)

    ConsoleWrite("----->" & @CRLF)
    ConsoleWrite("[$oDict_B] [$String_B] => " & $oDict_B.Item("$String_B") & @CRLF)
    ConsoleWrite("[$oDict_B] [$String_A] => " & $oDict_B.Item("$String_A") & @CRLF)
    ConsoleWrite("----->" & @CRLF)

    _WinAPI_CoUninitialize()

    $oDict_B = 0
  Endif
EndFunc

The fact that $oDict_B doesn't work when $oDict_A is deleted clearly indicates that $oDict_A and $oDict_B aren't references to the same object. If they were, the code would have worked. $oDict_A and $oDict_B are two different objects.

That parts of the code works as in your example along with all the Memory Access Violation errors indicates that the two objects have overlapping memory blocks.

If you just want to create a new reference to an existing object simply use $oDict_B = $oDict_A.

Link to comment
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
 Share

  • Recently Browsing   0 members

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