Results 1 to 4 of 4

Thread: [VB6] Working with Libraries (Win7+)

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,654

    [VB6] Working with Libraries (Win7+)


    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:
    This project uses my updated version of olelib. The included olelib.tlb has minimal modifications and is identical to the original in every way except for IShellFolder, IShellFolder2, and IEnumIDList- which were implemented incorrectly and have been corrected (those and interfaces/dll calls in shell32.dll that take those as parameters are now in oleexp.tlb); and the TaskbarList coclass has been upgraded to ITaskbarList4 and subsequently now resides in oleexp. See olechanges.txt for more info.
    You'll need to update the project reference to whereever you extract olelib.tlb and oleexp.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)

    UPDATE: olelib/oleexp no longer included in ZIP, always get latest version from http://www.vbforums.com/showthread.p...ary-oleexp-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
    twinBASIC 64bit compatible version at https://github.com/fafalone/MiscDemos
    Attached Files Attached Files
    Last edited by fafalone; Oct 30th, 2025 at 01:25 AM. Reason: Attached project updated to reference oleexp.tlb 4.0 or higher

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,654

    Re: [VB6] Working with Libraries (Win7+)

    Project updated:
    -Shows use of .RemoveFolder
    -Shows use of .SetContentType
    -Uses newer olelib upgrade that is much more compatible with the old one

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [VB6] Working with Libraries (Win7+)

    Just a heads up.

    SIGDN_FILESYSPATH won't return anything if the folder is a virtual folder, i.e., Control Panel

    If sinpt Then Call CoTaskMemFree(sinpt) line should be in the loop I believe. Each call to GetDisplayName that returns a pointer must have the memory freed on that pointer, per documentation. If your BStrFromLPWStr() function already addresses this, then ignore that last comment.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    976

    Re: [VB6] Working with Libraries (Win7+)

    It's working using CoCreateInstance as it's declared in MSDN
    Sorry for waking up a thread in sleep

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width