src/windows/native/sun/windows/WPrinterJob.cpp

Print this page

        

@@ -128,10 +128,13 @@
     DWORD cReturned = 0;
     LPBYTE pPrinterEnum = NULL;
 
     jstring utf_str;
     jclass clazz = env->FindClass("java/lang/String");
+    if (clazz == NULL) {
+        return NULL;
+    }
     jobjectArray nameArray;
 
     try {
         ::EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
                        NULL, 4, NULL, 0, &cbNeeded, &cReturned);

@@ -238,10 +241,13 @@
 {
     TRY;
 
     LPTSTR printerName = (LPTSTR)JNU_GetStringPlatformChars(env,
                                                             printer, NULL);
+    if (printerName == NULL) {
+        return NULL;
+    }
 
     jfloatArray printableArray = NULL;
 
     SAVE_CONTROLWORD
     HDC pdc = CreateDC(TEXT("WINSPOOL"), printerName, NULL, NULL);

@@ -260,11 +266,11 @@
             /* if failure, cleanup and return failure */
 
             if (pDevMode != NULL) {
                 ::GlobalFree(pDevMode);
             }
-
+            DeleteDC(pdc);
             ::ClosePrinter(hPrinter);
             JNU_ReleaseStringPlatformChars(env, printer, printerName);
             return printableArray;
         }
 

@@ -281,235 +287,246 @@
 
         int resx = GetDeviceCaps(pdc, LOGPIXELSX);
         int resy = GetDeviceCaps(pdc, LOGPIXELSY);
 
         printableArray=env->NewFloatArray(4);
-        if (printableArray == NULL) {
-            throw std::bad_alloc();
-        }
-        jboolean isCopy;
-        jfloat *iPrintables = env->GetFloatArrayElements(printableArray,
-                                                         &isCopy),
-            *savePrintables = iPrintables;
-
+        if (printableArray != NULL) {
+            jfloat *iPrintables =
+                env->GetFloatArrayElements(printableArray, NULL);
+            if (iPrintables != NULL) {
         iPrintables[0] = (float)left/resx;
         iPrintables[1] = (float)top/resy;
         iPrintables[2] = (float)width/resx;
         iPrintables[3] = (float)height/resy;
-
-        env->ReleaseFloatArrayElements(printableArray, savePrintables, 0);
-
+                env->ReleaseFloatArrayElements(printableArray, iPrintables, 0);
+            }
+        }
         GlobalFree(pDevMode);
+        DeleteDC(pdc);
     }
 
-    DeleteDC(pdc);
     JNU_ReleaseStringPlatformChars(env, printer, printerName);
 
     return printableArray;
 
     CATCH_BAD_ALLOC_RET(NULL);
 }
 
