ComObj Windows Shell - documentation question

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
neogna2
Posts: 600
Joined: 15 Sep 2016, 15:44

ComObj Windows Shell - documentation question

13 Jan 2021, 04:32

We can use ComObjCreate to interact with an Explorer window.

Code: Select all

;HWND to the active Explorer window
handle := WinExist("A")

for window in ComObjCreate("Shell.Application").Windows
    if (window.hwnd = handle)
    {
        sfv := window.Document
        path := sfv.Folder.Self.Path
        ;do something with path to the active Explorer window
        ;...
      }
}
I'm trying to find the Microsoft (or other) documentation page on properties and methods for the window object in the example code. Help me fill in the ????? gap below.

ComObjCreate("Shell.Application")
https://docs.microsoft.com/en-us/windows/win32/shell/shell-application

ComObjCreate("Shell.Application").Windows
https://docs.microsoft.com/en-us/windows/win32/shell/shell-windows
https://docs.microsoft.com/en-us/windows/win32/shell/shellwindows

for window in ComObjCreate("Shell.Application").Windows , window.hwnd , window.Document
????? Missing: Doc page on properties and methods HWND, Document, ...

path := sfv.Folder.Self.Path
https://docs.microsoft.com/en-us/windows/desktop/shell/folder
https://docs.microsoft.com/en-us/windows/win32/shell/folder2-self
https://docs.microsoft.com/en-us/windows/win32/shell/folderitem
https://docs.microsoft.com/en-us/windows/win32/shell/folderitem-path
User avatar
Xeo786
Posts: 760
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: ComObj Windows Shell - documentation question

13 Jan 2021, 08:20

there are so many example already on the forum

https://www.autohotkey.com/boards/viewtopic.php?f=6&t=35041
https://www.autohotkey.com/boards/viewtopic.php?f=7&t=31755
be specific what are you up to ?
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
neogna2
Posts: 600
Joined: 15 Sep 2016, 15:44

Re: ComObj Windows Shell - documentation question

13 Jan 2021, 10:00

Xeo786 wrote:
13 Jan 2021, 08:20
there are so many example already on the forum

https://www.autohotkey.com/boards/viewtopic.php?f=6&t=35041
https://www.autohotkey.com/boards/viewtopic.php?f=7&t=31755
be specific what are you up to ?
The linked resources/example by jeeswg are great but not what I'm asking for. I'm looking for Microsoft documentation with definitions and a complete list of methods/properties for the gap I pointed to above.

I'm not trying to accomplish a specific task right now, I just want to understand.

I have now read more Microsoft doc pages and will take a shot at partially filling in the gap I pointed to, step by step. I'm probably confused so please point out what I get wrong and what I get right here.

ComObjCreate("Shell.Application").Windows
This AHK code uses the Shell.Windows method to return "a ShellWindows object. This object represents a collection of all of the open windows that belong to the Shell."
https://docs.microsoft.com/en-us/windows/win32/shell/shell-windows

for window in ComObjCreate("Shell.Application").Windows
This AHK code uses a for loop to get each window in the ShellWindows object collection. It does so by using the method "Item" which "Retrieves an InternetExplorer object that represents the Shell window."
https://docs.microsoft.com/en-us/windows/win32/shell/shellwindows
The doc page for InternetExplorer object is at
https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa752084(v=vs.85)
The name sounds like it would be an object specifically related to internet browser windows. But in reality it can also be other things, like a File Explorer window. However confusingly File Explorer is not explicitly mentioned on that doc page. The doc page list events, methods and properties but does not describe which of those pertain to File Explorer windows.

window.hwnd
This AHK code gets the the HWND handle of the currently looped over window (InternetExplorer object). But the property hwnd is not documented on the InternetExplorer object doc page. (Perhaps it is a property that shell objects have more generally and therefore not listed specifically among the InternetExplorer object properties?)

window.Document
This AHK code uses the InternetExplorer object property Document which "Gets the automation object of the active document, if any."
The Document property doc page is at
https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa752052(v=vs.85)
and says "When other document types [not a HTML page] are active [...] this property returns the default IDispatch dispatch interface (dispinterface) pointer for the hosted document object."

The IDispatch interface (oaidl.h) doc page is
https://docs.microsoft.com/en-us/windows/win32/api/oaidl/nn-oaidl-idispatch
and says "Exposes objects, methods and properties to programming tools and other applications that support Automation. COM components implement the IDispatch interface to enable access by Automation clients"

