ComObjGet Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
foxhunter
Posts: 70
Joined: 04 Aug 2016, 04:27

ComObjGet

06 Sep 2016, 13:27

Hello,
I don't understand the documentation example of ComObjGet. There is the line if queryEnum[process] where I don't know where the process-Variable is going to be initialized. Rename processto another variablename (e.g. queryprocess) will fail. Could someone please explain?

Code: Select all

; Example: Press Shift+Escape to show the command line which was used
;   to launch the active window's process.  Requires XP or later.
+Esc::
    WinGet pid, PID, A
    ; Get WMI service object.
    wmi := ComObjGet("winmgmts:")
    ; Run query to retrieve matching process(es).
    queryEnum := wmi.ExecQuery(""
        . "Select * from Win32_Process where ProcessId=" . pid)
        ._NewEnum()
    ; Get first matching process.
    if queryEnum[process]     ; ---->  Question: where does process variable come from?
        MsgBox 0, Command line, % process.CommandLine
    else
        MsgBox Process not found!
    ; Free all global objects (not necessary when using local vars).
    wmi := queryEnum := process := ""
return
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: ComObjGet  Topic is solved

06 Sep 2016, 13:45

queryEnum contains an Enumerator object.
The code acts like a for-loop that only gets the first item in the object.
Here's a modified excerpt of the code with comments added by me.

Code: Select all

;...
    queryEnum := wmi.ExecQuery("Select * from Win32_Process where ProcessId=" . pid)._NewEnum()
    ; queryEnum now contains an enumerator object
    if queryEnum[process]   ; <-- "process" receives the next (ie first) key
        ;...
The following would have a similar effect (untested):

Code: Select all

for key, in wmi.ExecQuery("Select * from Win32_Process where ProcessId=" . pid)
{
    process := key
    break  ; Break after the first item
}

Return to “Ask For Help”

Who is online

Users browsing this forum: mikeyww, phrank, punchin, RubbeH and 41 guests