Code: Select all
#Requires Autohotkey v2.0
; ----------------------------------------------------------------------------------------------------------------------
KF := GetKnownFoldersPaths()
; ----------------------------------------------------------------------------------------------------------------------
GetKnownFoldersPaths() {
Local KnownFolders := {}
; ===================================================================================================================
; IKnownFoldersManager interface
; https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-iknownfoldermanager
; CLSID_KnownFoldersManager = "{4DF0C730-DF9D-4AE3-9153-AA6B82E9795A}"
; IID_IKnownFoldersManager = "{8BE2D872-86AA-4d47-B776-32CCA40C7018}"
; ===================================================================================================================
Local IKFM := ComObject("{4DF0C730-DF9D-4AE3-9153-AA6B82E9795A}", "{8BE2D872-86AA-4d47-B776-32CCA40C7018}")
; Gets an array of all registered known folder IDs. This can be used in enumerating all known folders.
; https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-iknownfoldermanager-getfolderids
Local KFID := 0
Local CID := 0
ComCall(5, IKFM, "PtrP", &KFID, "IntP", &CID)
; Loop thru all returned folder IDs.
Local IKF := 0
Local StrPtr := 0
Local KFP := 0
Local Alias := ""
Local Path := ""
Local PKFID := KFID ; pointer to the current KNOWNFOLDERID
Loop CID {
; Gets an object that represents a known folder identified by its KNOWNFOLDERID.
; https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-iknownfoldermanager-getfolder
ComCall(6, IKFM, "Ptr", PKFID, "PtrP", &IKF)
; Retrieves a structure that contains the defining elements of a known folder,
; which includes the folder's category, name, path, description, tooltip, icon, and other properties.
; https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-iknownfolder-getfolderdefinition
Local KFD := Buffer(40 + (A_PtrSize * 9), 0)
ComCall(11, IKF, "Ptr", KFD.Ptr)
; Get the Name field of the structure
Alias := StrReplace(StrGet(NumGet(KFD, A_PtrSize, "UPtr")), " ")
; Free the strings pointed to by structure fields
FreeKnownFolderDefinitionFields(KFD)
; Retrieve the path of the known folder as a string.
; https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-iknownfolder-getpath
Path := ""
If !ComCall(6, IKF, "UInt", 0, "PtrP", &KFP, "UInt") {
Path := ExpandEnvironmentStrings(StrGet(KFP))
DllCall("Ole32.dll\CoTaskMemFree", "Ptr", KFP)
}
; Release the IKnownFolder interface
IKF := 0
; Add the name and path to the object, if a path was retrieved
If !(Path = "")
KnownFolders.%Alias% := Path
; Switch to the next KNOWNFOLDERID
PKFID += 16
}
; Free the KnownFolderID object
DllCall("Ole32.dll\CoTaskMemFree", "Ptr", KFID)
; Release the IKnownFolderManager interface
IKFM := 0
; Return the folder object
Return KnownFolders
; -------------------------------------------------------------------------------------------------------------------
FreeKnownFolderDefinitionFields(KFD) {
Static OffSets := [A_PtrSize, A_PtrSize, A_PtrSize + 16, A_PtrSize, A_PtrSize, A_PtrSize, A_PtrSize, A_PtrSize]
Local Offset := 0
For O In Offsets {
Offset += O
DllCall("Ole32.dll\CoTaskMemFree", "Ptr", NumGet(KFD.Ptr + OffSet, "UPtr"))
}
}
; -------------------------------------------------------------------------------------------------------------------
ExpandEnvironmentStrings(Str) {
Local Chars := 0
Local Expanded := ""
If (Chars := DllCall("ExpandEnvironmentStringsW", "Str", Str, "Ptr", 0, "UInt", 0, "Int")) {
VarSetStrCapacity(&Expanded, ++Chars)
DllCall("ExpandEnvironmentStringsW", "Str", Str, "Str", &Expanded, "UInt", Chars, "Int")
}
Return (Chars ? Expanded : Str)
}
}
; ======================================================================================================================
Main := Gui( , "KnownFolders")
Main.OnEvent("Close", (*) => ExitApp())
Main.SetFont("s10", "Lucida Console")
MainLV := Main.AddListView("w1200 r30 Grid", ["Index", "Alias", "Path"])
For K In KF.OwnProps()
MainLV.Add("", A_Index, K, KF.%K%)
Loop MainLV.GetCount("Column")
MainLV.ModifyCol(A_Index, "AutoHdr")
Main.Show()