How To Convert a File Path to an ITEMIDLIST

Summary

When developing an application that interacts with the Windows Explorer shell, you may need to convert an arbitrary path to a file to an ITEMIDLIST. You can do this using the IShellFolder::ParseDisplayName API.

More Information

Following is an example of how to use the IShellFolder interface to convert the path to the file Readme.txt in the current directory to an ITEMIDLIST. The example is written in C. If the program is written using Visual C++, accessing member functions through the lpVtbl variable is unnecessary.

   LPITEMIDLIST  pidl;
LPSHELLFOLDER pDesktopFolder;
char szPath[MAX_PATH];
OLECHAR olePath[MAX_PATH];
ULONG chEaten;
ULONG dwAttributes;
HRESULT hr;

//
// Get the path to the file we need to convert.
//
GetCurrentDirectory(MAX_PATH, szPath);
lstrcat(szPath, "\\readme.txt");

//
// Get a pointer to the Desktop's IShellFolder interface.
//
if (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
{
//
// IShellFolder::ParseDisplayName requires the file name be in
// Unicode.
//
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szPath, -1,
olePath, MAX_PATH);

//
// Convert the path to an ITEMIDLIST.
//
hr = pDesktopFolder->lpVtbl->ParseDisplayName(pDesktopFolder,
NULL,
NULL,
olePath,
&chEaten,
&pidl,
&dwAttributes);
if (FAILED(hr))
{
// Handle error.
}

//
// pidl now contains a pointer to an ITEMIDLIST for .\readme.txt.
// This ITEMIDLIST needs to be freed using the IMalloc allocator
// returned from SHGetMalloc().
//

//release the desktop folder object
pDesktopFolder->lpVtbl->Release();
}
Lastnosti

ID članka: 132750 – Zadnji pregled: 11. jul. 2005 – Revizija: 1

Povratne informacije