VAN0 Posted October 9 Share Posted October 9 Hello. Is there a way see what data is an object? What I mean by that is if have an object and we know a specific "key", we can access value via dot-annotation: $myObject.myKey But what if we don't know the key? For ... In seems the way to go but when I tried $myObject[$myKey] it would fail with: Quote Subscript used on non-accessible variable Basically, I want to show structure of an object as json: { myKey: myValue } Here is my code so far, it shows correct number of objects, but it doesn't show data in them: Local $oWMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2") Local $sQuery = "SELECT CommandLine, ProcessId FROM Win32_Process" Local $oProcesses = $oWMI.ExecQuery($sQuery, "WQL", 0x30) ConsoleWrite(object2string($oProcesses) & @CRLF) func object2string($obj) local $ret = "", $val, $sKey for $val in $obj $sKey = ObjName($val) If IsObj($val) Then $val = object2string($val) EndIf $ret &= ($ret ? ", " : "") & $sKey & ": " & $val next return "{" & $ret & "}" EndFunc In this example we know that the data in $val variable can be accessed as $val.CommandLine and $val.ProcessId but what if we don't know that, how can we make it dynamic? Link to comment Share on other sites More sharing options...
ioa747 Posted October 9 Share Posted October 9 (edited) expandcollapse popup; https://www.autoitscript.com/forum/topic/212366-get-data-from-an-object-without-knowing-its-structure/?do=findComment&comment=1537603 #include <String.au3> #include <Array.au3> $aArray = QueryWin32("SELECT ProcessId, CommandLine FROM Win32_Process") _ArrayDisplay($aArray) $aArray = QueryWin32("SELECT ProcessId, CommandLine, Caption, ThreadCount, ExecutablePath FROM Win32_Process") _ArrayDisplay($aArray) $aArray = QueryWin32('SELECT ProcessId, CommandLine, Caption, ThreadCount, ExecutablePath FROM Win32_Process WHERE Caption = "msedge.exe"') _ArrayDisplay($aArray) ;-------------------------------------------------------------------------------------------------------------------------------- Func QueryWin32($sQueryString) Local $oWMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2") ; $sQuery = "SELECT CommandLine, ProcessId FROM Win32_Process" Local $oProcesses = $oWMI.ExecQuery($sQueryString, "WQL", 0x30) Local $aPart = StringSplit(_StringBetween($sQueryString, "SELECT ", " FROM Win32_")[0], ", ", 1) Local $aResult[1][$aPart[0]] Local $idx = 0 ; make header of the collection For $i = 1 To $aPart[0] $aResult[0][$i - 1] = $aPart[$i] ;ConsoleWrite($i & ") " & $aPart[$i] & @CRLF) Next ; make the value of the collection For $oProcess In $oProcesses $idx += 1 ReDim $aResult[$idx + 1][$aPart[0]] For $i = 1 To $aPart[0] Local $sExeString = Execute('$oProcess.' & $aPart[$i]) ;ConsoleWrite($aPart[$i] & "=" & $sExeString & @CRLF) $aResult[$idx][$i - 1] = $sExeString Next Next Return $aResult EndFunc ;==>QueryWin32process #cs string CreationClassName; string Caption; string CommandLine; datetime CreationDate; string CSCreationClassName; string CSName; string Description; string ExecutablePath; uint16 ExecutionState; string Handle; uint32 HandleCount; datetime InstallDate; uint64 KernelModeTime; uint32 MaximumWorkingSetSize; uint32 MinimumWorkingSetSize; string Name; string OSCreationClassName; string OSName; uint64 OtherOperationCount; uint64 OtherTransferCount; uint32 PageFaults; uint32 PageFileUsage; uint32 ParentProcessId; uint32 PeakPageFileUsage; uint64 PeakVirtualSize; uint32 PeakWorkingSetSize; uint32 Priority; uint64 PrivatePageCount; uint32 ProcessId; uint32 QuotaNonPagedPoolUsage; uint32 QuotaPagedPoolUsage; uint32 QuotaPeakNonPagedPoolUsage; uint32 QuotaPeakPagedPoolUsage; uint64 ReadOperationCount; uint64 ReadTransferCount; uint32 SessionId; string Status; datetime TerminationDate; uint32 ThreadCount; uint64 UserModeTime; uint64 VirtualSize; string WindowsVersion; uint64 WorkingSetSize; uint64 WriteOperationCount; uint64 WriteTransferCount; #CE Edited October 10 by ioa747 Update VAN0 1 I know that I know nothing Link to comment Share on other sites More sharing options...
VAN0 Posted October 9 Author Share Posted October 9 Thank you, but this is "cheating", you are getting "keys" from query string. I'm trying to come up with a universal method to dissect already existing object with unknown structure. Link to comment Share on other sites More sharing options...
TheXman Posted October 9 Share Posted October 9 (edited) <snip> Edited October 14 by TheXman Semper volens adiuvare qui merentur CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
junkew Posted November 5 Share Posted November 5 Would eval function help? In which circumstance you wouldnt know the objectstructure or subfields. What you refer as "cheating" looks to me reuse what you know from the query. In general there is no reflection mechanism on objects. FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
VAN0 Posted November 5 Author Share Posted November 5 2 minutes ago, junkew said: Would eval function help? How would eval reveal what's inside an object? 2 minutes ago, junkew said: In which circumstance you wouldnt know the objectstructure or subfields. A "simple" debug function that would output in console whatever was provided to it. So, I could simply add debug($myvar) anywhere in the code and don't worry about providing any additional information about what that $myvar is. Link to comment Share on other sites More sharing options...
Nine Posted November 5 Share Posted November 5 If the object was created with WMI, you can use meta properties to get access to the content of the object (see properties_ and methods_ properties) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
junkew Posted November 6 Share Posted November 6 Not sure if typelibinspector still works for ole objects. And there was a debugger in the past that partially could do that but real reflection on all variables is not possible. FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now