Sign in to follow this  
Followers 0
Tripredacus

var has data but not Object?

5 posts in this topic

I may be misunderstanding the IsObj() function... Clear this up for me? Here is example code below...

This works:

#include <file.au3>
Global $itdid, $objWMIService, $colItems, $sWMIService, $sName, $sModel, $uuItem, $objSWbemObject, $strName, $strVersion, $strWMIQuery, $objItem, $uiDitem, $strDesc
$sWMIService = "winmgmts:\\" & @ComputerName & "\root\CIMV2"
$objWMIService = ObjGet($sWMIService)
IF IsObj($objWMIService) Then
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
    If IsObj($colItems) Then
        For $oItem In $colItems
            $sName = $oItem.Manufacturer
            $sModel = $oItem.Model
        Next
    EndIf
        $itdid = Chr(34) & $sModel & Chr(34)
    MsgBox(64, "Win32_ComputerSystem Item", $itdid & @CRLF & "Thanks")
EndIf

However, if I check to see if $sModel is an object, the MsgBox will not appear:

If IsObj($sModel) Then
        $itdid = Chr(34) & $sModel & Chr(34)
    MsgBox(64, "Win32_ComputerSystem Item", $itdid & @CRLF & "Thanks")
    EndIf

So $sModel has data in it as it is declared as Global, but IsObj doesn't see anything in there. What specifically defines an "object" is it the presence of data, or does it have to be an array?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Are you asking how does IsObj know whether something is an object as compared to a plain variable or an array? There must be a flag that objects have that IsObj checks. That's my best guess.

Or a pointer that objects have that point to an interface of methods. If that pointer is null then it isn't an object. I'm sure this is all wrong but maybe someone else with more knowledge can expand on this?

Edited by LaCastiglione

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Deep in the little C++ heart of the AutoIt interpreter, every AutoIt variable is "Variant" type with flags included in the definition to tell the interpreter how to interpret them. VarGetType() just tells you what those flags say about the usage of that variant. In the OP script, $sWMIService is an AutoIt string, meaning inside the C++ world we can't see it's a variant with a flag that says treat it as a string. $objWMIService is the same thing, and right after it was declared the hidden type flags said it was a string (because that's the default), but after assigning a value to it with ObjGet() the flags were changed to indicated it was a COM object. Try this:

Global $itdid, $objWMIService, $colItems, $sWMIService, $sName, $sModel, $oItem

$sWMIService = "winmgmts:\\" & @ComputerName & "\root\CIMV2"
ConsoleWrite("$sWMIService = " & $sWMIService & ";  Type = " & VarGetType($sWMIService) & @LF)

ConsoleWrite("Before:  $objWMIService = " & $objWMIService & ";  Type = " & VarGetType($objWMIService) & @LF)
$objWMIService = ObjGet($sWMIService)
ConsoleWrite("After:  $objWMIService = " & $objWMIService & ";  Type = " & VarGetType($objWMIService) & @LF)

If IsObj($objWMIService) Then
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
    ConsoleWrite("$colItems = " & $colItems & ";  Type = " & VarGetType($colItems) & @LF)

    If IsObj($colItems) Then
        For $oItem In $colItems
            $sName = $oItem.Manufacturer
            ConsoleWrite("$sName = " & $sName & ";  Type = " & VarGetType($sName) & @LF)

            $sModel = $oItem.Model
            ConsoleWrite("$sModel = " & $sModel & ";  Type = " & VarGetType($sModel) & @LF)
        Next
    EndIf
    $itdid = Chr(34) & $sModel & Chr(34)
    MsgBox(64, "Win32_ComputerSystem Item", $itdid & @CRLF & "Thanks")
EndIf

Note the object doesn't display any content to the ConsoleWrite() because there is no way to convert and object to a string on the fly for that input.

:huh2:

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Just to add that (when speaking of hearts) an object is just a pointer to a pointer that points to a set of pointers pointing to a real code. Literally.

Simple.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks for the

:huh2:

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
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
Sign in to follow this  
Followers 0