
Windows 7 introduced the Library to the filesystem. But it's no ordinary folder. There are a few reasons you'd want to be able to work directly with these objects in VB... the issue first came up for me when I was writing a search program, and wanted to let the user select a library to search and then automatically search all folders in the library. Another time I came across the issue was for saving... if you pick a library from a save box, what folder should it go to? You need the library default save location. Further down the road, it's possible to actually write a program to manage libaries and even create new ones in VB. For that, I'll create a complete wrapper of IShellLibrary in a class module.
For now, however, I wanted to share a small sample program illustrating the basics of interacting with Library objects through the IShellLibrary interface. This program shows how to display basic Library information, and how to add a folder to an existing library.
As a bonus, it shows how to use a couple of important interfaces in VB- IShellItem and IEnumShellItems.
Note: You'll need to update the project reference to whereever you extract olelib.tlb to.
Alternative: Create a new project, add the reference, then add Form1.frm and modLib.bas.
(although I've never heard of a .tlb capable of being used as a malware vector since it's not executable per-se, the paranoid among you will find the source code that can be put through MKTYPLIB to create a hash-identical olelib.tlb)
Preview function - Getting all folders included in a library:
Code:
Public Function Library_GetFolders(pidlLib As Long, sout() As String) As Long
'WINDOWS 7 AND ABOVE ONLY:
'Given the fully qualified pidl for a library (including new ones besides music/videos/docs/pics)
'returns an array of the folders that are included in the library
Dim isi As IShellItem, isiChild As IShellItem, isiDef As IShellItem
Dim isl As IShellLibrary
Dim iesi As IEnumShellItems
Dim isia As IShellItemArray
Dim sinpt As Long, sinsz As String
Dim sinpt2 As Long, sinsz2 As String
Dim objClsid As GUIDA
Dim hRes As Long, hr As Long
Dim i As Long
ReDim sout(0)
hRes = SHCreateShellItem(0, 0, pidlLib, isi)
'create a GUID from each CLSID string
Call CLSIDFromString(StrPtr(CLSID_ShellLibrary), objClsid)
'obtain an interface pointer for idClsid
hr = CoCreateInstanceISL(objClsid, 0&, _
CLSCTX_INPROC_SERVER, _
IID_IShellLibrary, _
isl)
Call isl.LoadLibraryFromItem(isi, STGM_READ)
isl.GetFolders LFF_ALLITEMS, IID_IShellItemArray, isia
isia.EnumItems iesi
Do While (iesi.Next(1, isiChild, 0) = 0)
ReDim Preserve sout(i)
isiChild.GetDisplayName SIGDN_FILESYSPATH, sinpt
sinsz = BStrFromLPWStr(sinpt, True)
If sinsz <> "" Then
sout(i) = sinsz
i = i + 1
End If
Set isiChild = Nothing
Loop
If sinpt Then Call CoTaskMemFree(sinpt)
Set isl = Nothing
Set isi = Nothing
Set isia = Nothing
Set iesi = Nothing
End Function