Where is it documented that once we have window.Document we can proceed with window.Document.Folder and then other steps in the example in my OP? I don't know.
User avatar
Xeo786
Posts: 760
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: ComObj Windows Shell - documentation question

14 Jan 2021, 00:38

I do not know in very deep what I understand is that, the explorer containing files is simply a document, which is visible on our screen, and handled through shell

Code: Select all

objFolder := oShell.explore(MyFolders) 
; oShell.open(MyFolders) 	 ; seems same as open
; objFolder := oShell.BrowseForFolder(0,"Select",0,MyFolders) ; Select folder 
; msgbox, % objFolder.ParentFolder.Self.Path
and there is Namespace to do many stuff without making/showing explorer so what I understand document is just for visibility
and ComObjCreate("Shell.Application").Windows is object return with all the windows having document

Code: Select all

objFolder := oShell.NameSpace(MyFolders)
objFolderItems := objFolder.Items()
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
neogna2
Posts: 600
Joined: 15 Sep 2016, 15:44

Re: ComObj Windows Shell - documentation question

14 Jan 2021, 18:45

Thanks for the feedback. I hadn't used Shell.NameSpace for windowless COM scripting before, nice to learn new things.

I looked again at the Microsoft docs. Here is my updated attempt to fill in the gap. I'm trying to spell out every step to make it stick in my memory. Perhaps I'm still missing or confusing something and in that case let me know and help me get it right. :thumbup:


COM, Windows Shell and File Explorer windows
To use COM to do things with a File Explorer window we have to take a few steps through the Windows Shell hierachy.
I use this script snippet as a starting point and spell out some of its steps.

Code: Select all

;HWND to the active Explorer window
handle := WinExist("A")

for window in ComObjCreate("Shell.Application").Windows
    if (window.hwnd = handle)
    {
        sfv := window.Document
        path := sfv.Folder.Self.Path
        ;do something with path to the active Explorer window ...
    }
}
ComObjCreate("Shell.Application").Windows
This AHK code uses what Microsoft names the Shell.Windows method to get a ShellWindows object that "represents a collection of all of the open windows that belong to the Shell."

for window in ComObjCreate("Shell.Application").Windows
This AHK code iterates the ShellWindows object and at each step gets "an InternetExplorer object that represents the Shell window." But the InternetExplorer object doc page is a little confusing, for four reasons.
  1. The name "InternetExplorer" sounds like something specific to the old Internet Explorer browser built into Windows. But in reality this object can also represent other things, like a File Explorer window.
  2. The doc page does not explicitly mention File Explorer windows, even though it partly applies to them.
  3. Some of the methods and properties are not usable on File Explorer windows.
  4. The doc page does not document the property hwnd, which returns the window's handle: window.hwnd
sfv := window.Document
This AHK code uses the InternetExplorer object's property Document to get a ShellFolderView object.
I 'm naming it "sfv", short for ShellFolderView, like I've seen others on the AutoHotkey forums do.
This step can be confusing because of a gap in Microsoft's documentation:
  • The docs page for Document property does not say that it gives us a ShellFolderView object.
  • The docs page for ShellFolderView object does not say that we get it from the Document property.
Once that connection is made clear the pieces fit together.

The ShellFolderView object "Represents the objects in a view and provides properties and methods used to obtain information about the contents of the view." In other words, it lets us do things with the File Explorer window (change sort order, select files, toggle between icon view and list view, ...) and with its files and folders (open a file, move a file, ...).

Methods/properties to read or change the File Explorer window
For example SelectedItems , SelectItem , FocusedItem and CurrentViewMode
They are listed on the docs page shellfolderview and some more here ishellfolderviewdual , ishellfolderviewdual2 , ishellfolderviewdual3

Methods/properties for doing things with the files and folders
This takes two steps.
  1. Use the ShellFolderView object's Folder property to get a Folder object.
  2. Then use the Folder object's properties/methods to do things with one or more of its files and (sub)folders. They are listed at folder and folder2-object
For example we can use the Folder object's FolderItems method to get a FolderItems object and iterate over it to get each FolderItem object's Name property. In short, get the name of each file in the File Explorer window.

Code: Select all

