< prev index next >

src/java.desktop/windows/native/libawt/windows/awt_Desktop.cpp

Print this page




  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 
  32 #ifdef __cplusplus
  33 extern "C" {
  34 #endif
  35 
  36 /*
  37  * Class:     sun_awt_windows_WDesktopPeer

























  38  * Method:    ShellExecute
  39  * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
  40  */
  41 JNIEXPORT jstring JNICALL Java_sun_awt_windows_WDesktopPeer_ShellExecute
  42   (JNIEnv *env, jclass cls, jstring fileOrUri_j, jstring verb_j)
  43 {
  44     LPCWSTR fileOrUri_c = JNU_GetStringPlatformChars(env, fileOrUri_j, JNI_FALSE);
  45     CHECK_NULL_RETURN(fileOrUri_c, NULL);
  46     LPCWSTR verb_c = JNU_GetStringPlatformChars(env, verb_j, JNI_FALSE);
  47     if (verb_c == NULL) {
  48         JNU_ReleaseStringPlatformChars(env, fileOrUri_j, fileOrUri_c);
  49         return NULL;
  50     }
  51 
  52     // 6457572: ShellExecute possibly changes FPU control word - saving it here
  53     unsigned oldcontrol87 = _control87(0, 0);
  54     HINSTANCE retval = ::ShellExecute(NULL, verb_c, fileOrUri_c, NULL, NULL, SW_SHOWNORMAL);
  55     DWORD error = ::GetLastError();
  56     _control87(oldcontrol87, 0xffffffff);
  57 


  63         LPTSTR buffer = NULL;
  64         int len = ::FormatMessage(
  65                     FORMAT_MESSAGE_ALLOCATE_BUFFER |
  66                     FORMAT_MESSAGE_FROM_SYSTEM  |
  67                     FORMAT_MESSAGE_IGNORE_INSERTS,
  68                     NULL,
  69                     error,
  70                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  71                     (LPTSTR)&buffer,
  72                     0,
  73                     NULL );
  74 
  75         if (buffer) {
  76             jstring errmsg = JNU_NewStringPlatform(env, buffer);
  77             LocalFree(buffer);
  78             return errmsg;
  79         }
  80     }
  81 
  82     return NULL;




































  83 }
  84 
  85 #ifdef __cplusplus
  86 }
  87 #endif


  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     fprintf(stderr, "%s:%i \n", __FUNCTION__, __LINE__);
  67         fflush(stderr);
  68     ::CoInitialize(NULL);
  69 }
  70     
  71 /*
  72  * Class:     sun_awt_windows_WDesktopPeer
  73  * Method:    ShellExecute
  74  * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
  75  */
  76 JNIEXPORT jstring JNICALL Java_sun_awt_windows_WDesktopPeer_ShellExecute
  77   (JNIEnv *env, jclass cls, jstring fileOrUri_j, jstring verb_j)
  78 {
  79     LPCWSTR fileOrUri_c = JNU_GetStringPlatformChars(env, fileOrUri_j, JNI_FALSE);
  80     CHECK_NULL_RETURN(fileOrUri_c, NULL);
  81     LPCWSTR verb_c = JNU_GetStringPlatformChars(env, verb_j, JNI_FALSE);
  82     if (verb_c == NULL) {
  83         JNU_ReleaseStringPlatformChars(env, fileOrUri_j, fileOrUri_c);
  84         return NULL;
  85     }
  86 
  87     // 6457572: ShellExecute possibly changes FPU control word - saving it here
  88     unsigned oldcontrol87 = _control87(0, 0);
  89     HINSTANCE retval = ::ShellExecute(NULL, verb_c, fileOrUri_c, NULL, NULL, SW_SHOWNORMAL);
  90     DWORD error = ::GetLastError();
  91     _control87(oldcontrol87, 0xffffffff);
  92 


  98         LPTSTR buffer = NULL;
  99         int len = ::FormatMessage(
 100                     FORMAT_MESSAGE_ALLOCATE_BUFFER |
 101                     FORMAT_MESSAGE_FROM_SYSTEM  |
 102                     FORMAT_MESSAGE_IGNORE_INSERTS,
 103                     NULL,
 104                     error,
 105                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
 106                     (LPTSTR)&buffer,
 107                     0,
 108                     NULL );
 109 
 110         if (buffer) {
 111             jstring errmsg = JNU_NewStringPlatform(env, buffer);
 112             LocalFree(buffer);
 113             return errmsg;
 114         }
 115     }
 116 
 117     return NULL;
 118 }
 119 
 120 /*
 121  * Class:     sun_awt_windows_WDesktopPeer
 122  * Method:    moveToTrash
 123  * Signature: (Ljava/lang/String;)Z
 124  */
 125 JNIEXPORT jboolean JNICALL Java_sun_awt_windows_WDesktopPeer_moveToTrash
 126   (JNIEnv *env, jclass, jstring jpath)
 127 {
 128     LPCTSTR pathStr = JNU_GetStringPlatformChars(env, jpath, (jboolean *)NULL);
 129     if (pathStr) {
 130         try {
 131             LPTSTR fileBuffer = new TCHAR[BUFFER_LIMIT];
 132             memset(fileBuffer, 0, BUFFER_LIMIT * sizeof(TCHAR));
 133             // the fileBuffer is double null terminated string
 134             _tcsncpy(fileBuffer, pathStr, BUFFER_LIMIT - 2); 
 135 
 136             SHFILEOPSTRUCT fop;
 137             memset(&fop, 0, sizeof(SHFILEOPSTRUCT));
 138             fop.hwnd = NULL;
 139             fop.wFunc = FO_DELETE;
 140             fop.pFrom = fileBuffer;
 141             fop.fFlags = FOF_ALLOWUNDO;
 142 
 143             int res = SHFileOperation(&fop);
 144 
 145             delete[] fileBuffer;
 146             JNU_ReleaseStringPlatformChars(env, jpath, pathStr);
 147 
 148             return !res ? JNI_TRUE : JNI_FALSE;
 149         } catch (std::bad_alloc&) {
 150             JNU_ReleaseStringPlatformChars(env, jpath, pathStr);
 151         }
 152     }
 153     return JNI_FALSE;
 154 }
 155 
 156 #ifdef __cplusplus
 157 }
 158 #endif
< prev index next >