After creating the library, we’ll use Shell API to perform operations on libraries, such as to set the icon of the library, and the default path to save the library, or enumerate all of the folders in the library, and so on.
#include “stdafx.h”
#include <shobjidl.h>
#include <objbase.h> // IID_PPV_ARGS macro
#include <Knownfolders.h>
#include <Shlguid.h.>
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
// Create the library
//manipulate the library
IShellLibrary *pslLibrary;
// load the library that already exists and manage it .
HRESULT hr = SHLoadLibraryFromParsingName(L“C:\\Users\\Win7\\
AppData\\Roaming\\Microsoft\\Windows\\Libraries\\
MyDownload.library-ms“,
STGM_READWRITE, IID_PPV_ARGS(&pslLibrary));
if(SUCCEEDED(hr))
{
// To set the icon of the library
hr = pslLibrary->SetIcon(L“C:\\Windows\\System32\\SHELL32.dll,-14″);
// To set the type of the library
hr = pslLibrary->SetFolderType(FOLDERTYPEID_GenericLibrary);
//traverse all folders in the library in the loop.
IShellItemArray *psiaFolders;
hr = pslLibrary->GetFolders(LFF_STORAGEITEMS,
IID_PPV_ARGS(&psiaFolders));
IEnumShellItems *penumShellItems;
psiaFolders->EnumItems(&penumShellItems);
DWORD dwCount = 0;
psiaFolders->GetCount(&dwCount);
IShellItem *psiFolder;
// traverse all folders the library managed in the loop.
for(DWORD dwIndex = 0; dwIndex < dwCount; ++dwIndex )
{
// To obtain the folder
psiaFolders->GetItemAt(dwIndex, &psiFolder );
WCHAR strFolderName[256] = L“”;
LPWSTR *pName = (LPWSTR*)strFolderName;
// To obtain the name of the folder.
hr = psiFolder->GetDisplayName(SIGDN_NORMALDISPLAY,
(LPWSTR*)pName);
if(SUCCEEDED(hr))
{
// To compare the name of the folder with the “Tools”
// That is ,to find the folder named “Tools”
if(wcscmp( *pName, L“Tools”) == 0)
{
// if the “Tools ”folder could be found ,
//and the folder would be set the default folder for the library to save the file.
hr = pslLibrary- >SetDefaultSaveFolder(DSFT_PRIVATE, psiFolder);
}
}
}
// To submit the changes to the library.
pslLibrary->Commit();
pslLibrary->Release();
}
}
::CoUninitialize();
return 0;
}
In this code, we firstly use SHLoadLibraryFromParsingName function to load the library from its definition file and create an IShellLibrary object.
Then we could do the operation on the library with the operation functions provided by the IShellLibrary object. In the code, we modified the icon of the library by using SetIcon function, which accepted a string as a parameter and assigned the name of DLL and Icon Index.
Afterwards, we modified the type of the library by using SetFolderType function ,which could accept GUID as the parameter ,which defined the types of the library ,which could be one of the following types: Generic types, picture, music, video, document and so on. When setting the type of library, we can change the Windows explorer view of the library, and enable the search and the view options specially designated for the library type. The next step is to traverse all folders in the library in the loop and find the folder named “Tools” and the folder will be set the default folder for the library to save the file. By default, when we chose a library to save the files in CFD, the system would use the first folder to save the files .However; we could re-specify the default location of the library by modifying the default folder. At last, we can call Commit function to submit the changes to the library, and that is, to write these changes to the library definition files, in order to complete the operation on the library.