void WorkWithSpecialFolder() {
// Allocate a pointer to an Item ID list
LPITEMIDLIST pidl;
// Get a pointer to an item ID list that
// represents the path of a special folder
HRESULT hr = SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl);
// Convert the item ID list's binary
// representation into a file system path
char szPath[_MAX_PATH];
BOOL f = SHGetPathFromIDList(pidl, szPath);
// Allocate a pointer to an IMalloc interface
LPMALLOC pMalloc;
// Get the address of our task allocator's IMalloc interface
hr = SHGetMalloc(&pMalloc);
// Free the item ID list allocated by SHGetSpecialFolderLocation
pMalloc->Free(pidl);
// Free our task allocator
pMalloc->Release();
// Work with the special folder's path (contained in szPath)
•
•
•
}
Figure 3 ShortPathToLongPath.cpp
/******************************************************************************
Module name: ShortPathToLongPath.cpp
Written by: Jeffrey Richter
Notices: Written 1997 by Jeffrey Richter
******************************************************************************/
#include <Windows.h>
#include <ShlObj.h>
#include <wchar.h>
///////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain (HINSTANCE hinst, HINSTANCE hintPrev, LPSTR pszCmdLine,
int nCmdShow) {
int nNumArgs = 0;
LPWSTR pszShortPathNameW = CommandLineToArgvW(GetCommandLineW(),
&nNumArgs)[1];
LPSHELLFOLDER psfDesktop = NULL;
ULONG chEaten = 0;
LPITEMIDLIST pidlShellItem = NULL;
WCHAR szLongPath[_MAX_PATH] = { 0 };
// Get the Desktop's shell folder interface
HRESULT hr = SHGetDesktopFolder(&psfDesktop);
// Request an ID list (relative to the desktop) for the short pathname
hr = psfDesktop->ParseDisplayName(NULL, NULL, pszShortPathNameW, &chEaten,
&pidlShellItem, NULL);
psfDesktop->Release(); // Release the desktop's IShellFolder
if (FAILED(hr)) {
// If we couldn't get an ID list for short pathname, it must not exist.
lstrcpyW(szLongPath, L"Error: Path not found!");
} else {
// We did get an ID list, convert it to a long pathname
SHGetPathFromIDListW(pidlShellItem, szLongPath);
// Free the ID list allocated by ParseDisplayName
LPMALLOC pMalloc = NULL;
SHGetMalloc(&pMalloc);
pMalloc->Free(pidlShellItem);
pMalloc->Release();
}
// Display short pathname in caption and the long pathname in the box.
MessageBox(NULL, szLongPath, pszShortPathNameW, MB_OK);
return(0);
}
///////////////////////////////// End of File /////////////////////////////////
Figure 4 DllMain
BOOL DllMain(HINSTANCE hinstDLL, DWORD fdwReason, BOOL fImpLoad) {
BOOL fInitializeOk = TRUE; // Assume successful initialization
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
if (fImpLoad) {
// DLL is being loaded at process load time.
// The primary thread is executing this code
// No other threads exist in this process at this time.
} else {
// DLL is being loaded because LoadLibrary was called.
// This code is being executed by any thread in the process. We
// don't know how many threads are already running in this process.
fInitializeOk = FALSE; // Fail the DLL's initialization
}
break;
case DLL_PROCESS_DETACH:
if (fImpLoad) {
// DLL is being unloaded because the process is terminating.
} else {
// DLL is being unloaded because FreeLibrary was called.
}
break;
case DLL_THREAD_ATTACH:
// Do any per-thread initialization here.
break;
case DLL_THREAD_DETACH:
// Do any per-thread cleanup here.
break;
}
return(fInitializeOk);
}