0

I'm trying to change the keyword property (under the Summary Information property set) of a folder.

I am aware that I can accomplish this using the StgCreatePropSetStg() function, which takes an IStorage object that will contain the property sets. I can then get an IStorage using StgCreateStorageEx().

One thing that bothers me is that I don't know how to make the generated IStorage object point to the path of the folder that I want to change the property.

I've tried to modify the sample in the documentation and ended up with this:

#include <stdio.h>
#include <windows.h>
#include <ole2.h>

int main() {

   HRESULT hr = S_OK;
   IPropertySetStorage *pPropSetStg = NULL;
   IPropertyStorage *pPropStg = NULL;
   WCHAR *pwszError = L"";
   PROPSPEC propspec; 
   PROPVARIANT propvarWrite; 
   PROPVARIANT propvarRead;

   try
   {
        // Create a file and a property set within it.

        //  ~~~~~ I`m not sure how to make an IStorage object point to a folder path
        hr = StgCreateStorageEx( L"WriteRead.stg",
                   STGM_CREATE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE,
                   STGFMT_STORAGE,
                   // STGFMT_STORAGE => Structured Storage 
                                     // property sets
                   // STGFMT_FILE    => NTFS file system 
                                     // property sets
                   0, NULL, NULL,
                   IID_IPropertySetStorage,
                   reinterpret_cast<void**>(&pPropSetStg) );
        if( FAILED(hr) ) throw L"Failed StgCreateStorageEx";

        hr = pPropSetStg->Create( fmtid, NULL, PROPSETFLAG_DEFAULT, 
                    STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE,
                    &pPropStg );
        if( FAILED(hr) ) throw L"Failed IPropertySetStorage::Create";

        // Write a Unicode string property to the property set

        propspec.ulKind = PRSPEC_LPWSTR;
        propspec.lpwstr = L"PIDSI_KEYWORDS";

        propvarWrite.vt = VT_LPWSTR;
        propvarWrite.pwszVal = L"Testing Tag";

        hr = pPropStg->WriteMultiple( 1, &propspec, &propvarWrite, 
                                      PID_FIRST_USABLE );
        if( FAILED(hr) ) 
            throw L"Failed IPropertyStorage::WriteMultiple";

        // Commit changes to the property set.

        hr = pPropStg->Commit(STGC_DEFAULT);
        if( FAILED(hr) ) 
            throw L"Failed IPropertyStorage::Commit";

        // Close and reopen everything.

        pPropStg->Release(); pPropStg = NULL;
        pPropSetStg->Release(); pPropSetStg = NULL;
   }
   catch( const WCHAR *pwszError )
   {
       wprintf( L"Error:  %s (hr=%08x)\n", pwszError, hr );
   }

   PropVariantClear( &propvarRead );

   if( pPropStg ) pPropStg->Release();
   if( pPropSetStg ) pPropSetStg->Release();
}

Having said all of that,

My questions is: How to make the generated IStorage object point to a folder path?

Also, if I have some misconception about how IStorage works, please correct me.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Eliazar
  • 301
  • 3
  • 13
  • 1
    `IStorage` is meant for reading and writing to pieces of a [Compound File](https://learn.microsoft.com/en-us/windows/win32/stg/compound-files). Doesn't really apply to folders. – Mark Ransom Nov 02 '22 at 13:16
  • 1
    Notice that the link you gave specifically talks about "documents". – Mark Ransom Nov 02 '22 at 13:19
  • Apologies for the late reply. Does this mean that there's absolutely no way for me to change the property of a folder using Windows API? If so, it seems like manually editing the desktop.ini is my only choice. – Eliazar Nov 02 '22 at 23:10
  • 1
    I won't say it's impossible, but it doesn't look like `IStorage` would be the way to do it - and I'm not personally aware of another. – Mark Ransom Nov 02 '22 at 23:23
  • 1
    [This piece of documentation](https://learn.microsoft.com/en-us/windows/win32/shell/how-to-customize-folders-with-desktop-ini) suggests that you should just manipulate `Desktop.ini` as a regular file. – Igor Tandetnik Nov 03 '22 at 00:23

0 Answers0