Introduction to the new shell library programming interface in Windows 7

Source: Internet
Author: User

You can download the sample of Windows 7 shell library from http://cfx.codeplex.com/release/projectreleases.aspx. Contains examples of shell library operations by C ++, C #, and VB. NET.Code: Cppwin7shelllibrary, C # win7shelllibrary, and vbwin7shelllibrary.

To help users manage files on hard disks more effectively, a new file management method is introduced in Windows 7: Library ). Library naturally evolved from the concept of My Documents folder in the previous operating system. With the library, we can organize multiple related folders under the same library to manage and search for data faster and more conveniently.

 

Create a Windows Shell Library

Windows 7 provides the shcreatelibrary API to create a shell Library:

C ++ createshelllibrary

/**//*!
* Create a new shell library under the user's libraries folder. If a library
* With the same name already exists, the new one overrides the existing one.
*
* \ Param pwszlibraryname
* The Name Of The shell library to be created.
*/
Bool createshelllibrary (lpwstr pwszlibraryname)
{
/**///////////////////////////////////// /////////////////////////////////////
// Create the shell library COM object.
//

Ishelllibrary * pshelllib = NULL;
Hresult hR = shcreatelibrary (iid_ppv_args (& pshelllib ));
If (failed (HR ))
{
_ Tprintf (_ T ("shcreatelibrary failed to create the shell library ")\
_ T ("COM Object w/err 0x % 08lx \ n"), HR );
Return false;
}

/**///////////////////////////////////// ////////////////////////////////////
// Save the new library under the user's libraries folder.
//

Ishellitem * psavedto = NULL;
HR = pshelllib-> saveinknownfolder (folderid_userslibraries,
Pwszlibraryname, lsf_overrideexisting, & psavedto );
If (failed (HR ))
{
_ Tprintf (_ T ("ishelllibrary: saveinknownfolder failed to save ")\
_ T ("library w/err 0x % 08lx \ n"), HR );
Return false;
}

/**///////////////////////////////////// /////////////////////////////////////
// Clean up.
//

If (pshelllib! = NULL)
Pshelllib-> release ();

If (psavedto! = NULL)
Psavedto-> release ();

Return true;
}

/**///////////////////////////////////// /////////////////////////////////
// Create a shell library.
//

Using (shelllibrary library = new shelllibrary (libraryname, true ))
{
}

Manage Windows Shell Library

You can call the shshowmanagelibraryui API to display the Windows Standard shell library management dialog box. It is worth noting that before calling shshowmanagelibraryui, make sure that the shell library is not opened in writable mode. Otherwise, modifications to the shell library in the shshowmanagelibraryui cannot be saved.

 

C ++ showmanagelibraryui

/**//*!
* Shows the library management dialog box of the specified library, which
* Enables users to manage the library folders and default save location.
*
* \ Param pwszlibraryname
* The Name Of The shell Library
*/
Bool showmanagelibraryui (lpwstr pwszlibraryname)
{
// Get the shell item that represents the library.
Ishellitem2 * pshellitem = getshelllibraryitem (pwszlibraryname );

Hresult hR = shshowmanagelibraryui (pshellitem, null,
L "cppwin7shelllibrary", l "manage library folders and settings ",
Lmd_allowunindexablenetworklocations );

// Clean up
If (pshellitem! = NULL)
Pshellitem-> release ();

Return succeeded (HR );
}

C # showmanagelibraryui

// Showmanagelibraryui requires that the library is not currently
// Opened with write permission.
Shelllibrary. showmanagelibraryui (libraryname, intptr. Zero,
"Cswin7shelllibrary", "manage library folders and settings", true );

Add a folder to shell Library

Shaddfolderpathtolibrary can be used to add folders to the specified shell library.

C ++ addfoldertoshelllibrary

/**//*!
* Add a folder to an existing shell library.
* \ Param pshelllib
* The ishelllibrary interface of the shell library
* \ Param pwszfolderpath
* the path the folder to be added into the shell library
* \ Param bsavelocation
* If bsavelocation is true, set the folder as the Save location of the shell
* library
*/
bool addfoldertoshelllibrary (ishelllibrary * pshelllib,
lpwstr pwszfolderpath, bool bsavelocation)
{< br> hresult hR = shaddfolderpathtolibrary (pshelllib, pwszfolderpath);
If (failed (HR ))
{< br> _ tprintf (_ T ("shaddfolderpathtolibrary failed to add a folder ") \
_ T ("to the shell library w/err 0x % 08lx \ n"), HR);
return false;
}

// Save the folder as the Save location of the shell Library
If (bsavelocation)
{
// Create shell item from folder path
Ishellitem2 * pshellitemsavefolder = NULL;
HR = shcreateitemfromparsingname (pwszfolderpath, 0,
Iid_ppv_args (& pshellitemsavefolder ));
 

If (failed (HR ))
{
_ Tprintf (_ T ("shcreateitemfromparsingname failed w/Err ")\
_ T ("0x % 08lx \ n"), HR );
Return false;
}

// Set the folder as the Save location
Pshelllib-> setdefaultsavefolder (dsft_detect, pshellitemsavefolder );

If (pshellitemsavefolder! = NULL)
Pshellitemsavefolder-> release ();

If (failed (HR ))
{
_ Tprintf (_ T ("ishelllibrary: setdefaultsavefolder failed ")\
_ T ("W/err 0x % 08lx \ n"), HR );
Return false;
}
}

// Commit the change of the shell Library
Pshelllib-> commit ();

Return true;
}
C # addfoldertoshelllibrary

