User Download Folder << Back

Bob Worsley asked how he can find out where the default user download folder is at. The default download folder for the current user can be obtained through the official Windows API (Please do not poke at the registry).

Samuel Pizarro answered Bob's post by pointing at the official MSDN documentation - SHGetKnownFolderPath and a list of Known Folder Id. Truth to be told I had never called that API myself before. I thought it would be a good exercise to try to use it in VDF 16.1 and DF 20.1.

Use UI

#IF !@<200
Use CharTranslate.pkg

External_Function CLSIDFromString "CLSIDFromString" OLE32.DLL WString lpsz Pointer pclsid Returns UInteger
External_Function SHGetKnownFolderPath "SHGetKnownFolderPath" SHELL32.DLL Pointer rfid UInteger dwFlags UInteger hToken Pointer ppszPath Returns UInteger
External_Function CoTaskMemFree "CoTaskMemFree" Ole32.dll Pointer pv Returns UInteger

Define FOLDERID_Downloads For "{374DE290-123F-4565-9164-39C4925E467B}"

Function KnownFolder Global String sFolderGuid Returns String
	Pointer ppszPath
	UInteger uVoid hResult
	String sFolder
	UChar[16] g
	// Converting a string Guid to actual Guid
	#IF !@<200
		Pointer pWideString
		Integer iVoid
		Move (OemToUtf16Buffer(AddressOf(sFolderGuid), -1)) to pWideString
		Move (CLSIDFromString(pWideString, AddressOf(g))) to hResult
		Move (Free(pWideString)) to iVoid
		Move (CLSIDFromString(sFolderGuid, AddressOf(g))) to hResult
	// Actually the path of the known folder specified by sFolderGuid
	Move 0 to ppszPath
	Move (SHGetKnownFolderPath(AddressOf(g), 0, 0, AddressOf(ppszPath))) to uVoid
	// Convert the LPWSTR to OEM (<20.0) / UTF (20.0+) string type
	#IF !@<200
		Address aPath
		Move (Utf16ToOemBuffer(ppszPath, -1)) to aPath
		Move aPath to sFolder
		Move (PointerToWString(ppszPath)) to sFolder
	Move (CoTaskMemFree(ppszPath)) to uVoid
	Function_Return sFolder

Free Web Hosting