1 /*
   2  * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #define OEMRESOURCE
  27 
  28 #ifdef DEBUG
  29 // Warning : do not depend on anything in <awt.h>.  Including this file
  30 // is a fix for 4507525 to use the same operator new and delete as AWT.
  31 // This file should stand independent of AWT and should ultimately be
  32 // put into its own DLL.
  33 #include <awt.h>
  34 #else
  35 // Include jni_util.h first, so JNU_* macros can be redefined
  36 #include "jni_util.h"
  37 // Borrow some macros from awt.h
  38 #define JNU_NewStringPlatform(env, x) env->NewString(reinterpret_cast<jchar*>(x), static_cast<jsize>(_tcslen(x)))
  39 #define JNU_GetStringPlatformChars(env, x, y) reinterpret_cast<LPCWSTR>(env->GetStringChars(x, y))
  40 #define JNU_ReleaseStringPlatformChars(env, x, y) env->ReleaseStringChars(x, reinterpret_cast<const jchar*>(y))
  41 #endif // DEBUG
  42 
  43 #include <windows.h>
  44 #include <shlobj.h>
  45 #include <shellapi.h>
  46 #include "jlong.h"
  47 #include "alloc.h"
  48 
  49 #include "stdhdrs.h"
  50 
  51 // Copy from shlguid.h which is no longer in PlatformSDK
  52 #ifndef DEFINE_SHLGUID
  53 #define DEFINE_SHLGUID(name, l, w1, w2) DEFINE_GUID(name, l, w1, w2, 0xC0, 0, 0, 0, 0, 0, 0, 0x46)
  54 #endif
  55 
  56 // {93F2F68C-1D1B-11d3-A30E-00C04F79ABD1}
  57 DEFINE_GUID(IID_IShellFolder2, 0x93f2f68c, 0x1d1b, 0x11d3, 0xa3, 0xe, 0x0, 0xc0, 0x4f, 0x79, 0xab, 0xd1);
  58 
  59 #undef IID_IShellLinkW
  60 #undef IID_IExtractIconW
  61 // copied from shlguid.h
  62 DEFINE_SHLGUID(IID_IShellLinkW,         0x000214F9L, 0, 0);
  63 DEFINE_SHLGUID(IID_IExtractIconW,       0x000214FAL, 0, 0);
  64 
  65 //#include <sun_awt_shell_Win32ShellFolder2.h>
  66 
  67 // Shell Functions
  68 typedef BOOL (WINAPI *DestroyIconType)(HICON);
  69 typedef HINSTANCE (WINAPI *FindExecutableType)(LPCTSTR,LPCTSTR,LPTSTR);
  70 typedef HICON (WINAPI *ImageList_GetIconType)(HIMAGELIST,int,UINT);
  71 typedef BOOL (WINAPI *GetIconInfoType)(HICON,PICONINFO);
  72 typedef HRESULT (WINAPI *SHGetDesktopFolderType)(IShellFolder**);
  73 typedef DWORD* (WINAPI *SHGetFileInfoType)(LPCTSTR,DWORD,SHFILEINFO*,UINT,UINT);
  74 typedef HRESULT (WINAPI *SHGetMallocType)(IMalloc**);
  75 typedef BOOL (WINAPI *SHGetPathFromIDListType)(LPCITEMIDLIST,LPTSTR);
  76 typedef HRESULT (WINAPI *SHGetSpecialFolderLocationType)(HWND,int,LPITEMIDLIST*);
  77 
  78 static DestroyIconType fn_DestroyIcon;
  79 static FindExecutableType fn_FindExecutable;
  80 static GetIconInfoType fn_GetIconInfo;
  81 static ImageList_GetIconType fn_ImageList_GetIcon;
  82 static SHGetDesktopFolderType fn_SHGetDesktopFolder;
  83 static SHGetFileInfoType fn_SHGetFileInfo;
  84 static SHGetMallocType fn_SHGetMalloc;
  85 static SHGetPathFromIDListType fn_SHGetPathFromIDList;
  86 static SHGetSpecialFolderLocationType fn_SHGetSpecialFolderLocation;
  87 
  88 // Field IDs
  89 static jmethodID MID_pIShellFolder;
  90 static jfieldID FID_pIShellIcon;
  91 static jmethodID MID_relativePIDL;
  92 static jfieldID FID_displayName;
  93 static jfieldID FID_folderType;
  94 
  95 // Other statics
  96 static IMalloc* pMalloc;
  97 static IShellFolder* pDesktop;
  98 
  99 // Some macros from awt.h, because it is not included in release
 100 #ifndef IS_WIN2000
 101 #define IS_WIN2000 (LOBYTE(LOWORD(::GetVersion())) >= 5)
 102 #endif
 103 #ifndef IS_WINXP
 104 #define IS_WINXP ((IS_WIN2000 && HIBYTE(LOWORD(::GetVersion())) >= 1) || LOBYTE(LOWORD(::GetVersion())) > 5)
 105 #endif
 106 #ifndef IS_WINVISTA
 107 #define IS_WINVISTA (!(::GetVersion() & 0x80000000) && LOBYTE(LOWORD(::GetVersion())) >= 6)
 108 #endif
 109 
 110 
 111 extern "C" {
 112 
 113 static BOOL initShellProcs()
 114 {
 115     static HMODULE libShell32 = NULL;
 116     static HMODULE libUser32 = NULL;
 117     static HMODULE libComCtl32 = NULL;
 118     // If already initialized, return TRUE
 119     if (libShell32 != NULL && libUser32 != NULL) {
 120         return TRUE;
 121     }
 122     // Load libraries
 123     libShell32 = JDK_LoadSystemLibrary("shell32.dll");
 124     if (libShell32 == NULL) {
 125         return FALSE;
 126     }
 127     libUser32 = JDK_LoadSystemLibrary("user32.dll");
 128     if (libUser32 == NULL) {
 129         return FALSE;
 130     }
 131     libComCtl32 = JDK_LoadSystemLibrary("comctl32.dll");
 132     if (libComCtl32 == NULL) {
 133         return FALSE;
 134     }
 135 
 136     // Set up procs - libComCtl32
 137     fn_ImageList_GetIcon = (ImageList_GetIconType)GetProcAddress(libComCtl32, "ImageList_GetIcon");
 138     if (fn_ImageList_GetIcon == NULL) {
 139         return FALSE;
 140     }
 141 
 142     // Set up procs - libShell32
 143         fn_FindExecutable = (FindExecutableType)GetProcAddress(
 144                 libShell32, "FindExecutableW");
 145     if (fn_FindExecutable == NULL) {
 146         return FALSE;
 147     }
 148         fn_SHGetDesktopFolder = (SHGetDesktopFolderType)GetProcAddress(libShell32,
 149                 "SHGetDesktopFolder");
 150     if (fn_SHGetDesktopFolder == NULL) {
 151         return FALSE;
 152     }
 153         fn_SHGetFileInfo = (SHGetFileInfoType)GetProcAddress(
 154                 libShell32, "SHGetFileInfoW");
 155     if (fn_SHGetFileInfo == NULL) {
 156         return FALSE;
 157     }
 158         fn_SHGetMalloc = (SHGetMallocType)GetProcAddress(libShell32,
 159         "SHGetMalloc");
 160     if (fn_SHGetMalloc == NULL) {
 161         return FALSE;
 162     }
 163     // Set up IMalloc
 164     if (fn_SHGetMalloc(&pMalloc) != S_OK) {
 165         return FALSE;
 166     }
 167         fn_SHGetPathFromIDList = (SHGetPathFromIDListType)GetProcAddress(
 168                 libShell32, "SHGetPathFromIDListW");
 169     if (fn_SHGetPathFromIDList == NULL) {
 170         return FALSE;
 171     }
 172         fn_SHGetSpecialFolderLocation = (SHGetSpecialFolderLocationType)
 173         GetProcAddress(libShell32, "SHGetSpecialFolderLocation");
 174     if (fn_SHGetSpecialFolderLocation == NULL) {
 175         return FALSE;
 176     }
 177 
 178     // Set up procs - libUser32
 179     fn_GetIconInfo = (GetIconInfoType)GetProcAddress(libUser32, "GetIconInfo");
 180     if (fn_GetIconInfo == NULL) {
 181         return FALSE;
 182     }
 183     fn_DestroyIcon = (DestroyIconType)GetProcAddress(libUser32, "DestroyIcon");
 184     if (fn_DestroyIcon == NULL) {
 185         return FALSE;
 186     }
 187     return TRUE;
 188 }
 189 
 190 // To call real JNU_NewStringPlatform
 191 #undef JNU_NewStringPlatform
 192 static jstring jstringFromSTRRET(JNIEnv* env, LPITEMIDLIST pidl, STRRET* pStrret) {
 193     switch (pStrret->uType) {
 194         case STRRET_CSTR :
 195             return JNU_NewStringPlatform(env, reinterpret_cast<const char*>(pStrret->cStr));
 196         case STRRET_OFFSET :
 197             // Note : this may need to be WCHAR instead
 198             return JNU_NewStringPlatform(env,
 199                                          (CHAR*)pidl + pStrret->uOffset);
 200         case STRRET_WSTR :
 201             return env->NewString(reinterpret_cast<const jchar*>(pStrret->pOleStr),
 202                 static_cast<jsize>(wcslen(pStrret->pOleStr)));
 203     }
 204     return NULL;
 205 }
 206 // restoring the original definition
 207 #define JNU_NewStringPlatform(env, x) env->NewString(reinterpret_cast<jchar*>(x), static_cast<jsize>(_tcslen(x)))
 208 
 209 /*
 210  * Class:     sun_awt_shell_Win32ShellFolder2
 211  * Method:    initIDs
 212  * Signature: ()V
 213  */
 214 JNIEXPORT void JNICALL Java_sun_awt_shell_Win32ShellFolder2_initIDs
 215     (JNIEnv* env, jclass cls)
 216 {
 217     if (!initShellProcs()) {
 218         JNU_ThrowInternalError(env, "Could not initialize shell library");
 219         return;
 220     }
 221     MID_pIShellFolder = env->GetMethodID(cls, "setIShellFolder", "(J)V");
 222     CHECK_NULL(MID_pIShellFolder);
 223     FID_pIShellIcon = env->GetFieldID(cls, "pIShellIcon", "J");
 224     CHECK_NULL(FID_pIShellIcon);
 225     MID_relativePIDL = env->GetMethodID(cls, "setRelativePIDL", "(J)V");
 226     CHECK_NULL(MID_relativePIDL);
 227     FID_displayName = env->GetFieldID(cls, "displayName", "Ljava/lang/String;");
 228     CHECK_NULL(FID_displayName);
 229     FID_folderType = env->GetFieldID(cls, "folderType", "Ljava/lang/String;");
 230     CHECK_NULL(FID_folderType);
 231 }
 232 
 233 
 234 /*
 235 * Class:     sun_awt_shell_Win32ShellFolderManager2
 236 * Method:    initializeCom
 237 * Signature: ()V
 238 */
 239 JNIEXPORT void JNICALL Java_sun_awt_shell_Win32ShellFolderManager2_initializeCom
 240         (JNIEnv* env, jclass cls)
 241 {
 242     HRESULT hr = ::CoInitialize(NULL);
 243     if (FAILED(hr)) {
 244         char c[64];
 245         sprintf(c, "Could not initialize COM: HRESULT=0x%08X", hr);
 246         JNU_ThrowInternalError(env, c);
 247     }
 248 }
 249 
 250 /*
 251 * Class:     sun_awt_shell_Win32ShellFolderManager2
 252 * Method:    uninitializeCom
 253 * Signature: ()V
 254 */
 255 JNIEXPORT void JNICALL Java_sun_awt_shell_Win32ShellFolderManager2_uninitializeCom
 256         (JNIEnv* env, jclass cls)
 257 {
 258     ::CoUninitialize();
 259 }
 260 
 261 static IShellIcon* getIShellIcon(IShellFolder* pIShellFolder) {
 262     // http://msdn.microsoft.com/library/en-us/shellcc/platform/Shell/programmersguide/shell_int/shell_int_programming/std_ifaces.asp
 263     HRESULT hres;
 264     IShellIcon* pIShellIcon;
 265     if (pIShellFolder != NULL) {
 266         hres = pIShellFolder->QueryInterface(IID_IShellIcon, (void**)&pIShellIcon);
 267         if (SUCCEEDED(hres)) {
 268             return pIShellIcon;
 269         }
 270     }
 271     return (IShellIcon*)NULL;
 272 }
 273 
 274 
 275 /*
 276  * Class:     sun_awt_shell_Win32ShellFolder2
 277  * Method:    getIShellIcon
 278  * Signature: (J)J
 279  */
 280 JNIEXPORT jlong JNICALL Java_sun_awt_shell_Win32ShellFolder2_getIShellIcon
 281     (JNIEnv* env, jclass cls, jlong parentIShellFolder)
 282 {
 283     return (jlong)getIShellIcon((IShellFolder*)parentIShellFolder);
 284 }
 285 
 286 
 287 /*
 288  * Class:     sun_awt_shell_Win32ShellFolder2
 289  * Method:    initDesktop
 290  * Signature: ()V
 291  */
 292 JNIEXPORT void JNICALL Java_sun_awt_shell_Win32ShellFolder2_initDesktop
 293     (JNIEnv* env, jobject desktop)
 294 {
 295     // Get desktop IShellFolder
 296     HRESULT res = fn_SHGetDesktopFolder(&pDesktop);
 297     if (res != S_OK) {
 298         JNU_ThrowInternalError(env, "Could not get desktop shell folder");
 299         return;
 300     }
 301     // Set field ID for pIShellFolder
 302     env->CallVoidMethod(desktop, MID_pIShellFolder, (jlong)pDesktop);
 303     // Get desktop relative PIDL
 304     LPITEMIDLIST relPIDL;
 305     res = fn_SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &relPIDL);
 306     if (res != S_OK) {
 307         JNU_ThrowInternalError(env,
 308             "Could not get desktop shell folder ID list");
 309         return;
 310     }
 311     // Set field ID for relative PIDL
 312     env->CallVoidMethod(desktop, MID_relativePIDL, (jlong)relPIDL);
 313 }
 314 
 315 /*
 316  * Class:     sun_awt_shell_Win32ShellFolder2
 317  * Method:    initSpecial
 318  * Signature: (JI)V
 319  */
 320 JNIEXPORT void JNICALL Java_sun_awt_shell_Win32ShellFolder2_initSpecial
 321     (JNIEnv* env, jobject folder, jlong desktopIShellFolder, jint folderType)
 322 {
 323     // Get desktop IShellFolder interface
 324     IShellFolder* pDesktop = (IShellFolder*)desktopIShellFolder;
 325     if (pDesktop == NULL) {
 326         JNU_ThrowInternalError(env, "Desktop shell folder missing");
 327         return;
 328     }
 329     // Get special folder relative PIDL
 330     LPITEMIDLIST relPIDL;
 331     HRESULT res = fn_SHGetSpecialFolderLocation(NULL, folderType,
 332         &relPIDL);
 333     if (res != S_OK) {
 334         JNU_ThrowIOException(env, "Could not get shell folder ID list");
 335         return;
 336     }
 337     // Set field ID for relative PIDL
 338     env->CallVoidMethod(folder, MID_relativePIDL, (jlong)relPIDL);
 339     // Get special folder IShellFolder interface
 340     IShellFolder* pFolder;
 341     res = pDesktop->BindToObject(relPIDL, NULL, IID_IShellFolder,
 342         (void**)&pFolder);
 343     if (res != S_OK) {
 344         JNU_ThrowInternalError(env,
 345             "Could not bind shell folder to interface");
 346         return;
 347     }
 348     // Set field ID for pIShellFolder
 349     env->CallVoidMethod(folder, MID_pIShellFolder, (jlong)pFolder);
 350 }
 351 
 352 
 353 /*
 354  * Class:     sun_awt_shell_Win32ShellFolder2
 355  * Method:    getNextPIDLEntry
 356  * Signature: (J)J
 357  */
 358 JNIEXPORT jlong JNICALL Java_sun_awt_shell_Win32ShellFolder2_getNextPIDLEntry
 359     (JNIEnv* env, jclass cls, jlong jpIDL)
 360 {
 361     LPITEMIDLIST pIDL = (LPITEMIDLIST)jpIDL;
 362 
 363     // Check for valid pIDL.
 364     if(pIDL == NULL)
 365         return NULL;
 366 
 367     // Get the size of the specified item identifier.
 368     int cb = pIDL->mkid.cb;
 369 
 370     // If the size is zero, it is the end of the list.
 371     if (cb == 0)
 372         return NULL;
 373 
 374     // Add cb to pidl (casting to increment by bytes).
 375     pIDL = (LPITEMIDLIST)(((LPBYTE)pIDL) + cb);
 376 
 377     // Return NULL if it is null-terminating, or a pidl otherwise.
 378     return (pIDL->mkid.cb == 0) ? 0 : (jlong)pIDL;
 379 }
 380 
 381 
 382 /*
 383  * Class:     sun_awt_shell_Win32ShellFolder2
 384  * Method:    copyFirstPIDLEntry
 385  * Signature: (J)J
 386  */
 387 JNIEXPORT jlong JNICALL Java_sun_awt_shell_Win32ShellFolder2_copyFirstPIDLEntry
 388     (JNIEnv* env, jclass cls, jlong jpIDL)
 389 {
 390     LPITEMIDLIST pIDL = (LPITEMIDLIST)jpIDL;
 391     if (pIDL == NULL) {
 392         return 0;
 393     }
 394     // Get the size of the specified item identifier.
 395     int cb = pIDL->mkid.cb;
 396 
 397     // If the size is zero, it is the end of the list.
 398     if (cb == 0)
 399         return 0;
 400 
 401     if (!IS_SAFE_SIZE_ADD(cb, sizeof(SHITEMID))) {
 402         return 0;
 403     }
 404     // Allocate space for this as well as null-terminating entry.
 405     LPITEMIDLIST newPIDL = (LPITEMIDLIST)pMalloc->Alloc(cb + sizeof(SHITEMID));
 406 
 407     // Copy data.
 408     memcpy(newPIDL, pIDL, cb);
 409 
 410     // Set null terminator for next entry.
 411     LPITEMIDLIST nextPIDL = (LPITEMIDLIST)(((LPBYTE)newPIDL) + cb);
 412     nextPIDL->mkid.cb = 0;
 413 
 414     return (jlong)newPIDL;
 415 }
 416 
 417 static int pidlLength(LPITEMIDLIST pIDL) {
 418     int len = 0;
 419     while (pIDL->mkid.cb != 0) {
 420         int cb = pIDL->mkid.cb;
 421         len += cb;
 422         pIDL = (LPITEMIDLIST)(((LPBYTE)pIDL) + cb);
 423     }
 424     return len;
 425 }
 426 
 427 /*
 428  * Class:     sun_awt_shell_Win32ShellFolder2
 429  * Method:    combinePIDLs
 430  * Signature: (J)J
 431  */
 432 JNIEXPORT jlong JNICALL Java_sun_awt_shell_Win32ShellFolder2_combinePIDLs
 433     (JNIEnv* env, jclass cls, jlong jppIDL, jlong jpIDL)
 434 {
 435     // Combine an absolute (fully qualified) pidl in a parent with the relative
 436     // pidl of a child object to create a new absolute pidl for the child.
 437 
 438     LPITEMIDLIST parentPIDL   = (LPITEMIDLIST)jppIDL;
 439     LPITEMIDLIST relativePIDL = (LPITEMIDLIST)jpIDL;
 440 
 441     int len1 = pidlLength(parentPIDL);
 442     int len2 = pidlLength(relativePIDL);
 443 
 444     if (!IS_SAFE_SIZE_ADD(len1, len2) || !IS_SAFE_SIZE_ADD(len1 + len2, sizeof(SHITEMID))) {
 445         return 0;
 446     }
 447     LPITEMIDLIST newPIDL = (LPITEMIDLIST)pMalloc->Alloc(len1 + len2 + sizeof(SHITEMID));
 448     memcpy(newPIDL, parentPIDL, len1);
 449     memcpy(((LPBYTE) newPIDL) + len1, relativePIDL, len2);
 450     LPITEMIDLIST nullTerminator = (LPITEMIDLIST)(((LPBYTE) newPIDL) + len1 + len2);
 451     nullTerminator->mkid.cb = 0;
 452 
 453     return (jlong) newPIDL;
 454 }
 455 
 456 
 457 /*
 458  * Class:     sun_awt_shell_Win32ShellFolder2
 459  * Method:    releasePIDL
 460  * Signature: (J)V
 461  */
 462 JNIEXPORT void JNICALL Java_sun_awt_shell_Win32ShellFolder2_releasePIDL
 463     (JNIEnv* env, jclass cls, jlong pIDL)
 464 {
 465     if (pIDL != 0L) {
 466         pMalloc->Free((LPITEMIDLIST)pIDL);
 467     }
 468 }
 469 
 470 
 471 /*
 472  * Class:     sun_awt_shell_Win32ShellFolder2
 473  * Method:    releaseIShellFolder
 474  * Signature: (J)V
 475  */
 476 JNIEXPORT void JNICALL Java_sun_awt_shell_Win32ShellFolder2_releaseIShellFolder
 477     (JNIEnv* env, jclass cls, jlong pIShellFolder)
 478 {
 479     if (pIShellFolder != 0L) {
 480         ((IShellFolder*)pIShellFolder)->Release();
 481     }
 482 }
 483 
 484 
 485 /*
 486  * Class:     sun_awt_shell_Win32ShellFolder2
 487  * Method:    compareIDs
 488  * Signature: (JJJ)I
 489  */
 490 JNIEXPORT jint JNICALL Java_sun_awt_shell_Win32ShellFolder2_compareIDs
 491     (JNIEnv* env, jclass cls, jlong jpParentIShellFolder, jlong pIDL1, jlong pIDL2)
 492 {
 493     IShellFolder* pParentIShellFolder = (IShellFolder*)jpParentIShellFolder;
 494     if (pParentIShellFolder == NULL) {
 495         return 0;
 496     }
 497     return pParentIShellFolder->CompareIDs(0, (LPCITEMIDLIST) pIDL1, (LPCITEMIDLIST) pIDL2);
 498 }
 499 
 500 
 501 /*
 502  * Class:     sun_awt_shell_Win32ShellFolder2
 503  * Method:    getAttributes0
 504  * Signature: (JJI)J
 505  */
 506 JNIEXPORT jint JNICALL Java_sun_awt_shell_Win32ShellFolder2_getAttributes0
 507     (JNIEnv* env, jclass cls, jlong jpParentIShellFolder, jlong jpIDL, jint attrsMask)
 508 {
 509     IShellFolder* pParentIShellFolder = (IShellFolder*)jpParentIShellFolder;
 510     if (pParentIShellFolder == NULL) {
 511         return 0;
 512     }
 513     LPCITEMIDLIST pIDL = (LPCITEMIDLIST)jpIDL;
 514     if (pIDL == NULL) {
 515         return 0;
 516     }
 517     ULONG attrs = attrsMask;
 518     HRESULT res = pParentIShellFolder->GetAttributesOf(1, &pIDL, &attrs);
 519     return attrs;
 520 }
 521 
 522 
 523 /*
 524  * Class:     sun_awt_shell_Win32ShellFolder2
 525  * Method:    getFileSystemPath0
 526  * Signature: (I)Ljava/lang/String;
 527  */
 528 JNIEXPORT jstring JNICALL Java_sun_awt_shell_Win32ShellFolder2_getFileSystemPath0
 529     (JNIEnv* env, jclass cls, jint csidl)
 530 {
 531     LPITEMIDLIST relPIDL;
 532     TCHAR szBuf[MAX_PATH];
 533     HRESULT res = fn_SHGetSpecialFolderLocation(NULL, csidl, &relPIDL);
 534     if (res != S_OK) {
 535         JNU_ThrowIOException(env, "Could not get shell folder ID list");
 536         return NULL;
 537     }
 538     if (fn_SHGetPathFromIDList(relPIDL, szBuf)) {
 539         return JNU_NewStringPlatform(env, szBuf);
 540     } else {
 541         return NULL;
 542     }
 543 }
 544 
 545 /*
 546  * Class:     sun_awt_shell_Win32ShellFolder2
 547  * Method:    getEnumObjects
 548  * Signature: (JZ)J
 549  */
 550 JNIEXPORT jlong JNICALL Java_sun_awt_shell_Win32ShellFolder2_getEnumObjects
 551     (JNIEnv* env, jobject folder, jlong pIShellFolder,
 552      jboolean isDesktop, jboolean includeHiddenFiles)
 553 {
 554     IShellFolder* pFolder = (IShellFolder*)pIShellFolder;
 555     if (pFolder == NULL) {
 556         return 0;
 557     }
 558     DWORD dwFlags = SHCONTF_FOLDERS | SHCONTF_NONFOLDERS;
 559     if (includeHiddenFiles) {
 560         dwFlags |= SHCONTF_INCLUDEHIDDEN;
 561     }
 562         /*
 563     if (!isDesktop) {
 564         dwFlags = dwFlags | SHCONTF_NONFOLDERS;
 565     }
 566         */
 567     IEnumIDList* pEnum;
 568     if (pFolder->EnumObjects(NULL, dwFlags, &pEnum) != S_OK) {
 569         return 0;
 570     }
 571     return (jlong)pEnum;
 572 }
 573 
 574 /*
 575  * Class:     sun_awt_shell_Win32ShellFolder2
 576  * Method:    getNextChild
 577  * Signature: (J)J
 578  */
 579 JNIEXPORT jlong JNICALL Java_sun_awt_shell_Win32ShellFolder2_getNextChild
 580     (JNIEnv* env, jobject folder, jlong pEnumObjects)
 581 {
 582     IEnumIDList* pEnum = (IEnumIDList*)pEnumObjects;
 583     if (pEnum == NULL) {
 584         return 0;
 585     }
 586     LPITEMIDLIST pidl;
 587     if (pEnum->Next(1, &pidl, NULL) != S_OK) {
 588         return 0;
 589     }
 590     return (jlong)pidl;
 591 }
 592 
 593 /*
 594  * Class:     sun_awt_shell_Win32ShellFolder2
 595  * Method:    releaseEnumObjects
 596  * Signature: (J)V
 597  */
 598 JNIEXPORT void JNICALL Java_sun_awt_shell_Win32ShellFolder2_releaseEnumObjects
 599     (JNIEnv* env, jobject folder, jlong pEnumObjects)
 600 {
 601     IEnumIDList* pEnum = (IEnumIDList*)pEnumObjects;
 602     if (pEnum == NULL) {
 603         return;
 604     }
 605     pEnum->Release();
 606 }
 607 
 608 /*
 609  * Class:     sun_awt_shell_Win32ShellFolder2
 610  * Method:    bindToObject
 611  * Signature: (JJ)J
 612  */
 613 JNIEXPORT jlong JNICALL Java_sun_awt_shell_Win32ShellFolder2_bindToObject
 614     (JNIEnv* env, jclass cls, jlong parentIShellFolder, jlong relativePIDL)
 615 {
 616     IShellFolder* pParent = (IShellFolder*)parentIShellFolder;
 617     if (pParent == NULL) {
 618         return 0;
 619     }
 620     LPITEMIDLIST pidl = (LPITEMIDLIST)relativePIDL;
 621     if (pidl == NULL) {
 622         return 0;
 623     }
 624     IShellFolder* pFolder;
 625     HRESULT hr = pParent->BindToObject(pidl, NULL, IID_IShellFolder, (void**)&pFolder);
 626     if (SUCCEEDED (hr)) {
 627         return (jlong)pFolder;
 628     }
 629     return 0;
 630 }
 631 
 632 
 633 /*
 634  * Class:     sun_awt_shell_Win32ShellFolder2
 635  * Method:    getLinkLocation
 636  * Signature: (JJZ)J;
 637  */
 638 JNIEXPORT jlong JNICALL Java_sun_awt_shell_Win32ShellFolder2_getLinkLocation
 639     (JNIEnv* env, jclass cls, jlong parentIShellFolder, jlong relativePIDL, jboolean resolve)
 640 {
 641     HRESULT hres;
 642     STRRET strret;
 643     OLECHAR olePath[MAX_PATH]; // wide-char version of path name
 644     LPWSTR wstr;
 645 
 646     IShellFolder* pParent = (IShellFolder*)parentIShellFolder;
 647     if (pParent == NULL) {
 648         return NULL;
 649     }
 650 
 651     LPITEMIDLIST pidl = (LPITEMIDLIST)relativePIDL;
 652     if (pidl == NULL) {
 653         return NULL;
 654     }
 655 
 656     hres = pParent->GetDisplayNameOf(pidl, SHGDN_NORMAL | SHGDN_FORPARSING, &strret);
 657     if (FAILED(hres)) {
 658         return NULL;
 659     }
 660 
 661     switch (strret.uType) {
 662       case STRRET_CSTR :
 663         // IShellFolder::ParseDisplayName requires the path name in Unicode.
 664         MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, strret.cStr, -1, olePath, MAX_PATH);
 665         wstr = olePath;
 666         break;
 667 
 668       case STRRET_OFFSET :
 669         MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (CHAR *)pidl + strret.uOffset, -1, olePath, MAX_PATH);
 670         wstr = olePath;
 671         break;
 672 
 673       case STRRET_WSTR :
 674         wstr = strret.pOleStr;
 675         break;
 676     }
 677 
 678     IShellLinkW* psl;
 679     hres = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (LPVOID *)&psl);
 680     if (SUCCEEDED(hres)) {
 681         IPersistFile* ppf;
 682         hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
 683         if (SUCCEEDED(hres)) {
 684             hres = ppf->Load(wstr, STGM_READ);
 685             if (SUCCEEDED(hres)) {
 686                 if (resolve) {
 687                     hres = psl->Resolve(NULL, 0);
 688                     // Ignore failure
 689                 }
 690                 pidl = (LPITEMIDLIST)NULL;
 691                 hres = psl->GetIDList(&pidl);
 692             }
 693             ppf->Release();
 694         }
 695         psl->Release();
 696     }
 697 
 698     if (strret.uType == STRRET_WSTR) {
 699         CoTaskMemFree(strret.pOleStr);
 700     }
 701     if (SUCCEEDED(hres)) {
 702         return (jlong)pidl;
 703     } else {
 704         return 0;
 705     }
 706 }
 707 
 708 
 709 /*
 710  * Class:     sun_awt_shell_Win32ShellFolder2
 711  * Method:    parseDisplayName0
 712  * Signature: (JLjava/lang/String;)J
 713  */
 714 JNIEXPORT jlong JNICALL Java_sun_awt_shell_Win32ShellFolder2_parseDisplayName0
 715     (JNIEnv* env, jclass cls, jlong jpIShellFolder, jstring jname)
 716 {
 717 
 718     // Get desktop IShellFolder interface
 719     IShellFolder* pIShellFolder = (IShellFolder*)jpIShellFolder;
 720     if (pIShellFolder == NULL) {
 721         JNU_ThrowInternalError(env, "Desktop shell folder missing");
 722         return 0;
 723     }
 724     // Get relative PIDL for name
 725     LPITEMIDLIST pIDL;
 726     int nLength = env->GetStringLength(jname);
 727     const jchar* strPath = env->GetStringChars(jname, NULL);
 728     JNU_CHECK_EXCEPTION_RETURN(env, 0);
 729     jchar* wszPath = new jchar[nLength + 1];
 730     wcsncpy(reinterpret_cast<LPWSTR>(wszPath), reinterpret_cast<LPCWSTR>(strPath), nLength);
 731     wszPath[nLength] = 0;
 732     HRESULT res = pIShellFolder->ParseDisplayName(NULL, NULL,
 733                         reinterpret_cast<LPWSTR>(wszPath), NULL, &pIDL, NULL);
 734     if (res != S_OK) {
 735         JNU_ThrowIOException(env, "Could not parse name");
 736         pIDL = 0;
 737     }
 738     delete[] wszPath;
 739     env->ReleaseStringChars(jname, strPath);
 740     return (jlong)pIDL;
 741 }
 742 
 743 
 744 /*
 745  * Class:     sun_awt_shell_Win32ShellFolder2
 746  * Method:    getDisplayNameOf
 747  * Signature: (JJI)Ljava/lang/String;
 748  */
 749 JNIEXPORT jstring JNICALL Java_sun_awt_shell_Win32ShellFolder2_getDisplayNameOf
 750     (JNIEnv* env, jclass cls, jlong parentIShellFolder, jlong relativePIDL, jint attrs)
 751 {
 752     IShellFolder* pParent = (IShellFolder*)parentIShellFolder;
 753     if (pParent == NULL) {
 754         return NULL;
 755     }
 756     LPITEMIDLIST pidl = (LPITEMIDLIST)relativePIDL;
 757     if (pidl == NULL) {
 758         return NULL;
 759     }
 760     STRRET strret;
 761     if (pParent->GetDisplayNameOf(pidl, attrs, &strret) != S_OK) {
 762         return NULL;
 763     }
 764     jstring result = jstringFromSTRRET(env, pidl, &strret);
 765     if (strret.uType == STRRET_WSTR) {
 766         CoTaskMemFree(strret.pOleStr);
 767     }
 768     return result;
 769 }
 770 
 771 /*
 772  * Class:     sun_awt_shell_Win32ShellFolder2
 773  * Method:    getFolderType
 774  * Signature: (J)Ljava/lang/String;
 775  */
 776 JNIEXPORT jstring JNICALL Java_sun_awt_shell_Win32ShellFolder2_getFolderType
 777     (JNIEnv* env, jclass cls, jlong pIDL)
 778 {
 779     SHFILEINFO fileInfo;
 780     if (fn_SHGetFileInfo((LPCTSTR)pIDL, 0L, &fileInfo, sizeof(fileInfo),
 781         SHGFI_TYPENAME | SHGFI_PIDL) == 0) {
 782         return NULL;
 783     }
 784     return JNU_NewStringPlatform(env, fileInfo.szTypeName);
 785 }
 786 
 787 /*
 788  * Class:     sun_awt_shell_Win32ShellFolder2
 789  * Method:    getExecutableType
 790  * Signature: (Ljava/lang/String;)Ljava/lang/String;
 791  */
 792 JNIEXPORT jstring JNICALL Java_sun_awt_shell_Win32ShellFolder2_getExecutableType
 793     (JNIEnv* env, jobject folder, jstring path)
 794 {
 795     TCHAR szBuf[MAX_PATH];
 796     LPCTSTR szPath = JNU_GetStringPlatformChars(env, path, NULL);
 797     if (szPath == NULL) {
 798         return NULL;
 799     }
 800     HINSTANCE res = fn_FindExecutable(szPath, szPath, szBuf);
 801     JNU_ReleaseStringPlatformChars(env, path, szPath);
 802     if ((UINT_PTR)res < 32) {
 803         return NULL;
 804     }
 805     return JNU_NewStringPlatform(env, szBuf);
 806 }
 807 
 808 
 809 /*
 810  * Class:     sun_awt_shell_Win32ShellFolder2
 811  * Method:    getIcon
 812  * Signature: (Ljava/lang/String;Z)J
 813  */
 814 JNIEXPORT jlong JNICALL Java_sun_awt_shell_Win32ShellFolder2_getIcon
 815     (JNIEnv* env, jclass cls, jstring absolutePath, jboolean getLargeIcon)
 816 {
 817     HICON hIcon = NULL;
 818     SHFILEINFO fileInfo;
 819     LPCTSTR pathStr = JNU_GetStringPlatformChars(env, absolutePath, NULL);
 820     JNU_CHECK_EXCEPTION_RETURN(env, 0);
 821     if (fn_SHGetFileInfo(pathStr, 0L, &fileInfo, sizeof(fileInfo),
 822                          SHGFI_ICON | (getLargeIcon ? 0 : SHGFI_SMALLICON)) != 0) {
 823         hIcon = fileInfo.hIcon;
 824     }
 825     JNU_ReleaseStringPlatformChars(env, absolutePath, pathStr);
 826     return (jlong)hIcon;
 827 }
 828 
 829 /*
 830  * Class:     sun_awt_shell_Win32ShellFolder2
 831  * Method:    getIconIndex
 832  * Signature: (JJ)I
 833  */
 834 JNIEXPORT jint JNICALL Java_sun_awt_shell_Win32ShellFolder2_getIconIndex
 835     (JNIEnv* env, jclass cls, jlong pIShellIconL, jlong relativePIDL)
 836 {
 837     IShellIcon* pIShellIcon = (IShellIcon*)pIShellIconL;
 838     LPITEMIDLIST pidl = (LPITEMIDLIST)relativePIDL;
 839     if (pIShellIcon == NULL && pidl == NULL) {
 840         return 0;
 841     }
 842 
 843     INT index = -1;
 844 
 845     HRESULT hres;
 846     // http://msdn.microsoft.com/library/en-us/shellcc/platform/Shell/programmersguide/shell_int/shell_int_programming/std_ifaces.asp
 847     if (pIShellIcon != NULL) {
 848         hres = pIShellIcon->GetIconOf(pidl, GIL_FORSHELL, &index);
 849     }
 850 
 851     return (jint)index;
 852 }
 853 
 854 
 855 /*
 856  * Class:     sun_awt_shell_Win32ShellFolder2
 857  * Method:    extractIcon
 858  * Signature: (JJZ)J
 859  */
 860 JNIEXPORT jlong JNICALL Java_sun_awt_shell_Win32ShellFolder2_extractIcon
 861     (JNIEnv* env, jclass cls, jlong pIShellFolderL, jlong relativePIDL, jboolean getLargeIcon)
 862 {
 863     IShellFolder* pIShellFolder = (IShellFolder*)pIShellFolderL;
 864     LPITEMIDLIST pidl = (LPITEMIDLIST)relativePIDL;
 865     if (pIShellFolder == NULL || pidl == NULL) {
 866         return 0;
 867     }
 868 
 869     HICON hIcon = NULL;
 870 
 871     HRESULT hres;
 872     IExtractIconW* pIcon;
 873     hres = pIShellFolder->GetUIObjectOf(NULL, 1, const_cast<LPCITEMIDLIST*>(&pidl),
 874                                         IID_IExtractIconW, NULL, (void**)&pIcon);
 875     if (SUCCEEDED(hres)) {
 876         WCHAR szBuf[MAX_PATH];
 877         INT index;
 878         UINT flags;
 879         hres = pIcon->GetIconLocation(GIL_FORSHELL, szBuf, MAX_PATH, &index, &flags);
 880         if (SUCCEEDED(hres)) {
 881             HICON hIconLarge;
 882             hres = pIcon->Extract(szBuf, index, &hIconLarge, &hIcon, (16 << 16) + 32);
 883             if (SUCCEEDED(hres)) {
 884                 if (getLargeIcon) {
 885                     fn_DestroyIcon((HICON)hIcon);
 886                     hIcon = hIconLarge;
 887                 } else {
 888                     fn_DestroyIcon((HICON)hIconLarge);
 889                 }
 890             }
 891         }
 892         pIcon->Release();
 893     }
 894     return (jlong)hIcon;
 895 }
 896 
 897 
 898 /*
 899  * Class:     sun_awt_shell_Win32ShellFolder2
 900  * Method:    disposeIcon
 901  * Signature: (J)V
 902  */
 903 JNIEXPORT void JNICALL Java_sun_awt_shell_Win32ShellFolder2_disposeIcon
 904     (JNIEnv* env, jclass cls, jlong hicon)
 905 {
 906     fn_DestroyIcon((HICON)hicon);
 907 }
 908 
 909 /*
 910  * Class:     sun_awt_shell_Win32ShellFolder2
 911  * Method:    getIconBits
 912  * Signature: (JI)[I
 913  */
 914 JNIEXPORT jintArray JNICALL Java_sun_awt_shell_Win32ShellFolder2_getIconBits
 915     (JNIEnv* env, jclass cls, jlong hicon, jint iconSize)
 916 {
 917     jintArray iconBits = NULL;
 918 
 919     // Get the icon info
 920     ICONINFO iconInfo;
 921     if (fn_GetIconInfo((HICON)hicon, &iconInfo)) {
 922         // Get the screen DC
 923         HDC dc = GetDC(NULL);
 924         if (dc != NULL) {
 925             // Set up BITMAPINFO
 926             BITMAPINFO bmi;
 927             memset(&bmi, 0, sizeof(BITMAPINFO));
 928             bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
 929             bmi.bmiHeader.biWidth = iconSize;
 930             bmi.bmiHeader.biHeight = -iconSize;
 931             bmi.bmiHeader.biPlanes = 1;
 932             bmi.bmiHeader.biBitCount = 32;
 933             bmi.bmiHeader.biCompression = BI_RGB;
 934             // Extract the color bitmap
 935             int nBits = iconSize * iconSize;
 936             long colorBits[1024];
 937             GetDIBits(dc, iconInfo.hbmColor, 0, iconSize, colorBits, &bmi, DIB_RGB_COLORS);
 938             // XP supports alpha in some icons, and depending on device.
 939             // This should take precedence over the icon mask bits.
 940             BOOL hasAlpha = FALSE;
 941             if (IS_WINXP) {
 942                 for (int i = 0; i < nBits; i++) {
 943                     if ((colorBits[i] & 0xff000000) != 0) {
 944                         hasAlpha = TRUE;
 945                         break;
 946                     }
 947                 }
 948             }
 949             if (!hasAlpha) {
 950                 // Extract the mask bitmap
 951                 long maskBits[1024];
 952                 GetDIBits(dc, iconInfo.hbmMask, 0, iconSize, maskBits, &bmi, DIB_RGB_COLORS);
 953                 // Copy the mask alphas into the color bits
 954                 for (int i = 0; i < nBits; i++) {
 955                     if (maskBits[i] == 0) {
 956                         colorBits[i] |= 0xff000000;
 957                     }
 958                 }
 959             }
 960             // Release DC
 961             ReleaseDC(NULL, dc);
 962             // Create java array
 963             iconBits = env->NewIntArray(nBits);
 964             if (!(env->ExceptionCheck())) {
 965             // Copy values to java array
 966             env->SetIntArrayRegion(iconBits, 0, nBits, colorBits);
 967         }
 968         }
 969         // Fix 4745575 GDI Resource Leak
 970         // MSDN
 971         // GetIconInfo creates bitmaps for the hbmMask and hbmColor members of ICONINFO.
 972         // The calling application must manage these bitmaps and delete them when they
 973         // are no longer necessary.
 974         ::DeleteObject(iconInfo.hbmColor);
 975         ::DeleteObject(iconInfo.hbmMask);
 976     }
 977     return iconBits;
 978 }
 979 
 980 /*
 981  * Class:     sun_awt_shell_Win32ShellFolder2
 982  * Method:    getStandardViewButton0
 983  * Signature: (I)[I
 984  */
 985 JNIEXPORT jintArray JNICALL Java_sun_awt_shell_Win32ShellFolder2_getStandardViewButton0
 986     (JNIEnv* env, jclass cls, jint iconIndex)
 987 {
 988     jintArray result = NULL;
 989 
 990     // Create a toolbar
 991     HWND hWndToolbar = ::CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
 992         0, 0, 0, 0, 0,
 993         NULL, NULL, NULL, NULL);
 994 
 995     if (hWndToolbar != NULL) {
 996         SendMessage(hWndToolbar, TB_LOADIMAGES, (WPARAM)IDB_VIEW_SMALL_COLOR, (LPARAM)HINST_COMMCTRL);
 997 
 998         HIMAGELIST hImageList = (HIMAGELIST) SendMessage(hWndToolbar, TB_GETIMAGELIST, 0, 0);
 999 
1000         if (hImageList != NULL) {
1001             HICON hIcon = ImageList_GetIcon(hImageList, iconIndex, ILD_TRANSPARENT);
1002 
1003             if (hIcon != NULL) {
1004                 result = Java_sun_awt_shell_Win32ShellFolder2_getIconBits(env, cls, ptr_to_jlong(hIcon), 16);
1005 
1006                 DestroyIcon(hIcon);
1007             }
1008 
1009             ImageList_Destroy(hImageList);
1010         }
1011 
1012         DestroyWindow(hWndToolbar);
1013     }
1014 
1015     return result;
1016 }
1017 
1018 /*
1019  * Class:     sun_awt_shell_Win32ShellFolder2
1020  * Method:    getSystemIcon
1021  * Signature: (I)J
1022  */
1023 JNIEXPORT jlong JNICALL Java_sun_awt_shell_Win32ShellFolder2_getSystemIcon
1024     (JNIEnv* env, jclass cls, jint iconID)
1025 {
1026     return (jlong)LoadIcon(NULL, MAKEINTRESOURCE(iconID));
1027 }
1028 
1029 
1030 /*
1031  * Class:     sun_awt_shell_Win32ShellFolder2
1032  * Method:    getIconResource
1033  * Signature: (Ljava/lang/String;IIIZ)J
1034  */
1035 JNIEXPORT jlong JNICALL Java_sun_awt_shell_Win32ShellFolder2_getIconResource
1036     (JNIEnv* env, jclass cls, jstring libName, jint iconID,
1037      jint cxDesired, jint cyDesired, jboolean useVGAColors)
1038 {
1039     const char *pLibName = env->GetStringUTFChars(libName, NULL);
1040     JNU_CHECK_EXCEPTION_RETURN(env, 0);
1041     HINSTANCE libHandle = (HINSTANCE)JDK_LoadSystemLibrary(pLibName);
1042     if (libHandle != NULL) {
1043         UINT fuLoad = (useVGAColors && !IS_WINXP) ? LR_VGACOLOR : 0;
1044         return ptr_to_jlong(LoadImage(libHandle, MAKEINTRESOURCE(iconID),
1045                                       IMAGE_ICON, cxDesired, cyDesired,
1046                                       fuLoad));
1047     }
1048     return 0;
1049 }
1050 
1051 
1052 /*
1053  * Helper function for creating Java column info object
1054  */
1055 static jobject CreateColumnInfo(JNIEnv *pEnv,
1056                                 jclass *pClass, jmethodID *pConstructor,
1057                                 SHELLDETAILS *psd, ULONG visible)
1058 {
1059     jstring str = jstringFromSTRRET(pEnv, NULL, &(psd->str));
1060     JNU_CHECK_EXCEPTION_RETURN(pEnv, NULL);
1061 
1062     return pEnv->NewObject(*pClass, *pConstructor,
1063                     str,
1064                     (jint)(psd->cxChar * 6), // TODO: is 6 OK for converting chars to pixels?
1065                     (jint)psd->fmt, (jboolean) visible);
1066 }
1067 
1068 
1069 /*
1070  * Class:     sun_awt_shell_Win32ShellFolder2
1071  * Method:    doGetColumnInfo
1072  * Signature: (J)[Lsun/awt/shell/ShellFolderColumnInfo;
1073  */
1074 JNIEXPORT jobjectArray JNICALL
1075     Java_sun_awt_shell_Win32ShellFolder2_doGetColumnInfo
1076             (JNIEnv *env, jobject obj, jlong iShellFolder)
1077 {
1078 
1079     HRESULT hr;
1080     IShellFolder *pIShellFolder = (IShellFolder*) iShellFolder;
1081     IUnknown *pIUnknown = NULL;
1082 
1083     jclass columnClass = env->FindClass("sun/awt/shell/ShellFolderColumnInfo");
1084     if(NULL == columnClass) {
1085         return NULL;
1086     }
1087 
1088     jmethodID columnConstructor =
1089         env->GetMethodID(columnClass, "<init>", "(Ljava/lang/String;IIZ)V");
1090     if(NULL == columnConstructor) {
1091         return NULL;
1092     }
1093 
1094     // We'are asking the object the list of available columns
1095     SHELLDETAILS sd;
1096 
1097     hr = pIShellFolder->QueryInterface(IID_IShellFolder2, (void**)&pIUnknown);
1098     if(SUCCEEDED (hr)) {
1099 
1100         // The folder exposes IShellFolder2 interface
1101         IShellFolder2 *pIShellFolder2 = (IShellFolder2*) pIUnknown;
1102 
1103         // Count columns
1104         int colNum = -1;
1105         hr = S_OK;
1106         do{
1107             hr = pIShellFolder2->GetDetailsOf(NULL, ++colNum, &sd);
1108         } while (SUCCEEDED (hr));
1109 
1110         jobjectArray columns =
1111             env->NewObjectArray((jsize) colNum, columnClass, NULL);
1112         if(NULL == columns) {
1113             pIShellFolder2->Release();
1114             return NULL;
1115         }
1116 
1117         // Fill column details list
1118         SHCOLSTATEF csFlags;
1119         colNum = 0;
1120         hr = S_OK;
1121         while (SUCCEEDED (hr)) {
1122             hr = pIShellFolder2->GetDetailsOf(NULL, colNum, &sd);
1123 
1124             if (SUCCEEDED (hr)) {
1125                 hr = pIShellFolder2->GetDefaultColumnState(colNum, &csFlags);
1126                 if (SUCCEEDED (hr)) {
1127                     if(!(csFlags & SHCOLSTATE_HIDDEN)) {
1128                         jobject column = CreateColumnInfo(env,
1129                                             &columnClass, &columnConstructor,
1130                                             &sd, csFlags & SHCOLSTATE_ONBYDEFAULT);
1131                         if(!column){
1132                             pIShellFolder2->Release();
1133                             return NULL;
1134                         }
1135                         env->SetObjectArrayElement(columns, (jsize) colNum, column);
1136                     }
1137                 }
1138                 colNum++;
1139             }
1140         }
1141 
1142         pIShellFolder2->Release();
1143 
1144         return columns;
1145     }
1146 
1147     hr = pIShellFolder->CreateViewObject(NULL, IID_IShellDetails, (void**)&pIUnknown);
1148     if(SUCCEEDED (hr)) {
1149         // The folder exposes IShellDetails interface
1150         IShellDetails *pIShellDetails = (IShellDetails*) pIUnknown;
1151 
1152         // Count columns
1153         int colNum = -1;
1154         hr = S_OK;
1155         do{
1156             hr = pIShellDetails->GetDetailsOf(NULL, ++colNum, &sd);
1157         } while (SUCCEEDED (hr));
1158 
1159         jobjectArray columns =
1160             env->NewObjectArray((jsize) colNum, columnClass, NULL);
1161         if(NULL == columns) {
1162             pIShellDetails->Release();
1163             return NULL;
1164         }
1165 
1166         // Fill column details list
1167         colNum = 0;
1168         hr = S_OK;
1169         while (SUCCEEDED (hr)) {
1170             hr = pIShellDetails->GetDetailsOf(NULL, colNum, &sd);
1171             if (SUCCEEDED (hr)) {
1172                 jobject column = CreateColumnInfo(env,
1173                                     &columnClass, &columnConstructor,
1174                                     &sd, 1);
1175                 if(!column){
1176                     pIShellDetails->Release();
1177                     return NULL;
1178                 }
1179                 env->SetObjectArrayElement(columns, (jsize) colNum++, column);
1180             }
1181         }
1182 
1183         pIShellDetails->Release();
1184 
1185         return columns;
1186     }
1187 
1188     // The folder exposes neither IShellFolder2 nor IShelDetails
1189     return NULL;
1190 
1191 }
1192 
1193 /*
1194  * Class:     sun_awt_shell_Win32ShellFolder2
1195  * Method:    doGetColumnValue
1196  * Signature: (JJI)Ljava/lang/Object;
1197  */
1198 JNIEXPORT jobject JNICALL
1199     Java_sun_awt_shell_Win32ShellFolder2_doGetColumnValue
1200             (JNIEnv *env, jobject obj, jlong iShellFolder,
1201             jlong jpidl, jint columnIdx)
1202 {
1203 
1204     HRESULT hr;
1205     IShellFolder *pIShellFolder = (IShellFolder*) iShellFolder;
1206     IUnknown *pIUnknown = NULL;
1207 
1208 
1209     LPITEMIDLIST pidl = (LPITEMIDLIST) jpidl;
1210     SHELLDETAILS sd;
1211 
1212     hr = pIShellFolder->QueryInterface(IID_IShellFolder2, (void**)&pIUnknown);
1213     if(SUCCEEDED (hr)) {
1214         // The folder exposes IShellFolder2 interface
1215         IShellFolder2 *pIShellFolder2 = (IShellFolder2*) pIUnknown;
1216         hr = pIShellFolder2->GetDetailsOf(pidl, (UINT)columnIdx, &sd);
1217         pIShellFolder2->Release();
1218         if (SUCCEEDED (hr)) {
1219             STRRET strRet = sd.str;
1220             return jstringFromSTRRET(env, pidl, &strRet);
1221         }
1222     }
1223 
1224     hr = pIShellFolder->CreateViewObject(NULL, IID_IShellDetails, (void**)&pIUnknown);
1225     if(SUCCEEDED (hr)) {
1226         // The folder exposes IShellDetails interface
1227         IShellDetails *pIShellDetails = (IShellDetails*) pIUnknown;
1228         hr = pIShellDetails->GetDetailsOf(pidl, (UINT)columnIdx, &sd);
1229         pIShellDetails->Release();
1230         if (SUCCEEDED (hr)) {
1231             STRRET strRet = sd.str;
1232             return jstringFromSTRRET(env, pidl, &strRet);
1233         }
1234     }
1235 
1236     // The folder exposes neither IShellFolder2 nor IShelDetails
1237     return NULL;
1238 }
1239 
1240 /*
1241  * Class:     sun_awt_shell_Win32ShellFolder2
1242  * Method:    compareIDsByColumn
1243  * Signature: (JJJI)I
1244  */
1245 JNIEXPORT jint JNICALL
1246     Java_sun_awt_shell_Win32ShellFolder2_compareIDsByColumn
1247             (JNIEnv* env, jclass cls, jlong jpParentIShellFolder,
1248             jlong pIDL1, jlong pIDL2, jint columnIdx)
1249 {
1250     IShellFolder* pParentIShellFolder = (IShellFolder*)jpParentIShellFolder;
1251     if (pParentIShellFolder == NULL) {
1252         return 0;
1253     }
1254 
1255     HRESULT hr = pParentIShellFolder->CompareIDs(
1256                                             (UINT) columnIdx,
1257                                             (LPCITEMIDLIST) pIDL1,
1258                                             (LPCITEMIDLIST) pIDL2);
1259     if (SUCCEEDED (hr)) {
1260         return (jint) (short) HRESULT_CODE(hr);
1261     }
1262 
1263     return 0;
1264 }
1265 
1266 /*
1267  * Class:     sun_awt_shell_Win32ShellFolder2
1268  * Method:    loadKnownFolders
1269  * Signature: (V)[BLsun/awt/shell/Win32ShellFolder2$KnownfolderDefenition;
1270  */
1271 JNIEXPORT jobjectArray JNICALL Java_sun_awt_shell_Win32ShellFolder2_loadKnownFolders
1272     (JNIEnv* env, jclass cls )
1273 {
1274     CoInitialize(NULL);
1275     IKnownFolderManager* pkfm = NULL;
1276     HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager, NULL,
1277                                 CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm));
1278     if(!SUCCEEDED(hr)) return NULL;
1279 
1280     jclass cl = env->FindClass("sun/awt/shell/Win32ShellFolder2$KnownfolderDefenition");
1281     jfieldID field_guid = env->GetFieldID(cl, "guid", "Ljava/lang/String;");
1282     jfieldID field_name = env->GetFieldID(cl, "name", "Ljava/lang/String;");
1283     jfieldID field_description = env->GetFieldID(cl, "description", "Ljava/lang/String;");
1284     jfieldID field_parent = env->GetFieldID(cl, "parent", "Ljava/lang/String;");
1285     jfieldID field_relativePath = env->GetFieldID(cl, "relativePath", "Ljava/lang/String;");
1286     jfieldID field_parsingName = env->GetFieldID(cl, "parsingName", "Ljava/lang/String;");
1287     jfieldID field_tooltip = env->GetFieldID(cl, "tooltip", "Ljava/lang/String;");
1288     jfieldID field_localizedName = env->GetFieldID(cl, "localizedName", "Ljava/lang/String;");
1289     jfieldID field_icon = env->GetFieldID(cl, "icon", "Ljava/lang/String;");
1290     jfieldID field_security = env->GetFieldID(cl, "security", "Ljava/lang/String;");
1291     jfieldID field_path = env->GetFieldID(cl, "path", "Ljava/lang/String;");
1292     jfieldID field_saveLocation = env->GetFieldID(cl, "saveLocation", "Ljava/lang/String;");
1293     jfieldID field_category = env->GetFieldID(cl, "category", "I");
1294     jfieldID field_attributes = env->GetFieldID(cl, "attributes", "J");
1295     jfieldID field_defenitionFlags = env->GetFieldID(cl, "defenitionFlags", "I");
1296     jfieldID field_ftidType = env->GetFieldID(cl, "ftidType", "Ljava/lang/String;");
1297 
1298     jobjectArray result;
1299         KNOWNFOLDERID* pFoldersIds = NULL;
1300         UINT count = 0;
1301         if(SUCCEEDED(pkfm->GetFolderIds(&pFoldersIds, &count))) {
1302             result = env->NewObjectArray(count, cl, NULL);
1303         jmethodID initMethod = env->GetMethodID(cl, "<init>", "()V");
1304         for(UINT i = 0; i < count; ++i)
1305         {
1306                 jobject fld = env->NewObject(cl, initMethod);
1307                 env->SetObjectArrayElement(result, i, fld);
1308 
1309             const KNOWNFOLDERID& folderId = pFoldersIds[i];
1310             LPOLESTR guid = NULL;
1311             if(SUCCEEDED(StringFromCLSID(folderId, &guid))) {
1312                 env->SetObjectField(fld, field_guid, JNU_NewStringPlatform(env, guid));
1313                 CoTaskMemFree(guid);
1314             }
1315 
1316             IKnownFolder* pFolder = NULL;
1317             if(SUCCEEDED(pkfm->GetFolder(folderId, &pFolder))) {
1318                 KNOWNFOLDER_DEFINITION kfDef;
1319                 if(SUCCEEDED(pFolder->GetFolderDefinition(&kfDef)))
1320                 {
1321                     env->SetObjectField(fld, field_name,
1322                                JNU_NewStringPlatform(env, kfDef.pszName));
1323                     if(kfDef.pszDescription)
1324                         env->SetObjectField(fld, field_description,
1325                                JNU_NewStringPlatform(env, kfDef.pszDescription));
1326                     if(SUCCEEDED(StringFromCLSID(kfDef.fidParent, &guid))) {
1327                         env->SetObjectField(fld, field_parent,
1328                                     JNU_NewStringPlatform(env, guid));
1329                         CoTaskMemFree(guid);
1330                     }
1331                     if(kfDef.pszRelativePath)
1332                         env->SetObjectField(fld, field_relativePath,
1333                                     JNU_NewStringPlatform(env, kfDef.pszRelativePath));
1334                     if(kfDef.pszParsingName)
1335                         env->SetObjectField(fld, field_parsingName,
1336                                     JNU_NewStringPlatform(env, kfDef.pszParsingName));
1337                     if(kfDef.pszTooltip)
1338                         env->SetObjectField(fld, field_tooltip,
1339                                     JNU_NewStringPlatform(env, kfDef.pszTooltip));
1340                     if(kfDef.pszLocalizedName)
1341                         env->SetObjectField(fld, field_localizedName,
1342                                     JNU_NewStringPlatform(env, kfDef.pszLocalizedName));
1343                     if(kfDef.pszIcon)
1344                         env->SetObjectField(fld, field_icon,
1345                                     JNU_NewStringPlatform(env, kfDef.pszIcon));
1346                     if(kfDef.pszSecurity)
1347                         env->SetObjectField(fld, field_security,
1348                                     JNU_NewStringPlatform(env, kfDef.pszSecurity));
1349                     if(SUCCEEDED(StringFromCLSID(kfDef.ftidType, &guid))) {
1350                         env->SetObjectField(fld, field_ftidType,
1351                                     JNU_NewStringPlatform(env, guid));
1352                         CoTaskMemFree(guid);
1353                     }
1354                     env->SetIntField(fld, field_category, kfDef.category);
1355                     env->SetIntField(fld, field_defenitionFlags, kfDef.kfdFlags);
1356                     env->SetLongField(fld, field_attributes, kfDef.dwAttributes);
1357                     FreeKnownFolderDefinitionFields(&kfDef);
1358 
1359                     LPWSTR folderPath = NULL;
1360                     if(SUCCEEDED(pFolder->GetPath(KF_FLAG_NO_ALIAS, &folderPath))
1361                                 && folderPath) {
1362                         env->SetObjectField(fld, field_path,
1363                                JNU_NewStringPlatform(env, folderPath));
1364                         CoTaskMemFree(folderPath);
1365                     }
1366 
1367                     IShellLibrary *plib = NULL;
1368                     hr = CoCreateInstance(CLSID_ShellLibrary, NULL,
1369                                      CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&plib));
1370                     if(SUCCEEDED(hr)) {
1371                         hr = plib->LoadLibraryFromKnownFolder(folderId, STGM_READWRITE);
1372                         if(SUCCEEDED(hr)) {
1373                             IShellItem *item = NULL;
1374                             hr = plib->GetDefaultSaveFolder(DSFT_DETECT,
1375                                     IID_PPV_ARGS(&item));
1376                             if(SUCCEEDED(hr) && item) {
1377                                 LPWSTR loc = NULL;
1378                                 hr = item->GetDisplayName(SIGDN_FILESYSPATH, &loc);
1379                                 if(SUCCEEDED(hr) && loc)
1380                                 {
1381                                     env->SetObjectField(fld, field_saveLocation,
1382                                             JNU_NewStringPlatform(env, loc));
1383                                     CoTaskMemFree(loc);
1384                                 }
1385                                 item->Release();
1386                             }
1387                         }
1388                         plib->Release();
1389                     }
1390                 }
1391             }
1392             pFolder->Release();
1393         }
1394         CoTaskMemFree(pFoldersIds);
1395         }
1396     pkfm->Release();
1397     return result;
1398 }
1399 
1400 } // extern "C"