Using (shelllibrary library = shelllibrary. Load (libraryname, false ))
{
/**///////////////////////////////////// /////////////////////////////
// Add a folder to the shell library.
//

// Add the folder to the shell Library
Library. Add (folderpath );
Library. defaultsavefolder = folderpath;
}

Enumerate folders in Shell Library

Ishelllibrary: getfolders can be used to obtain folders in Shell library.

C ++ listfoldersinshelllibrary

/**//*!
* List all folders in the shell library.
*
* \ Param pshelllib
* The ishelllibrary interface of the shell Library
*/
Void listfoldersinshelllibrary (ishelllibrary * pshelllib)
{
Hresult hR = s_ OK;

Ishellitemarray * pshellitemarray = NULL;
Pshelllib-> getfolders (lff_allitems, iid_ppv_args (& pshellitemarray ));
If (failed (HR ))
{
_ Tprintf (_ T ("ishelllibrary: getfolders failed to get the folders ")\
_ T ("of the shell library w/err 0x % 08lx \ n"), HR );
Return;
}

DWORD dwfoldercount;
Pshellitemarray-> getcount (& dwfoldercount );

// Iterate through all folders of the shell Library
For (DWORD I = 0; I <dwfoldercount; I ++)
{
Ishellitem * pshellitem;
HR = pshellitemarray-> getitemat (I, & pshellitem );
If (failed (HR ))
Continue;

// Convert ishellitem to ishellitem2
Ishellitem2 * pshellitem2;
Pshellitem-> QueryInterface (iid_ppv_args (& pshellitem2 ));
Pshellitem-> release ();

// Fix folder path changes
Ishellitem2 * pshellitemresolvedfolder = NULL;

HR = pshelllib-> resolvefolder (pshellitem2, 5000, iid_ppv_args (
& Pshellitemresolvedfolder ));
If (succeeded (HR ))
{
// Point to the fixed folder
Pshellitem2-> release ();
Pshellitem2 = pshellitemresolvedfolder;
}
// Else we will show the unfixed folder

// Print the folder path
Lpwstr wszfolderpath;
HR = pshellitem2-> getstring (pkey_parsingpath, & wszfolderpath );
If (succeeded (HR ))
{
_ Putws (wszfolderpath );
}
Cotaskmemfree (wszfolderpath );

// Clean up
Pshellitem2-> release ();
}

Pshellitemarray-> release ();
}
C # listfoldersinshelllibrary

Using (shelllibrary library = shelllibrary. Load (libraryname, false ))
{
/**///////////////////////////////////// /////////////////////////////
// List all folders in the library.
//

Foreach (shellfolder folder in Library)
{
Console. writeline (folder );
}
}

Delete a shell Library

C ++ deleteshelllibrary

/**//*!
* Delete the shell library under the user's libraries folder according to
* Specified library name.
*
* \ Param pwszlibraryname
* The Name Of The shell library to be deleted.
*/
Bool deleteshelllibrary (lpwstr pwszlibraryname)
{
/**///////////////////////////////////// /////////////////////////////////////
// Get the shell item that represents the library and its full path.
//

Ishellitem2 * pshellitem = getshelllibraryitem (pwszlibraryname );

// Get the file-system full path of the shell item
Lpwstr wszlibraryfullpath;
Pshellitem-> getstring (pkey_parsingpath, & wszlibraryfullpath );

/**///////////////////////////////////// /////////////////////////////////////
// Delete file with the library file-system based full path.
//

Bool bsuccess = deletefilew (wszlibraryfullpath );

// Clean up
Cotaskmemfree (wszlibraryfullpath );
If (pshellitem! = NULL)
Pshellitem-> release ();

Return bsuccess;
}

C # deleteshelllibrary

/**///////////////////////////////////// /////////////////////////////////
// Delete the shell library.
//

String librariespath = path. Combine (environment. getfolderpath (
Environment. specialfolder. applicationdata ),
Shelllibrary. librariesknownfolder. relativepath );
 

String librarypath = path. Combine (librariespath, libraryname );
String libraryfullpath = path. changeextension (librarypath, "library-MS ");

File. Delete (libraryfullpath );
For more information about Windows 7 shell library, see cppwin7shelllibrary, cswin7shelllibrary, and vbwin7shelllibrary examples.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.