Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Larkal
Posts: 21
Joined: 08 Mar 2019, 00:36

Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release

Post by Larkal » 04 Jul 2020, 18:54

I'm trying to get Lexikos's ShellRun running on AutoHotkey 2, but I'm running into problems.
I'm not good with DllCalls and COM objects, so I'm at a bit of a loss.

I'm running into these errors and I don't know what to do:

Code: Select all

Error:  No valid COM object!
--->	shellObj := ComObject(9, pdisp, 1).Application

Error:  This value of type "String" has no method named "ShellExecute".
--->	shellObj.ShellExecute(prms*)
Original code that worked in older versions of AutoHotkey 2:

Code: Select all

/*
  ShellRun by Lexikos
    requires: AutoHotkey_L
    license: http creativecommons.org /publicdomain/zero/1.0/  Broken Link for safety
  Credit for explaining this method goes to BrandonLive:
  http brandonlive.com /2008/04/27/getting-the-shell-to-run-an-application-for-you-part-2-how/  Broken Link for safety
  Shell.ShellExecute(File [, Arguments, Directory, Operation, Show])
  http msdn.microsoft.com /en-us/library/windows/desktop/gg537745  Broken Link for safety
  https://docs.microsoft.com/en-us/windows/desktop/shell/shell-shellexecute
  ShellExecute takes up to 5 parameters.
  ShellExecute(sFile, [ vArguments ], [ vDirectory ], [ vOperation ], [ vShow ])
*/
ShellRun(prms*) {
    shellWindows := ComObjCreate("{9BA05972-F6A8-11CF-A442-00A0C90A8F39}")
    
    desktop := shellWindows.Item(ComObject(19, 8)) ; VT_UI4, SCW_DESKTOP                
   
    ; Retrieve top-level browser object.
    if ptlb := ComObjQuery(desktop
        , "{4C96BE40-915C-11CF-99D3-00AA004AE837}"  ; SID_STopLevelBrowser
        , "{000214E2-0000-0000-C000-000000000046}") ; IID_IShellBrowser
    {
        ; IShellBrowser.QueryActiveShellView -> IShellView
        if DllCall(NumGet(NumGet(ptlb+0)+15*A_PtrSize), "ptr", ptlb, "ptr*", psv:=0) = 0
        {
            ; Define IID_IDispatch.
            VarSetCapacity(IID_IDispatch, 16)
            NumPut(0x46000000000000C0, NumPut(0x20400, IID_IDispatch, "int64"), "int64")
           
            ; IShellView.GetItemObject -> IDispatch (object which implements IShellFolderViewDual)
            DllCall(NumGet(NumGet(psv+0)+15*A_PtrSize), "ptr", psv
                , "uint", 0, "ptr", &IID_IDispatch, "ptr*", pdisp:=0)
           
            ; Get Shell object.
            shellObj := ComObject(9,pdisp,1).Application
           
            ; IShellDispatch2.ShellExecute
            shellObj.ShellExecute(prms*)
           
            ObjRelease(psv)
        }
        ObjRelease(ptlb)
    }
}
My edit that's generating the errors listed above:
(virtually nothing has changed - I only changed VarSetCapacity into BufferAlloc)

Code: Select all

