Microsoft Study Bible

October 16, 2009

How to manipulate the library with unmanaged codes?

We could use the library Shell API in Windows 7 and unmanaged codes to manage the libraries .When downloading the software, we often used the folder to classify kinds of resources. For example, among those software, the resource which were downloaded were organized in “All downloads” .In fact, those resources could be stored in different directories and partitions on your hard disk. To be more conveniently use Windows-Explorer to access all of those resources, there is need to create a “MyDownload” library, which is corresponding to the Classified Management way of the “All downloads” file, and manage all of resources downloaded in.

   To better display how to manipulate the library with Shell API, we create a simple console application with Visual C++. In the main function, we’ll create and do the operation of library.

 #include “stdafx.h”
// the introduction of header files

#include <shobjidl.h> // introduce Shell API
#include <objbase.h>
// define  IID_PPV_ARGS  macro
#include <Knownfolders.h> // introduce FOLDERID

int _tmain(int argc, _TCHAR* argv[])
{
    
// COM initialization
    CoInitialize(NULL);
    
// use Shell API to create a library

    IShellLibrary *pIShelLibrary;
    HRESULT hr = SHCreateLibrary(IID_PPV_ARGS(&pIShelLibrary));
    
if (SUCCEEDED(hr))
    {
        
// if the library are successfully created,add the paths of the different files to it.

        IShellItem *pIShellItem;
        SHAddFolderPathToLibrary(pIShelLibrary,
        L
“C:\\Users\\Public\\Pictures”);
        SHAddFolderPathToLibrary(pIShelLibrary,
        L
“C:\\Users\\Public\\Music”
);
        SHAddFolderPathToLibrary(pIShelLibrary,
        L
“D:\\Tools”
);
        SHAddFolderPathToLibrary(pIShelLibrary,
        L
“D:\\Video”
);
        
// store current library into the directories of the system library .
        // that is to say ,to add a new library
MyDownload

        hr = pIShelLibrary->SaveInKnownFolder(FOLDERID_Libraries ,
        L
“MyDownload”
,
        LSF_MAKEUNIQUENAME,
        &pIShellItem);
      
// release the object.
       pIShellItem->Release();
       pIShelLibrary->Release();
  }
    
// release COM

    ::CoUninitialize();
    
return 0;

 

In this code, at first, we introduce the header files that Shell API required .Then in the main function, because those APIs all are based on .COM, we should firstly do COM initialization .After initialization, we can do the operation of the libraries by Shell API. In the code example, we create a new library object with SHCreateLibrary function, and use SHAddFolderPathToLibrary function to add the paths on the hard disk into the library, that is, to use the library to manage the files under these paths. Afterwards, we stored this library created into FOLDERID_Libraries, that is, create a new library definition file under this directoryFinally, we need to release the object COM .After the above steps, we could see the library created in the file browser.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress

Close
E-mail It