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

Get help for the alpha version of AutoHotkey v2 here. Please state the v2 version you are working with in the title when making a new topic.
Larkal
Posts: 14
Joined: 08 Mar 2019, 00:36

Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release

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: 4243
Joined: 11 Jan 2017, 17:59

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

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: 4481
Joined: 17 Jul 2016, 01:02
Contact:

Re: Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release

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: 4243
Joined: 11 Jan 2017, 17:59

Re: Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release

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: 4481
Joined: 17 Jul 2016, 01:02
Contact:

Re: Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release

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: 14
Joined: 08 Mar 2019, 00:36

Re: Help Updating Lexikos ShellRun for Latest AutoHotkey 2 Release

05 Jul 2020, 17:37

@swagfag Thank you very much!!

Return to “AutoHotkey v2 Help”

Who is online

Users browsing this forum: No registered users and 0 guests