< prev index next >

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

Print this page

        

@@ -247,11 +247,11 @@
     jstring utf_str;
     jclass clazz = env->FindClass("java/lang/String");
     if (clazz == NULL) {
         return NULL;
     }
-    jobjectArray nameArray;
+    jobjectArray nameArray = NULL;
 
     try {
         ::EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
                        NULL, 4, NULL, 0, &cbNeeded, &cReturned);
         pPrinterEnum = new BYTE[cbNeeded];

@@ -268,17 +268,18 @@
                 if (info4->Attributes & PRINTER_ATTRIBUTE_NETWORK) {
                     pNetworkPrinterLoc[remotePrintersCount++] = i;
                 }
             }
 
+            // return remote printers only if the list contains it.
+            if (remotePrintersCount > 0) {
             // Allocate space only for the network type printers
             nameArray = env->NewObjectArray(remotePrintersCount, clazz, NULL);
             if (nameArray == NULL) {
                 throw std::bad_alloc();
             }
-        } else {
-            nameArray = NULL;
+            }
         }
 
         // Loop thro' network printers list only
         for (int i = 0; i < remotePrintersCount; i++) {
             PRINTER_INFO_4 *info4 = (PRINTER_INFO_4 *)

@@ -296,11 +297,16 @@
         throw;
     }
 
     delete [] pPrinterEnum;
     delete [] pNetworkPrinterLoc;
+
+    if (nameArray != NULL) {
     return nameArray;
+    } else {
+      return env->NewObjectArray(0, clazz, NULL);
+    }
 
     CATCH_BAD_ALLOC_RET(NULL);
 }
 
 JNIEXPORT jfloatArray JNICALL
< prev index next >