JDK-8231352 : [Citrix/RDP] PrintServiceLookup.lookupDefaultPrintService() does not return configured default printer when redirected
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 8u221,11
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_10
  • CPU: generic
  • Submitted: 2019-09-23
  • Updated: 2020-07-19
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Duplicate :  
Relates :  
Description
This bug has been originally reported at AdoptOpenJDK here:
https://github.com/AdoptOpenJDK/openjdk-build/issues/1277

In an Citrix remote desktop scenario on Windows 10 an incorrect default printer is being returned by javax.print.PrintServiceLookup.lookupDefaultPrintService(). Example code:

public class PrintServiceLookupTest {
       public static void main(String[] args) {
             javax.print.PrintService service = javax.print.PrintServiceLookup.lookupDefaultPrintService();
             if (service != null) {
                    String printServiceName = service.getName();
                    System.out.println("Default print service name (" + printServiceName+")");
             } else {
                    System.out.println("No default print service found.");
             }
       }
}

Steps to reproduce:

1. Logon to session Citrix or RDP
2. set a local or network default printer
3. run the code "PrintServiceLookupTest".
4. set a redirected default printer
5. run the code "PrintServiceLookupTest".

The registry key used for local or network printers is:

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device

The registry key used for redirect printers is:

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\SessionDefaultDevices\ "session" \Device

It looks like OpenJDK isn't using the correct registry key for the RDP/Citrix use case. This isn't an issue on a default host Windows OS. So a remote setup via Citrix is necessary to reproduce.

Actual output:
Some printer which isn't the redirected default printer.

Expected output:
Same as this windows query:

> Get-WmiObject -Query " SELECT * FROM Win32_Printer" | Where-Object {$_.Default -eq "true"} | Select Name



Comments
https://mail.openjdk.java.net/pipermail/2d-dev/2020-January/010551.html
04-06-2020

JDK-8232511 seems to be duplicate of this.
04-06-2020

As my request(31th Jan) to 2d-dev is not yet approved , hence i am attaching the patch(webrev.zip) which resolves this issue at our end.
11-02-2020

below is probable fix, i removed some old legacy code which checks the version of Windows OS and used the recommended GetDefaultPrinter API. diff -r dcf88e5c8c07 src/java.desktop/windows/native/libawt/windows/WPrinterJob.cpp --- a/src/java.desktop/windows/native/libawt/windows/WPrinterJob.cpp Mon Dec 09 16:14:16 2019 +0100 +++ b/src/java.desktop/windows/native/libawt/windows/WPrinterJob.cpp Sun Jan 19 15:58:45 2020 +0530 @@ -71,50 +71,23 @@ Java_sun_print_PrintServiceLookupProvider_getDefaultPrinterName(JNIEnv *env, jobject peer) { - TRY; - - TCHAR cBuffer[250]; - OSVERSIONINFO osv; - PRINTER_INFO_2 *ppi2 = NULL; - DWORD dwNeeded = 0; - DWORD dwReturned = 0; - LPTSTR pPrinterName = NULL; + TCHAR *cBuffer; jstring jPrinterName; - - // What version of Windows are you running? - osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - GetVersionEx(&osv); - - // If Windows 2000, XP, Vista - if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT) { - - // Retrieve the default string from Win.ini (the registry). - // String will be in form "printername,drivername,portname". - - if (GetProfileString(TEXT("windows"), TEXT("device"), TEXT(",,,"), - cBuffer, 250) <= 0) { - return NULL; - } - // Copy printer name into passed-in buffer... - int index = 0; - int len = lstrlen(cBuffer); - while ((index < len) && cBuffer[index] != _T(',')) { - index++; - } - if (index==0) { - return NULL; - } - - pPrinterName = (LPTSTR)GlobalAlloc(GPTR, (index+1)*sizeof(TCHAR)); - lstrcpyn(pPrinterName, cBuffer, index+1); - jPrinterName = JNU_NewStringPlatform(env, pPrinterName); - GlobalFree(pPrinterName); - return jPrinterName; - } else { + DWORD size = 0; + + GetDefaultPrinter(NULL, &size); // this API call will returns the required buffer size + cBuffer = (TCHAR*)malloc(size * sizeof(TCHAR)); + if( cBuffer == NULL){ + return NULL; + } + if(GetDefaultPrinter(cBuffer, &size) != 0){ + jPrinterName = JNU_NewStringPlatform(env, cBuffer); + free(cBuffer); + return jPrinterName; + }else { + free(cBuffer); return NULL; } - - CATCH_BAD_ALLOC_RET(NULL); }
19-01-2020

[~prr] Forwarding you an answer from the original reporter: Hi Philip, you can try in Terminal Server (RDP) with Windows 2016 or 2019. "GetProfileString" api is being called to get default selected printer and that is been deprecated. I have opened a case at Microsoft and the new method is GetDefaultPrinter https://docs.microsoft.com/en-us/windows/win32/printdocs/getdefaultprinter Regards Simone Borsato
25-09-2019

I can't comment on (since I don't know anything about) the supposed redirect printer key needed for Citrix but I do see where we are getting the value as described in Java_sun_print_PrintServiceLookupProvider_getDefaultPrinterName(..) ... // Retrieve the default string from Win.ini (the registry). // String will be in form "printername,drivername,portname". if (GetProfileString(TEXT("windows"), TEXT("device"), TEXT(",,,"), cBuffer, 250) <= 0) { ... https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprofilestringa suggests we shouldn't be calling that legacy function at all. There must be a better way without going near the registry. But since I am unlikely to ever have access to a Citrix set up I don't think we'll be able to test it.
23-09-2019