Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/java.desktop/windows/native/libawt/windows/WPrinterJob.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, 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
Expand Down Expand Up @@ -133,12 +133,25 @@ static jobjectArray getPrinterNames(JNIEnv *env, DWORD flags) {
jobjectArray nameArray;

try {
::EnumPrinters(flags,
NULL, 4, NULL, 0, &cbNeeded, &cReturned);
pPrinterEnum = new BYTE[cbNeeded];
::EnumPrinters(flags,
NULL, 4, pPrinterEnum, cbNeeded, &cbNeeded,
&cReturned);
::EnumPrinters(flags, NULL, 4, NULL,
0, &cbNeeded, &cReturned);

BOOL bResult;
int nCount = 0;
do {
if (pPrinterEnum != NULL) {
delete [] pPrinterEnum;
}
pPrinterEnum = new BYTE[cbNeeded];

bResult = ::EnumPrinters(flags, NULL, 4, pPrinterEnum,
cbNeeded, &cbNeeded, &cReturned);
} while (!bResult && ++nCount < 5);

if (!bResult) {
// No printers in case of error
cReturned = 0;
}

if (cReturned > 0) {
nameArray = env->NewObjectArray(cReturned, clazz, NULL);
Expand Down