-
-JNIEXPORT jintArray JNICALL
-Java_sun_print_Win32PrintService_getAllMediaIDs(JNIEnv *env,
-                                                jobject peer,
-                                                jstring printer,
-                                                jstring port)
+jintArray getIDs(JNIEnv *env, jstring printer, jstring port, int dm_id)
 {
-  TRY;
 
   LPTSTR printerName = (LPTSTR)JNU_GetStringPlatformChars(env, printer, NULL);
   LPTSTR printerPort = (LPTSTR)JNU_GetStringPlatformChars(env, port, NULL);
-  jintArray mediasizeArray = NULL;
+
+  if (printerName == NULL || printerPort == NULL) {
+      if (printerName != NULL) {
+          JNU_ReleaseStringPlatformChars(env, printer, printerName);
+      }
+      if (printerPort != NULL) {
+          JNU_ReleaseStringPlatformChars(env, port, printerPort);
+      }
+      return NULL;
+  }
 
   SAVE_CONTROLWORD
-  int numSizes = ::DeviceCapabilities(printerName, printerPort,
-                                      DC_PAPERS,   NULL, NULL);
+  int numIDs = ::DeviceCapabilities(printerName, printerPort, dm_id,
+                                    NULL, NULL);
   RESTORE_CONTROLWORD
 
-  if (numSizes > 0) {
-
-    mediasizeArray = env->NewIntArray(numSizes);
-    if (mediasizeArray == NULL) {
-      throw std::bad_alloc();
+  jintArray idArray = NULL;
+  if (numIDs > 0) {
+      idArray = env->NewIntArray(numIDs);
+      if (idArray != NULL) {
+          jint *jpcIndices = env->GetIntArrayElements(idArray, NULL);
+          if (jpcIndices != NULL) {
+              jint *saveFormats = jpcIndices;
+              LPTSTR buf = NULL;
+              try {
+                  buf = (LPTSTR)new char[numIDs * sizeof(WORD)];
+              } catch (std::bad_alloc&) {
+                  buf = NULL;
     }
-
-    jboolean isCopy;
-    jint *jpcIndices = env->GetIntArrayElements(mediasizeArray,
-                                       &isCopy), *saveFormats = jpcIndices;
-    LPTSTR papersBuf = (LPTSTR)new char[numSizes * sizeof(WORD)];
+              if (buf != NULL) {
     if (::DeviceCapabilities(printerName, printerPort,
-                             DC_PAPERS, papersBuf, NULL) != -1) {
+                                           dm_id, buf, NULL) != -1) {
+                      WORD *id = (WORD *)buf;
+                      for (int i = 0; i < numIDs; i++, id++) {
+                          jpcIndices[i] = *id;
+                      }
+                  }
       RESTORE_CONTROLWORD
-      WORD *pDmPaperSize = (WORD *)papersBuf;
-      for (int i = 0; i < numSizes; i++, pDmPaperSize++) {
-        jpcIndices[i] = *pDmPaperSize;
+                  delete[] buf;
+              }
+              env->ReleaseIntArrayElements(idArray, saveFormats, 0);
       }
     }
-    delete[] papersBuf;
-    env->ReleaseIntArrayElements(mediasizeArray, saveFormats, 0);
   }
 
   JNU_ReleaseStringPlatformChars(env, printer, printerName);
   JNU_ReleaseStringPlatformChars(env, port, printerPort);
-  return mediasizeArray;
+  return idArray;
+}
 
-  CATCH_BAD_ALLOC_RET(NULL);
+JNIEXPORT jintArray JNICALL
+Java_sun_print_Win32PrintService_getAllMediaIDs(JNIEnv *env,
+                                                jobject peer,
+                                                jstring printer,
+                                                jstring port)
+{
+    return getIDs(env, printer, port, DC_PAPERS);
 }
 
 
 JNIEXPORT jintArray JNICALL
 Java_sun_print_Win32PrintService_getAllMediaTrays(JNIEnv *env,
                                                   jobject peer,
                                                   jstring printer,
                                                   jstring port)
 {
-  TRY;
-
-  LPTSTR printerName = (LPTSTR)JNU_GetStringPlatformChars(env,
-                                                          printer, NULL);
-  LPTSTR printerPort = (LPTSTR)JNU_GetStringPlatformChars(env, port, NULL);
-
-  jintArray mediaTrayArray = NULL;
-
-  SAVE_CONTROLWORD
-  int nBins = ::DeviceCapabilities(printerName, printerPort,
-                                   DC_BINS,   NULL, NULL) ;
-  RESTORE_CONTROLWORD
-  if (nBins > 0) {
-    mediaTrayArray = env->NewIntArray(nBins);
-    if (mediaTrayArray == NULL) {
-      throw std::bad_alloc();
-    }
-
-    jboolean isCopy;
-    jint *jpcIndices = env->GetIntArrayElements(mediaTrayArray,
-                                           &isCopy), *saveFormats = jpcIndices;
-
-    LPTSTR buf = (LPTSTR)new char[nBins * sizeof(WORD)];
-
-    if (::DeviceCapabilities(printerName, printerPort,
-                             DC_BINS, buf, NULL) != -1) {
-      RESTORE_CONTROLWORD
-      WORD *pBins = (WORD *)buf;
-      for (int i = 0; i < nBins; i++) {
-        jpcIndices[i] = *(pBins+i);
-      }
-    }
-    delete[] buf;
-    env->ReleaseIntArrayElements(mediaTrayArray, saveFormats, 0);
-  }
-
-  JNU_ReleaseStringPlatformChars(env, printer, printerName);
-  JNU_ReleaseStringPlatformChars(env, port, printerPort);
-  return mediaTrayArray;
-
-  CATCH_BAD_ALLOC_RET(NULL);
+    return getIDs(env, printer, port, DC_BINS);
 }
 
 
 JNIEXPORT jintArray JNICALL
 Java_sun_print_Win32PrintService_getAllMediaSizes(JNIEnv *env,
                                                   jobject peer,
                                                   jstring printer,
                                                   jstring port)
 {
-  TRY;
-
-  LPTSTR printerName = (LPTSTR)JNU_GetStringPlatformChars(env,
-                                                          printer, NULL);
+  LPTSTR printerName = (LPTSTR)JNU_GetStringPlatformChars(env, printer, NULL);
   LPTSTR printerPort = (LPTSTR)JNU_GetStringPlatformChars(env, port, NULL);
 
-  jintArray mediaArray = NULL;
+  if (printerName == NULL || printerPort == NULL) {
+      if (printerName != NULL) {
+          JNU_ReleaseStringPlatformChars(env, printer, printerName);
+      }
+      if (printerPort != NULL) {
+          JNU_ReleaseStringPlatformChars(env, port, printerPort);
+      }
+      return NULL;
+  }
 
   SAVE_CONTROLWORD
-  int nPapers = ::DeviceCapabilities(printerName, printerPort,
-                                      DC_PAPERSIZE,   NULL, NULL) ;
+  int nPapers = ::DeviceCapabilities(printerName, printerPort, DC_PAPERSIZE,
+                                     NULL, NULL) ;
   RESTORE_CONTROLWORD
+
+  jintArray mediaArray = NULL;
+  jint *saveFormats = NULL;
+
   if (nPapers > 0) {
     mediaArray = env->NewIntArray(nPapers*2);
-    if (mediaArray == NULL) {
-      throw std::bad_alloc();
+      if (mediaArray != NULL) {
+          jint *jpcIndices = env->GetIntArrayElements(mediaArray, NULL);
+          if (jpcIndices != NULL) {
+              saveFormats = jpcIndices;
+              LPTSTR buf = NULL;
+              try {
+                  buf = (LPTSTR)new char[nPapers * sizeof(POINT)];
+              } catch (std::bad_alloc&) {
+                  buf = NULL;
     }
-
-    jboolean isCopy;
-    jint *jpcIndices = env->GetIntArrayElements(mediaArray,
-                                          &isCopy), *saveFormats = jpcIndices;
-
-    LPTSTR buf = (LPTSTR)new char[nPapers * sizeof(POINT)]; // array of POINTs
-
+              if (buf != NULL) {
     if (::DeviceCapabilities(printerName, printerPort,
                              DC_PAPERSIZE, buf, NULL) != -1) {
-
       POINT *pDim = (POINT *)buf;
       for (int i = 0; i < nPapers; i++) {
         jpcIndices[i*2] = (pDim+i)->x;
         jpcIndices[i*2+1] = (pDim+i)->y;
       }
     }
     RESTORE_CONTROLWORD
     delete[] buf;
+              }
     env->ReleaseIntArrayElements(mediaArray, saveFormats, 0);
+              saveFormats = NULL;
+          }
+      }
   }
 
   JNU_ReleaseStringPlatformChars(env, printer, printerName);
   JNU_ReleaseStringPlatformChars(env, port, printerPort);
+  if (mediaArray != NULL && saveFormats != NULL) {
+      env->ReleaseIntArrayElements(mediaArray, saveFormats, 0);
+  }
   return mediaArray;
 
-  CATCH_BAD_ALLOC_RET(NULL);
 }
 
 
 jobjectArray getAllDCNames(JNIEnv *env, jobject peer, jstring printer,
                  jstring port, unsigned int dc_id, unsigned int buf_len)
 {
-  TRY;
 
-  LPTSTR printerName = (LPTSTR)JNU_GetStringPlatformChars(env,
-                                                          printer, NULL);
+  LPTSTR printerName = (LPTSTR)JNU_GetStringPlatformChars(env, printer, NULL);
   LPTSTR printerPort = (LPTSTR)JNU_GetStringPlatformChars(env, port, NULL);
 
+  if (printerName == NULL || printerPort == NULL) {
+      if (printerName != NULL) {
+          JNU_ReleaseStringPlatformChars(env, printer, printerName);
+      }
+      if (printerPort != NULL) {
+          JNU_ReleaseStringPlatformChars(env, port, printerPort);
+      }
+      return NULL;
+  }
+
   jstring utf_str;
-  jclass cls = env->FindClass("java/lang/String");
-  jobjectArray names= NULL;
+  jobjectArray names = NULL;
   LPTSTR buf = NULL;
   SAVE_CONTROLWORD
   int cReturned = ::DeviceCapabilities(printerName, printerPort,
                                          dc_id, NULL, NULL);
   RESTORE_CONTROLWORD
-  if (cReturned > 0) {
+  if (cReturned <= 0) {
+      JNU_ReleaseStringPlatformChars(env, printer, printerName);
+      JNU_ReleaseStringPlatformChars(env, port, printerPort);
+      return NULL;
+  }
 
+  try {
     buf = (LPTSTR)new char[cReturned * buf_len * sizeof(TCHAR)];
+  } catch (std::bad_alloc&) {
+      buf = NULL;
+  }
     if (buf == NULL) {
-      throw std::bad_alloc();
+      JNU_ReleaseStringPlatformChars(env, printer, printerName);
+      JNU_ReleaseStringPlatformChars(env, port, printerPort);
+      JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
+     return NULL;
     }
 
     cReturned = ::DeviceCapabilities(printerName, printerPort,
                                      dc_id, buf, NULL);
     RESTORE_CONTROLWORD
 
+  JNU_ReleaseStringPlatformChars(env, printer, printerName);
+  JNU_ReleaseStringPlatformChars(env, port, printerPort);
+
     if (cReturned > 0) {
+      jclass cls = env->FindClass("java/lang/String");
+      if (cls != NULL) {
       names = env->NewObjectArray(cReturned, cls, NULL);
-      if (names == NULL) {
-        throw std::bad_alloc();
+      }
+      if (names == NULL || cls == NULL) {
+          delete buf;
+          return names;
       }
 
       for (int i = 0; i < cReturned; i++) {
         utf_str = JNU_NewStringPlatform(env, buf+(buf_len*i));
         if (utf_str == NULL) {
-          throw std::bad_alloc();
+                delete buf;
+                return names;
         }
         env->SetObjectArrayElement(names, i, utf_str);
         env->DeleteLocalRef(utf_str);
       }
     }
     delete[] buf;
-  }
   return names;
 
-  CATCH_BAD_ALLOC_RET(NULL);
 }
 
 
 JNIEXPORT jobjectArray JNICALL
 Java_sun_print_Win32PrintService_getAllMediaNames(JNIEnv *env,

@@ -538,10 +555,20 @@
                                                     jstring port)
 {
   LPTSTR printerName = (LPTSTR)JNU_GetStringPlatformChars(env, printer, NULL);
   LPTSTR printerPort = (LPTSTR)JNU_GetStringPlatformChars(env, port, NULL);
 
+  if (printerName == NULL || printerPort == NULL) {
+      if (printerName != NULL) {
+          JNU_ReleaseStringPlatformChars(env, printer, printerName);
+      }
+      if (printerPort != NULL) {
+          JNU_ReleaseStringPlatformChars(env, port, printerPort);
+      }
+      return 1;
+  }
+
   SAVE_CONTROLWORD
   int numCopies = ::DeviceCapabilities(printerName, printerPort,
                                        DC_COPIES,   NULL, NULL);
   RESTORE_CONTROLWORD
 

@@ -571,52 +598,62 @@
 Java_sun_print_Win32PrintService_getAllResolutions(JNIEnv *env,
                                                    jobject peer,
                                                    jstring printer,
                                                    jstring port)
 {
-  TRY;
-
   LPTSTR printerName = (LPTSTR)JNU_GetStringPlatformChars(env, printer, NULL);
   LPTSTR printerPort = (LPTSTR)JNU_GetStringPlatformChars(env, port, NULL);
 
-  jintArray resolutionArray = NULL;
+ if (printerName == NULL || printerPort == NULL) {
+      if (printerName != NULL) {
+          JNU_ReleaseStringPlatformChars(env, printer, printerName);
+      }
+      if (printerPort != NULL) {
+          JNU_ReleaseStringPlatformChars(env, port, printerPort);
+      }
+      return NULL;
+  }
 
   SAVE_CONTROLWORD
   int nResolutions = ::DeviceCapabilities(printerName, printerPort,
                                           DC_ENUMRESOLUTIONS, NULL, NULL);
   RESTORE_CONTROLWORD
+
+  jintArray resolutionArray = NULL;
   if (nResolutions > 0) {
     resolutionArray = env->NewIntArray(nResolutions*2);
-    if (resolutionArray == NULL) {
-      throw std::bad_alloc();
+    if (resolutionArray != NULL) {
+        jint *jpcIndices = env->GetIntArrayElements(resolutionArray, NULL);
+        if (jpcIndices != NULL) {
+            jint *saveFormats = jpcIndices;
+            LPTSTR resBuf = NULL;
+            try {
+                resBuf = (LPTSTR)new char[nResolutions * sizeof(LONG) * 2];
+            } catch (std::bad_alloc&) {
+                resBuf = NULL;
     }
-
-    jboolean isCopy;
-    jint *jpcIndices = env->GetIntArrayElements(resolutionArray,
-                                          &isCopy), *saveFormats = jpcIndices;
-
-    LPTSTR resBuf = (LPTSTR)new char[nResolutions * sizeof(LONG) * 2]; // pairs of long
-
+            if (resBuf != NULL) {
     if (::DeviceCapabilities(printerName, printerPort,
-                             DC_ENUMRESOLUTIONS, resBuf, NULL) != -1) {
-
+                                         DC_ENUMRESOLUTIONS, resBuf,
+                                         NULL) != -1) {
       LONG *pResolution = (LONG *)resBuf;
       for (int i = 0; i < nResolutions; i++) {
         jpcIndices[i*2] = *pResolution++;
         jpcIndices[i*2+1] = *pResolution++;
       }
     }
     RESTORE_CONTROLWORD
     delete[] resBuf;
+            }
     env->ReleaseIntArrayElements(resolutionArray, saveFormats, 0);
   }
+    }
+  }
 
   JNU_ReleaseStringPlatformChars(env, printer, printerName);
   JNU_ReleaseStringPlatformChars(env, printer, printerPort);
   return resolutionArray;
-
-  CATCH_BAD_ALLOC_RET(NULL);
 }
 
 
 static BOOL IsDCPostscript( HDC hDC )
 {

@@ -670,10 +707,11 @@
     AwtPrintControl::FindPrinter(printer, buffer, &cbBuf,
                                       &printerName, &printerPort);
   } catch (std::bad_alloc&) {
     delete [] buffer;
     JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
+    return NULL;
   }
 
   if (printerPort == NULL) {
     printerPort = TEXT("LPT1");
   }

@@ -690,10 +728,21 @@
                                                  jstring printer,
                                                  jstring port)
 {
   LPTSTR printerName = (LPTSTR)JNU_GetStringPlatformChars(env, printer, NULL);
   LPTSTR printerPort = (LPTSTR)JNU_GetStringPlatformChars(env, port, NULL);
+
+  if (printerName == NULL || printerPort == NULL) {
+      if (printerName != NULL) {
+          JNU_ReleaseStringPlatformChars(env, printer, printerName);
+      }
+      if (printerPort != NULL) {
+          JNU_ReleaseStringPlatformChars(env, port, printerPort);
+      }
+      return NULL;
+  }
+
   // 0x1000 is a flag to indicate that getCapabilities has already been called.
   // 0x0001 is a flag for color support and supported is the default.
   jint ret = 0x1001;
   DWORD dmFields;
 

@@ -759,32 +808,45 @@
                                                     jstring port)
 {
   HANDLE      hPrinter;
   LPDEVMODE   pDevMode = NULL;
 
-  TRY;
-
   LPTSTR printerName = (LPTSTR)JNU_GetStringPlatformChars(env, printer, NULL);
   LPTSTR printerPort = (LPTSTR)JNU_GetStringPlatformChars(env, port, NULL);
 
+  if (printerName == NULL || printerPort == NULL) {
+      if (printerName != NULL) {
+          JNU_ReleaseStringPlatformChars(env, printer, printerName);
+      }
+      if (printerPort != NULL) {
+          JNU_ReleaseStringPlatformChars(env, port, printerPort);
+      }
+      return NULL;
+  }
+
+  jint* defIndices = NULL;
   jintArray defaultArray = env->NewIntArray(NDEFAULT);
-  if (defaultArray == NULL) {
-      throw std::bad_alloc();
+  if (defaultArray != NULL) {
+      defIndices = env->GetIntArrayElements(defaultArray, NULL);
+  }
+  if (defIndices == NULL) {
+      JNU_ReleaseStringPlatformChars(env, printer, printerName);
+      JNU_ReleaseStringPlatformChars(env, port, printerPort);
+      return NULL;
   }
 
-  jboolean isCopy;
-  jint *defIndices = env->GetIntArrayElements(defaultArray,
-                                          &isCopy), *saveFormats = defIndices;
+  jint *saveFormats = defIndices;
 
-  for (int i=0; i<NDEFAULT; i++) {
-      defIndices[i]=GETDEFAULT_ERROR;
+  for (int i=0; i < NDEFAULT; i++) {
+      defIndices[i] = GETDEFAULT_ERROR;
   }
 
   /* Start by opening the printer */
   if (!::OpenPrinter(printerName, &hPrinter, NULL)) {
       env->ReleaseIntArrayElements(defaultArray, saveFormats, 0);
       JNU_ReleaseStringPlatformChars(env, printer, printerName);
+      JNU_ReleaseStringPlatformChars(env, port, printerPort);
       return defaultArray;
   }
 
   if (!AwtPrintControl::getDevmode(hPrinter, printerName, &pDevMode)) {
       /* if failure, cleanup and return failure */

@@ -792,10 +854,11 @@
           ::GlobalFree(pDevMode);
       }
       ::ClosePrinter(hPrinter);
       env->ReleaseIntArrayElements(defaultArray, saveFormats, 0);
       JNU_ReleaseStringPlatformChars(env, printer, printerName);
+      JNU_ReleaseStringPlatformChars(env, port, printerPort);
       return defaultArray;
   }
 
   /* Have seen one driver which reports a default paper id which is not
    * one of their supported paper ids. If what is returned is not

@@ -861,22 +924,19 @@
 
   if (pDevMode->dmFields & DM_COLOR) {
       defIndices[8] = pDevMode->dmColor;
   }
 
-
   GlobalFree(pDevMode);
   ::ClosePrinter(hPrinter);
 
   env->ReleaseIntArrayElements(defaultArray, saveFormats, 0);
 
   JNU_ReleaseStringPlatformChars(env, printer, printerName);
   JNU_ReleaseStringPlatformChars(env, port, printerPort);
 
   return defaultArray;
-
-  CATCH_BAD_ALLOC_RET(NULL);
 }
 
 
 JNIEXPORT jint JNICALL
 Java_sun_print_Win32PrintService_getJobStatus(JNIEnv *env,

@@ -889,10 +949,13 @@
     DWORD  cByteUsed;
     PRINTER_INFO_2 *pPrinterInfo = NULL;
     int ret=0;
 
     LPTSTR printerName = (LPTSTR)JNU_GetStringPlatformChars(env, printer, NULL);
+    if (printerName == NULL) {
+        return -1;
+    }
 
     // Start by opening the printer
     if (!::OpenPrinter(printerName, &hPrinter, NULL)) {
         JNU_ReleaseStringPlatformChars(env, printer, printerName);
         return -1;

@@ -957,17 +1020,19 @@
 static jfieldID getIdOfLongField(JNIEnv *env, jobject self,
                                  const char *fieldName) {
   jclass myClass = env->GetObjectClass(self);
   jfieldID fieldId = env->GetFieldID(myClass, fieldName, "J");
   DASSERT(fieldId != 0);
-
   return fieldId;
 }
 
 
 static inline HANDLE getHPrinter(JNIEnv *env, jobject self) {
   jfieldID fieldId = getIdOfLongField(env, self, HPRINTER_STR);
+  if (fieldId == (jfieldID)0) {
+      return (HANDLE)NULL;
+  }
   return (HANDLE)(env->GetLongField(self, fieldId));
 }
 
 
 JNIEXPORT jboolean JNICALL

@@ -977,10 +1042,13 @@
                                                jstring jobname)
 {
   HANDLE      hPrinter;
   DOC_INFO_1  DocInfo;
   LPTSTR printerName = (LPTSTR)JNU_GetStringPlatformChars(env, printer, NULL);
+  if (printerName == NULL) {
+      return false;
+  }
   DASSERT(jobname != NULL);
   LPTSTR lpJobName = (LPTSTR)JNU_GetStringPlatformChars(env, jobname, NULL);
   LPTSTR jname = _tcsdup(lpJobName);
   JNU_ReleaseStringPlatformChars(env, jobname, lpJobName);
 

@@ -1014,12 +1082,16 @@
     return false;
   }
 
   // store handle
   jfieldID fieldId = getIdOfLongField(env, peer, HPRINTER_STR);
+  if (fieldId == (jfieldID)0) {
+      return false;
+  } else {
   env->SetLongField(peer, fieldId, reinterpret_cast<jlong>(hPrinter));
   return true;
+  }
 }
 
 
 JNIEXPORT jboolean JNICALL
 Java_sun_print_Win32PrintJob_printRawData(JNIEnv *env,

@@ -1037,10 +1109,13 @@
     return false;
   }
 
   try {
     data=(jbyte *)env->GetPrimitiveArrayCritical(dataArray, 0);
+    if (data == NULL) {
+        return false;
+    }
 
     // Send the data to the printer.
     if( ! ::WritePrinter(hPrinter, data, count,(LPDWORD)&dwBytesWritten)) {
       env->ReleasePrimitiveArrayCritical(dataArray, data, 0);
       return false;