/*
  ShellRun by Lexikos
    requires: AutoHotkey_L
    license: http creativecommons.org /publicdomain/zero/1.0/  Broken Link for safety
  Credit for explaining this method goes to BrandonLive:
  http brandonlive.com /2008/04/27/getting-the-shell-to-run-an-application-for-you-part-2-how/  Broken Link for safety
  Shell.ShellExecute(File [, Arguments, Directory, Operation, Show])
  http msdn.microsoft.com /en-us/library/windows/desktop/gg537745  Broken Link for safety
  https://docs.microsoft.com/en-us/windows/desktop/shell/shell-shellexecute
  ShellExecute takes up to 5 parameters.
  ShellExecute(sFile, [ vArguments ], [ vDirectory ], [ vOperation ], [ vShow ])
*/
ShellRun(prms*) {
    shellWindows := ComObjCreate("{9BA05972-F6A8-11CF-A442-00A0C90A8F39}")
    desktop := shellWindows.Item(ComObject(19, 8)) ; VT_UI4, SCW_DESKTOP                
   
    ; Retrieve top-level browser object.
    if ptlb := ComObjQuery(desktop
        , "{4C96BE40-915C-11CF-99D3-00AA004AE837}"  ; SID_STopLevelBrowser
        , "{000214E2-0000-0000-C000-000000000046}") ; IID_IShellBrowser
    {
        ; IShellBrowser.QueryActiveShellView -> IShellView
        if (DllCall(NumGet(NumGet(ptlb+0)+15*A_PtrSize), "ptr", ptlb, "ptr*", psv:=0) = 0)
        {
            ; Define IID_IDispatch.
            IID_IDispatch := BufferAlloc(16)
            NumPut(0x46000000000000C0, NumPut(0x20400, IID_IDispatch, "int64"), "int64")
           
            ; IShellView.GetItemObject -> IDispatch (object which implements IShellFolderViewDual)
            DllCall(NumGet(NumGet(psv+0)+15*A_PtrSize), "ptr", psv
                , "uint", 0, "ptr", &IID_IDispatch, "ptr*", pdisp:=0)
           
            ; Get Shell object.
            shellObj := ComObject(9, pdisp, 1).Application
           
            ; IShellDispatch2.ShellExecute
            shellObj.ShellExecute(prms*)
           
            ObjRelease(psv)
        }
        ObjRelease(ptlb)
    }
}

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release

Post by swagfag » 04 Jul 2020, 19:43

Code: Select all

ShellRun(prms*)
{
    shellWindows := ComObjCreate("Shell.Application").Windows
    _hwnd := BufferAlloc(4)
    desktop := shellWindows.FindWindowSW(0, "", 8, ComObject(0x4003, _hwnd.Ptr), 1)
   
    ; Retrieve top-level browser object.
    if ptlb := ComObjQuery(desktop,
        "{4C96BE40-915C-11CF-99D3-00AA004AE837}", ; SID_STopLevelBrowser
        "{000214E2-0000-0000-C000-000000000046}") ; IID_IShellBrowser
    {
        ; IShellBrowser.QueryActiveShellView -> IShellView
        if ComCall(15, ptlb, "ptr*", psv:=0) = 0
        {
            ; Define IID_IDispatch.
            IID_IDispatch := BufferAlloc(16)
            NumPut("int64", 0x20400, "int64", 0x46000000000000C0, IID_IDispatch)
           
            ; IShellView.GetItemObject -> IDispatch (object which implements IShellFolderViewDual)
            ComCall(15, psv, "uint", 0, "ptr", IID_IDispatch, "ptr*", pdisp:=0)
           
            ; Get Shell object.
            shell := ComObject(9,pdisp,1).Application
           
            ; IShellDispatch2.ShellExecute
            shell.ShellExecute(prms*)
           
            ObjRelease(psv)
        }
    }
}

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release

Post by Helgef » 05 Jul 2020, 04:24

@swagfag, seems you missed ObjRelease(ptlb).
@Larkal, you might want to update to the latest version. The updated error dialog will discourage you from continuing the thread.

Cheers.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release

Post by swagfag » 05 Jul 2020, 06:05

@Helgef why? ComObjQuery gives me a wrapped ptr back, so it should release it when it goes out of scope

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release

Post by Helgef » 05 Jul 2020, 06:13

sorry I just saw lexikos did it and guessed you missed it, looking at the docs I see you are correct.

Cheers.

Larkal
Posts: 21
Joined: 08 Mar 2019, 00:36

Re: Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release

Post by Larkal » 05 Jul 2020, 17:37

@swagfag Thank you very much!!

Larkal
Posts: 21
Joined: 08 Mar 2019, 00:36

Re: Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release

Post by Larkal » 24 Apr 2021, 02:09

Just updated to v2.0-a132-8673ba2f and it's broken again.

@swagfag's code was previously working. I've tried to update it but this is a bit beyond me. I was reading the documentation for the ComObj changes, but I'm still mystified.

Here's swagfag's code with the changes I tried making that don't work.

Code: Select all