for item in sfv.Folder.Items
  MsgBox % item.Name
Right?
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: ComObj Windows Shell - documentation question

15 Jan 2021, 04:33

This might be useful:

Code: Select all

#NoEnv
For Window in ComObjCreate("Shell.Application").Windows
{
   MsgBox, 0, Interface Name, % "Window: "      . ComObjType(Window, "Name")
   MsgBox, 0, Interface Name, % "Document: "    . ComObjType(Window.Document, "Name")
   MsgBox, 0, Interface Name, % "Folder: "      . ComObjType(Window.Document.Folder, "Name")
   MsgBox, 0, Interface Name, % "Folder.Self: " . ComObjType(Window.Document.Folder.Self, "Name")
   Break
}

See also: MSHTML -> IWebBrowser2
neogna2
Posts: 600
Joined: 15 Sep 2016, 15:44

Re: ComObj Windows Shell - documentation question

19 Jan 2021, 18:06

just me wrote:
15 Jan 2021, 04:33
This might be useful:
Yes, that helps. I expanded it to also get "Class" values.

Code: Select all

For Window in ComObjCreate("Shell.Application").Windows
{
   Info := ""
   Try Info .= Window.FullName
   Info .= "`n--------------------`n"
   Info .= "Element: Interface Name  (Class)"
   Info .= "`n--------------------`n"
   Info .= "Window: "
   Try Info .=       ComObjType(Window, "Name") 
   Try Info .= "  (" ComObjType(Window, "Class") ")"
   Info .= "`n" "Document: "
   Try Info .=       ComObjType(Window.Document, "Name") 
   Try Info .= "  (" ComObjType(Window.Document, "Class") ")"
   Info .= "`n" "Folder: "
   Try Info .=       ComObjType(Window.Document.Folder, "Name") 
   Try Info .= "  (" ComObjType(Window.Document.Folder, "Class") ")"
   Info .= "`n" "Folder.Self: "
   Try Info .=       ComObjType(Window.Document.Folder.Self, "Name") 
   Try Info .= "  (" ComObjType(Window.Document.Folder.Self, "Class") ")"
   MsgBox % Info
   Break
}
Results for a File Explorer window
C:\WINDOWS\explorer.exe
--------------------
Element: Interface Name (Class)
--------------------
Window: IWebBrowser2 (WebBrowser)
Document: IShellFolderViewDual3 (ShellFolderView)
Folder: Folder3 ()
Folder.Self: FolderItem2 (ShellFolderItem)
From what I can see this confirms my attempt earlier to understand the parts and steps. We first get an InternetExplorer object (Class name "WebBrowser", Interface name "IWebBrowser2"). Next we use its Document property to get a ShellFolderView object.

The MSHTML doc page you linked has child pages for IWebBrowser2 and a HWND property. The HWND property doc fills in the gap for point number 4 in my earlier post. But those pages do not, from what I see, fill in the gap in the MS documentation between the Document property and the ShellFolderView object. Do you know of some other Microsoft (or other) documentation specifically for COM and File Explorer windows that covers that explicitly? I can write and use Shell COM code in AutoHotkey without that extra documentation, but it would be nice to find.

Out of curiousity I also ran the ComObjType script on an Internet Explorer browser window and got these results.
C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE
--------------------
Element: Interface Name (Class)
--------------------
Window: IWebBrowser2 (WebBrowser)
Document: JScriptTypeInfo (HTMLDocument)
Folder:
Folder.Self:
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: ComObj Windows Shell - documentation question

21 Jan 2021, 04:48

I didn't find more docs, but if you can get the Windows header file ExDisp.h you'll find the definitions of the interfaces there.
Inserio
Posts: 5
Joined: 29 Jul 2018, 14:49

Re: ComObj Windows Shell - documentation question

20 Nov 2023, 01:24

Sorry to resurrect this but I came here looking for some details about this and struggled like the rest.
That is, until I went to PowerShell instead. If anyone gets stuck with this again, you'll probably have a much easier time (at least from a practical perspective) going through the results using PowerShell.

For example:

Code: Select all

$hwnd = 0x50f34
((New-Object -ComObject Shell.Application).Windows() | Where-Object -Property HWND -eq $hwnd).Document | Get-Member
Output:
image.png
image.png (50.57 KiB) Viewed 465 times

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], peter_ahk and 329 guests