Results 1 to 13 of 13

Thread: Here is a little code snippet if you feel for manage your so called "Known Folders"

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    976

    Here is a little code snippet if you feel for manage your so called "Known Folders"

    As the the title says. Manage your known folders by enabling IKnownFolderManager Interface.
    I have not yet tried all methodes but it seems really useful and powerfull.
    For example setting up your own "Control Panel" for management of all the known folders.
    And for example use GetFolder method to retrieve indvidual IKnownFolder and configure them as well.
    Also I have named the function in "MS Shellish way"
    Enough with explaining
    Go ahead and play around but BE AWARE!! ALL CHANGES ARE PERMANENT!!
    Code:
    'This will remove EVERYTHING in your Library folder and add in this case "D:\" drive
    
    Private Sub Command4_Click()
      Dim pIKFM As IKnownFolderManager
      Dim pIKF As IKnownFolder
      Dim lpGUID As UUID
        
      ShGetIKnownFolderManager pIKFM
      'Libraries Main Folder
      IIDFromString StrPtr("{1B3EA5DC-B587-4786-B4EF-BD1DC332AEAE}"), lpGUID
      pIKFM.GetFolder lpGUID, pIKF
      If Not pIKF Is Nothing Then
        pIKF.SetPath KF_FLAG_DEFAULT, "D:\"
      End If
    End Sub
    
    Public Function ShGetIKnownFolderManager(pIKFM As IKnownFolderManager) As Long
      Static CLSID_KnownFolderManager As GUID
      
      Dim hr As Long
      
      hr = GuidFromStringW(StrPtr("{4df0c730-df9d-4ae3-9153-aa6b82e9795a}"), CLSID_KnownFolderManager)
      hr = CoCreateInstance(CLSID_KnownFolderManager, 0, CLSCTX_INPROC_SERVER, IID_IKnownFolderManager, pIKFM)
      
      If hr = S_OK Then
        ShGetIKnownFolderManager = S_OK
      Else
        ShGetIKnownFolderManager = hr
      End If
    End Function
    Depending on how old your system is you can use one of these two API's for converting the "bracket guid strings" to GUID Types.
    I use the Unicode version (W).

    Code:
    Public Declare Function GuidFromStringA Lib "shell32.dll" Alias "#703" (ByVal pszGUID As String, ByRef pguid As GUID) As Long
    Public Declare Function GuidFromStringW Lib "shell32.dll" Alias "#704" (ByVal pszGUID As Long, ByRef pguid As GUID) As Long
    Here is the MSDN documentation for this interface.

    https://learn.microsoft.com/en-us/wi...nfoldermanager

    Cheers

    /Kent
    Last edited by nebeln; Jan 10th, 2024 at 03:22 PM.

  2. #2
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,644

    Cool Re: Here is a little code snippet if you feel for manage your so called "Known Folder

    Quote Originally Posted by nebeln View Post
    Code:
    Public Declare Function GuidFromStringA Lib "shell32.dll" Alias "#703" (ByVal pszGUID As String, ByRef pguid As GUID) As Long
    Public Declare Function GuidFromStringW Lib "shell32.dll" Alias "#704" (ByVal pszGUID As Long, ByRef pguid As GUID) As Long
    Declaring API functions by ordinal is fairly unsafe. Except for a select few libraries that remained unchanged over the decades, most others have been constantly updated from one version of Windows to the next (mainly by adding new functions) and so the ordinals change and your code will break.

    You can easily test this by running your program in Windows 10 and 11 since these are currently the most relevant operating systems.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    976

    Re: Here is a little code snippet if you feel for manage your so called "Known Folder

    I have test runed both theese two API's on this current machine which is Win10 Pro and for me the A version not working = faults in next API CoCreateInstance but with W version CoCreateInstance returns S_OK.

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: Here is a little code snippet if you feel for manage your so called "Known Folder

    Quote Originally Posted by nebeln View Post
    As the the title says. Manage your known folders by enabling IKnownFolderManager Interface.
    I have not yet tried all methodes but it seems really useful and powerfull.
    For example setting up your own "Control Panel" for management of all the known folders.
    And for example use GetFolder method to retrieve indvidual IKnownFolder and configure them as well.
    Also I have named the function in "MS Shellish way"
    Enough with explaining
    Go ahead and play around but BE AWARE!! ALL CHANGES ARE PERMANENT!!
    Code:
    'This will remove EVERYTHING in your Library folder and add in this case "D:\" drive
    
    Private Sub Command4_Click()
      Dim pIKFM As IKnownFolderManager
      Dim pIKF As IKnownFolder
      Dim lpGUID As UUID
        
      ShGetIKnownFolderManager pIKFM
      'Libraries Main Folder
      IIDFromString StrPtr("{1B3EA5DC-B587-4786-B4EF-BD1DC332AEAE}"), lpGUID
      pIKFM.GetFolder lpGUID, pIKF
      If Not pIKF Is Nothing Then
        pIKF.SetPath KF_FLAG_DEFAULT, "D:\"
      End If
    End Sub
    
    Public Function ShGetIKnownFolderManager(pIKFM As IKnownFolderManager) As Long
      Static CLSID_KnownFolderManager As GUID
      
      Dim hr As Long
      
      hr = GuidFromStringW(StrPtr("{4df0c730-df9d-4ae3-9153-aa6b82e9795a}"), CLSID_KnownFolderManager)
      hr = CoCreateInstance(CLSID_KnownFolderManager, 0, CLSCTX_INPROC_SERVER, IID_IKnownFolderManager, pIKFM)
      
      If hr = S_OK Then
        ShGetIKnownFolderManager = S_OK
      Else
        ShGetIKnownFolderManager = hr
      End If
    End Function
    Depending on how old your system is you can use one of these two API's for converting the "bracket guid strings" to GUID Types.
    I use the Unicode version (W).

    Code:
    Public Declare Function GuidFromStringA Lib "shell32.dll" Alias "#703" (ByVal pszGUID As String, ByRef pguid As GUID) As Long
    Public Declare Function GuidFromStringW Lib "shell32.dll" Alias "#704" (ByVal pszGUID As Long, ByRef pguid As GUID) As Long
    Here is the MSDN documentation for this interface.

    https://learn.microsoft.com/en-us/wi...nfoldermanager

    Cheers

    /Kent
    Why choose a function that is only exported by ordinal and may not be supported beyond Vista according to the docs https://learn.microsoft.com/en-us/wi...guidfromstring when the documentation even suggest two other supported ways of doing this?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    976

    Re: Here is a little code snippet if you feel for manage your so called "Known Folder

    Hi
    Thank for asking...I did this totally willingly because I wanted to be "in the shell" since the the API's are exported from the Shell32.
    Yes, for sure you can use IIDFromString or CLSIDFromString if you feel that more secure.

    /Kent

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: Here is a little code snippet if you feel for manage your so called "Known Folder

    Quote Originally Posted by nebeln View Post
    Hi
    Thank for asking...I did this totally willingly because I wanted to be "in the shell" since the the API's are exported from the Shell32.
    Yes, for sure you can use IIDFromString or CLSIDFromString if you feel that more secure.

    /Kent
    Nothing to do with security, simply that the documentation itself recommends using one of the other functions instead.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    976

    Re: Here is a little code snippet if you feel for manage your so called "Known Folder

    But as long as my stated API's do the deed's there is no worry...so feel free to change API

    /Kent

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    976

    Re: Here is a little code snippet if you feel for manage your so called "Known Folder

    It can be a "False true" because if you check the "contributors"..they are writing this, not MS...so there is a little bit as "Wikipedia"
    And if you double check with Geoff Chapell - he never says that GUIDFromString should be swapped with IID or CLSID.
    Last edited by nebeln; Jan 10th, 2024 at 06:07 PM.

  9. #9
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: Here is a little code snippet if you feel for manage your so called "Known Folder

    Quote Originally Posted by nebeln View Post
    It can be a "False true" because if you check the "contributors"..they are writing this, not MS...so there is a little bit as "Wikipedia"
    It is the official MS documentation, it is most certainly not just like Wikipedia - if you check those 5 contributors then at least 4 of them are MS employees.

    I just think it is generally good practice to show code samples that use the recommended, and supported, functionality when such functionality exists.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    976

    Re: Here is a little code snippet if you feel for manage your so called "Known Folder

    As I told I did this total deliberatily even if I knew the recommenadtions because I wanted to keep it inside "the shell"

  11. #11
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,667

    Re: Here is a little code snippet if you feel for manage your so called "Known Folder

    If you have those interfaces defined, it's presumably because you have oleexp. So why not use this:

    Code:
      Dim pIKFM As IKnownFolderManager
      Dim pIKF As IKnownFolder
    
    Set pIKFM = New KnownFolderManager
    
    pIKFM.GetFolder FOLDERID_Libraries, pIKF
    Dropping in mIID.bas makes sure virtually none of the shell programming IIDs, FOLDERIDs, etc, need 5 minutes spent duplicating the effort for every one. Then you don't need to use an API *at all*.

    As long as you're not nebeln with a project requirement of 'no matter how inconvenient, make sure not to do things the standard way, especially if fafalone does", I'd strongly recommend the above approaches.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    976

    Re: Here is a little code snippet if you feel for manage your so called "Known Folder

    By doing as you explain, then you’re exactly as Schmith but with you as a header text and not MS 🤣

  13. #13
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,667

    Re: Here is a little code snippet if you feel for manage your so called "Known Folder

    Yeah I'm not telling to you use an opaque binary blob with less functionality, but thanks for telling me how little you understand, well, anything I say, anything Schimdt says, or how any of this all really works. Class wrappers aren't the same as coclass declarations and predefined UUIDs.

    The equivalent here would be if I had suggested using shell32.Shell's special folder functionality.

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