#ifndef ExploreFileH #define ExploreFileH
#include#include
#ifdef UNICODE #define ExploreFile ExploreFileW #elif _UNICODE #define ExploreFile ExploreFileW #else #define ExploreFile ExploreFileA #endif
BOOL ExploreFileA (const char* pszFile) { wchar_t wszFile [MAX_PATH + 1] = { 0}; if (!MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, pszFile, -1, wszFile, MAX_PATH)) { return FALSE; } return ExploreFileW (wszFile); }
BOOL ExploreFileW (const wchar_t* pwszFile) { LPITEMIDLIST pidl; LPCITEMIDLIST cpidl, cpidl2; IShellFolder* pDesktopFolder; ULONG ulEaten; DWORD dwAttributes; HRESULT hr; if (pwszFile == NULL) { return FALSE; } ulEaten = wcslen (pwszFile); if (FAILED (SHGetDesktopFolder (&pDesktopFolder))) { return FALSE; } hr = pDesktopFolder->ParseDisplayName ( NULL, 0, (LPOLESTR)pwszFile, &ulEaten, &pidl, &dwAttributes); if (FAILED (hr)) { pDesktopFolder->Release (); return FALSE; } cpidl2 = cpidl = pidl; CoInitialize (NULL); if (FAILED (SHOpenFolderAndSelectItems ( cpidl, 0, &cpidl2, // 一定要弄个cpidl2,否则会有内存泄露,一次4K,木有搞懂,高手帮解答。 NULL))) { pDesktopFolder->Release (); CoUninitialize (); return FALSE; } pDesktopFolder->Release (); CoUninitialize (); return TRUE; }
#endif // ExploreFileH