1 /* 2 * Copyright (c) 2005, 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 #include "jni_util.h" 27 #include "awt.h" 28 #include <jni.h> 29 #include <shellapi.h> 30 #include <float.h> 31 #include <shlobj.h> 32 #include "awt_Toolkit.h" 33 34 #define BUFFER_LIMIT MAX_PATH+1 35 36 #define NOTIFY_FOR_ALL_SESSIONS 1 37 #define NOTIFY_FOR_THIS_SESSION 0 38 39 typedef BOOL (WINAPI *WTSRegisterSessionNotification)(HWND,DWORD); 40 static WTSRegisterSessionNotification fn_WTSRegisterSessionNotification; 41 42 #ifdef __cplusplus 43 extern "C" { 44 #endif 45 46 /* 47 * Class: sun_awt_windows_WDesktopPeer 48 * Method: init 49 * Signature: ()V 50 */ 51 JNIEXPORT void JNICALL Java_sun_awt_windows_WDesktopPeer_init 52 (JNIEnv *, jclass) { 53 static HMODULE libWtsapi32 = NULL; 54 if (libWtsapi32 == NULL) { 55 libWtsapi32 = JDK_LoadSystemLibrary("Wtsapi32.dll"); 56 if (libWtsapi32) { 57 fn_WTSRegisterSessionNotification = (WTSRegisterSessionNotification) 58 GetProcAddress(libWtsapi32, "WTSRegisterSessionNotification"); 59 if (fn_WTSRegisterSessionNotification) { 60 HWND hwnd = AwtToolkit::GetInstance().GetHWnd(); 61 //used for UserSessionListener 62 fn_WTSRegisterSessionNotification(hwnd, NOTIFY_FOR_THIS_SESSION); 63 } 64 } 65 } 66 ::CoInitialize(NULL); 67 } 68 69 /* 70 * Class: sun_awt_windows_WDesktopPeer 71 * Method: ShellExecute 72 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; 73 */ 74 JNIEXPORT jstring JNICALL Java_sun_awt_windows_WDesktopPeer_ShellExecute 75 (JNIEnv *env, jclass cls, jstring fileOrUri_j, jstring verb_j) 76 { 77 LPCWSTR fileOrUri_c = JNU_GetStringPlatformChars(env, fileOrUri_j, JNI_FALSE); 78 CHECK_NULL_RETURN(fileOrUri_c, NULL); 79 LPCWSTR verb_c = JNU_GetStringPlatformChars(env, verb_j, JNI_FALSE); 80 if (verb_c == NULL) { 81 JNU_ReleaseStringPlatformChars(env, fileOrUri_j, fileOrUri_c); 82 return NULL; 83 } 84 85 // 6457572: ShellExecute possibly changes FPU control word - saving it here 86 unsigned oldcontrol87 = _control87(0, 0); 87 HINSTANCE retval = ::ShellExecute(NULL, verb_c, fileOrUri_c, NULL, NULL, SW_SHOWNORMAL); 88 DWORD error = ::GetLastError(); 89 _control87(oldcontrol87, 0xffffffff); 90 91 JNU_ReleaseStringPlatformChars(env, fileOrUri_j, fileOrUri_c); 92 JNU_ReleaseStringPlatformChars(env, verb_j, verb_c); 93 94 if ((int)retval <= 32) { 95 // ShellExecute failed. 96 LPTSTR buffer = NULL; 97 int len = ::FormatMessage( 98 FORMAT_MESSAGE_ALLOCATE_BUFFER | 99 FORMAT_MESSAGE_FROM_SYSTEM | 100 FORMAT_MESSAGE_IGNORE_INSERTS, 101 NULL, 102 error, 103 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language 104 (LPTSTR)&buffer, 105 0, 106 NULL ); 107 108 if (buffer) { 109 jstring errmsg = JNU_NewStringPlatform(env, buffer); 110 LocalFree(buffer); 111 return errmsg; 112 } 113 } 114 115 return NULL; 116 } 117 118 /* 119 * Class: sun_awt_windows_WDesktopPeer 120 * Method: moveToTrash 121 * Signature: (Ljava/lang/String;)Z 122 */ 123 JNIEXPORT jboolean JNICALL Java_sun_awt_windows_WDesktopPeer_moveToTrash 124 (JNIEnv *env, jclass, jstring jpath) 125 { 126 LPCTSTR pathStr = JNU_GetStringPlatformChars(env, jpath, (jboolean *)NULL); 127 if (pathStr) { 128 try { 129 LPTSTR fileBuffer = new TCHAR[BUFFER_LIMIT]; 130 memset(fileBuffer, 0, BUFFER_LIMIT * sizeof(TCHAR)); 131 // the fileBuffer is double null terminated string 132 _tcsncpy(fileBuffer, pathStr, BUFFER_LIMIT - 2); 133 134 SHFILEOPSTRUCT fop; 135 memset(&fop, 0, sizeof(SHFILEOPSTRUCT)); 136 fop.hwnd = NULL; 137 fop.wFunc = FO_DELETE; 138 fop.pFrom = fileBuffer; 139 fop.fFlags = FOF_ALLOWUNDO; 140 141 int res = SHFileOperation(&fop); 142 143 delete[] fileBuffer; 144 JNU_ReleaseStringPlatformChars(env, jpath, pathStr); 145 146 return !res ? JNI_TRUE : JNI_FALSE; 147 } catch (std::bad_alloc&) { 148 JNU_ReleaseStringPlatformChars(env, jpath, pathStr); 149 } 150 } 151 return JNI_FALSE; 152 } 153 154 #ifdef __cplusplus 155 } 156 #endif