|
| 1 | +/* Copyright (c) 2010 Daniel Doubrovkine, All Rights Reserved |
| 2 | + * |
| 3 | + * The contents of this file is dual-licensed under 2 |
| 4 | + * alternative Open Source/Free licenses: LGPL 2.1 or later and |
| 5 | + * Apache License 2.0. (starting with JNA version 4.0.0). |
| 6 | + * |
| 7 | + * You can freely decide which license you want to apply to |
| 8 | + * the project. |
| 9 | + * |
| 10 | + * You may obtain a copy of the LGPL License at: |
| 11 | + * |
| 12 | + * http://www.gnu.org/licenses/licenses.html |
| 13 | + * |
| 14 | + * A copy is also included in the downloadable source code package |
| 15 | + * containing JNA, in file "LGPL2.1". |
| 16 | + * |
| 17 | + * You may obtain a copy of the Apache License at: |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/ |
| 20 | + * |
| 21 | + * A copy is also included in the downloadable source code package |
| 22 | + * containing JNA, in file "AL2.0". |
| 23 | + */ |
| 24 | +package com.sun.jna.platform.win32; |
| 25 | + |
| 26 | +import com.sun.jna.Memory; |
| 27 | +import com.sun.jna.Native; |
| 28 | +import com.sun.jna.Structure; |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * @author dblock[at]dblock[dot]org |
| 35 | + */ |
| 36 | +public class WinspoolDummyTest { |
| 37 | + |
| 38 | + private static final String EVERYONE = "S-1-1-0"; |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testNoDuplicateMethodsNames() { |
| 42 | + // Get a printer name from Java to use |
| 43 | + //String printer = PrintServiceLookup.lookupDefaultPrintService().getName(); |
| 44 | + String printer = "Microsoft Print to PDF"; |
| 45 | + |
| 46 | + // Get custom Winspool2 instance |
| 47 | + Winspool winspool = Winspool.INSTANCE; |
| 48 | + |
| 49 | + // Get printer handle from Winspool |
| 50 | + WinNT.HANDLEByReference hPrinter = new WinNT.HANDLEByReference(); |
| 51 | + boolean success = winspool.OpenPrinter(printer, hPrinter, null); |
| 52 | + System.out.println("OpenPrinter status: " + success); |
| 53 | + if (getLastError() || !success) { |
| 54 | + System.err.println("Can't open printer, stopping"); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + // Get DEVMODE size |
| 59 | + int bufferSize = winspool.DocumentProperties(null, hPrinter.getValue(), printer, null, null, 0); |
| 60 | + |
| 61 | + if (getLastError() || bufferSize == 0) { |
| 62 | + System.err.println("Problem calling DocumentProperties"); |
| 63 | + } else { |
| 64 | + System.out.println("The DEVMODE size must be " + bufferSize); |
| 65 | + Memory devmodeBuffer = new Memory(bufferSize); |
| 66 | + int status = winspool.DocumentProperties(null, hPrinter.getValue(), printer, devmodeBuffer, null, 6 /* DM_OUT_BUFFER | DM_PROMPT */); |
| 67 | + |
| 68 | + WinGDI.DEVMODE dm = Structure.newInstance(WinGDI.DEVMODE.class, devmodeBuffer); |
| 69 | + dm.read(); |
| 70 | + |
| 71 | + System.out.println(dm); |
| 72 | + System.out.printf("Device: %s%n", dm.getDmDeviceName()); |
| 73 | + System.out.printf("Formname: %s%n", dm.getDmFormName()); |
| 74 | + if ((dm.dmFields & WinGDI.DM_ORIENTATION) == WinGDI.DM_ORIENTATION |
| 75 | + || (dm.dmFields & WinGDI.DM_PAPERSIZE) == WinGDI.DM_PAPERSIZE |
| 76 | + || (dm.dmFields & WinGDI.DM_PAPERLENGTH) == WinGDI.DM_PAPERLENGTH |
| 77 | + || (dm.dmFields & WinGDI.DM_PAPERWIDTH) == WinGDI.DM_PAPERWIDTH |
| 78 | + || (dm.dmFields & WinGDI.DM_COPIES) == WinGDI.DM_COPIES) { |
| 79 | + dm.dmUnion1.setType("dummystructname"); |
| 80 | + dm.dmUnion1.read(); |
| 81 | + if((dm.dmFields & WinGDI.DM_ORIENTATION) == WinGDI.DM_ORIENTATION) { |
| 82 | + System.out.printf("Orientientation: %d%n", dm.dmUnion1.dummystructname.dmOrientation); |
| 83 | + } |
| 84 | + if((dm.dmFields & WinGDI.DM_PAPERSIZE) == WinGDI.DM_PAPERSIZE) { |
| 85 | + System.out.printf("Paper size: %d%n", dm.dmUnion1.dummystructname.dmPaperSize); |
| 86 | + } |
| 87 | + if((dm.dmFields & WinGDI.DM_PAPERLENGTH) == WinGDI.DM_PAPERLENGTH) { |
| 88 | + System.out.printf("Paper length: %d%n", dm.dmUnion1.dummystructname.dmPaperLength); |
| 89 | + } |
| 90 | + if((dm.dmFields & WinGDI.DM_PAPERWIDTH) == WinGDI.DM_PAPERWIDTH) { |
| 91 | + System.out.printf("Paper width: %d%n", dm.dmUnion1.dummystructname.dmPaperWidth); |
| 92 | + } |
| 93 | + if((dm.dmFields & WinGDI.DM_COPIES) == WinGDI.DM_COPIES) { |
| 94 | + System.out.printf("Copies: %d%n", dm.dmUnion1.dummystructname.dmCopies); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // TODO: This may throw false error |
| 99 | + long IDOK = 1; |
| 100 | + if (getLastError()) { |
| 101 | + System.err.println("Problem calling DocumentProperties"); |
| 102 | + } |
| 103 | + |
| 104 | + if (status == IDOK) { |
| 105 | + System.out.println("The call to DocumentProperties returned IDOK"); |
| 106 | + } else { |
| 107 | + // API states a negative value is equivalent to an error |
| 108 | + System.err.println("The call to DocumentProperties returned " + status); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // Close printer handle |
| 113 | + winspool.INSTANCE.ClosePrinter(hPrinter.getValue()); |
| 114 | + } |
| 115 | + |
| 116 | + private static boolean getLastError() { |
| 117 | + int error = Native.getLastError(); |
| 118 | + if (error == 0) { |
| 119 | + System.out.println(" OK"); |
| 120 | + return false; |
| 121 | + } else { |
| 122 | + System.out.println(" ERROR: " + error); |
| 123 | + } |
| 124 | + return true; |
| 125 | + } |
| 126 | +} |
0 commit comments