ShellRun(prms*) {
    shellWindows := ComObject("Shell.Application").Windows
    _hwnd := BufferAlloc(4)
    MsgBox _hwnd.Ptr
    desktop := shellWindows.FindWindowSW(0, "", 8, ComValue(0x4003, _hwnd.Ptr), 1)

    ; Retrieve top-level browser object.   
	ptlb := ComObjQuery(desktop,
        "{4C96BE40-915C-11CF-99D3-00AA004AE837}", ; SID_STopLevelBrowser
        "{000214E2-0000-0000-C000-000000000046}") ; IID_IShellBrowser
	
    if (ptlb)
    {
        ; IShellBrowser.QueryActiveShellView -> IShellView
        if (ComCall(15, ptlb, "ptr*", psv:=0) = 0)
        {
            ; Define IID_IDispatch.
            IID_IDispatch := BufferAlloc(16)
            NumPut("int64", 0x20400, "int64", 0x46000000000000C0, IID_IDispatch)
           
            ; IShellView.GetItemObject -> IDispatch (object which implements IShellFolderViewDual)
            ComCall(15, psv, "uint", 0, "ptr", IID_IDispatch, "ptr*", pdisp:=0)
           
            ; Get Shell object.
            shellObj := ComObject(9, pdisp, 1).Application
           
            ; IShellDispatch2.ShellExecute
            shellObj.ShellExecute(prms*)
           
            ObjRelease(psv)
        }
    }
}

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release  Topic is solved

Post by swagfag » 24 Apr 2021, 09:48

Code: Select all

#Requires AutoHotkey v2.0-a132-8673ba2f

ShellRun(prms*)
{
    shellWindows := ComObject("Shell.Application").Windows
    desktop := shellWindows.FindWindowSW(0, 0, 8, 0, 1) ; SWC_DESKTOP, SWFO_NEEDDISPATCH
   
    ; Retrieve top-level browser object.
    if tlb := ComObjQuery(desktop,
        "{4C96BE40-915C-11CF-99D3-00AA004AE837}", ; SID_STopLevelBrowser
        "{000214E2-0000-0000-C000-000000000046}") ; IID_IShellBrowser
    {
        ; IShellBrowser.QueryActiveShellView -> IShellView
        if ComCall(15, tlb, "ptr*", &psv:=0) = 0
        {
            ; Define IID_IDispatch.
            IID_IDispatch := BufferAlloc(16)
            NumPut("int64", 0x20400, "int64", 0x46000000000000C0, IID_IDispatch)
           
            ; IShellView.GetItemObject -> IDispatch (object which implements IShellFolderViewDual)
            ComCall(15, psv, "uint", 0, "ptr", IID_IDispatch, "ptr*", &pdisp:=0)
           
            ; Get Shell object.
            shell := ComObjFromPtr(pdisp).Application
           
            ; IShellDispatch2.ShellExecute
            shell.ShellExecute(prms*)
           
            ObjRelease(psv)
        }
    }
}

Larkal
Posts: 21
Joined: 08 Mar 2019, 00:36

Re: Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release

Post by Larkal » 24 Apr 2021, 16:41

Awesome thanks a lot! I use this function every day so I really appreciate it.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release

Post by swagfag » 10 Jun 2021, 11:19

for some reason FindWindowSW was suddenly returning an empty string of all things. a computer restart fixed it(there were windows updates pending). probably explorer got corrupted somehow(even the equivalent v1 examples werent working)

anyway, heres an updated version:

Code: Select all

#Requires AutoHotkey v2.0-a136-feda41f4

ShellRun(prms*)
{
    shellWindows := ComObject("Shell.Application").Windows
    desktop := shellWindows.FindWindowSW(0, 0, 8, 0, 1) ; SWC_DESKTOP, SWFO_NEEDDISPATCH
   
    ; Retrieve top-level browser object.
    tlb := ComObjQuery(desktop,
        "{4C96BE40-915C-11CF-99D3-00AA004AE837}", ; SID_STopLevelBrowser
        "{000214E2-0000-0000-C000-000000000046}") ; IID_IShellBrowser
    
    ; IShellBrowser.QueryActiveShellView -> IShellView
    ComCall(15, tlb, "ptr*", sv := ComValue(13, 0)) ; VT_UNKNOWN
    
    ; Define IID_IDispatch.
    NumPut("int64", 0x20400, "int64", 0x46000000000000C0, IID_IDispatch := Buffer(16))
   
    ; IShellView.GetItemObject -> IDispatch (object which implements IShellFolderViewDual)
    ComCall(15, sv, "uint", 0, "ptr", IID_IDispatch, "ptr*", sfvd := ComValue(9, 0)) ; VT_DISPATCH
   
    ; Get Shell object.
    shell := sfvd.Application
   
    ; IShellDispatch2.ShellExecute
    shell.ShellExecute(prms*)
}

Post Reply

Return to “Ask for Help (v2)”