From b25ed57b764fc485e4e8ca4118ffb1cc70fdfe7f Mon Sep 17 00:00:00 2001 From: Sergey Bylokhov Date: Tue, 12 Dec 2023 18:30:41 +0000 Subject: [PATCH] 8270269: Desktop.browse method fails if earlier CoInitialize call as COINIT_MULTITHREADED Reviewed-by: aivanov --- .../classes/sun/awt/windows/WDesktopPeer.java | 18 +++++++++++------- .../native/libawt/windows/awt_Desktop.cpp | 18 ++++-------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/java.desktop/windows/classes/sun/awt/windows/WDesktopPeer.java b/src/java.desktop/windows/classes/sun/awt/windows/WDesktopPeer.java index 57b5f70ca37..788c1477265 100644 --- a/src/java.desktop/windows/classes/sun/awt/windows/WDesktopPeer.java +++ b/src/java.desktop/windows/classes/sun/awt/windows/WDesktopPeer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -40,6 +40,8 @@ import java.net.URI; import javax.swing.event.EventListenerList; +import sun.awt.shell.ShellFolder; + /** * Concrete implementation of the interface {@code DesktopPeer} for * the Windows platform. @@ -102,18 +104,20 @@ final class WDesktopPeer implements DesktopPeer { } private void ShellExecute(File file, String verb) throws IOException { - String errMsg = ShellExecute(file.getAbsolutePath(), verb); + String errMsg = ShellFolder.invoke( + () -> ShellExecute(file.getAbsolutePath(), verb)); if (errMsg != null) { - throw new IOException("Failed to " + verb + " " + file + ". Error message: " + errMsg); + throw new IOException("Failed to " + verb + " " + file + + ". Error message: " + errMsg); } } private void ShellExecute(URI uri, String verb) throws IOException { - String errmsg = ShellExecute(uri.toString(), verb); - + String errmsg = ShellFolder.invoke( + () -> ShellExecute(uri.toString(), verb)); if (errmsg != null) { - throw new IOException("Failed to " + verb + " " + uri - + ". Error message: " + errmsg); + throw new IOException("Failed to " + verb + " " + uri + + ". Error message: " + errmsg); } } diff --git a/src/java.desktop/windows/native/libawt/windows/awt_Desktop.cpp b/src/java.desktop/windows/native/libawt/windows/awt_Desktop.cpp index 9cf2cea6fab..ba79523249c 100644 --- a/src/java.desktop/windows/native/libawt/windows/awt_Desktop.cpp +++ b/src/java.desktop/windows/native/libawt/windows/awt_Desktop.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -85,24 +85,14 @@ JNIEXPORT jstring JNICALL Java_sun_awt_windows_WDesktopPeer_ShellExecute // 6457572: ShellExecute possibly changes FPU control word - saving it here unsigned oldcontrol87 = _control87(0, 0); - HRESULT hr = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | - COINIT_DISABLE_OLE1DDE); - HINSTANCE retval; - DWORD error; - if (SUCCEEDED(hr)) { - retval = ::ShellExecute(NULL, verb_c, fileOrUri_c, NULL, NULL, - SW_SHOWNORMAL); - error = ::GetLastError(); - ::CoUninitialize(); - } + HINSTANCE retval = ::ShellExecute(NULL, verb_c, fileOrUri_c, NULL, NULL, + SW_SHOWNORMAL); + DWORD error = ::GetLastError(); _control87(oldcontrol87, 0xffffffff); JNU_ReleaseStringPlatformChars(env, fileOrUri_j, fileOrUri_c); JNU_ReleaseStringPlatformChars(env, verb_j, verb_c); - if (FAILED(hr)) { - return JNU_NewStringPlatform(env, L"CoInitializeEx() failed."); - } if ((int)((intptr_t)retval) <= 32) { // ShellExecute failed. LPTSTR buffer = NULL;