JNA - 获取倍数 OpenPrinter

JNA - Get multiples OpenPrinter

我需要一种帮助!

我必须获取 PrintJob(最后一个)的所有信息。 问题是:我公司的计算机每台 PC 有不止一台打印机。 所以我需要在 Spooler 级别监控不止一台打印机。 我已经尝试了很多逻辑,最后一个是:

Winspool.PRINTER_INFO_2[] printerInfo2 = WinspoolUtil.getPrinterInfo2();

    if (printerInfo2.length == 0)
        System.out.println( "Sem impressoas" );

    String[] printerNames = new String[printerInfo2.length];


    System.out.println();

    int arrayIndex = 0;
    for (Winspool.PRINTER_INFO_2 aPrinterInfo2 : printerInfo2) {
        printerNames[arrayIndex] = aPrinterInfo2.pPrinterName;

        //if (aPrinterInfo2.pPrinterName.startsWith( "\" )) {
        //printerNames[arrayIndex] = aPrinterInfo2.pPrinterName.substring( 17 );
        //}
        arrayIndex++;
    }
    //endregion


    HANDLEByReference handleByReference = new HANDLEByReference();
    boolean[] printersOpened = new boolean[printerNames.length];
    arrayIndex = 0;
    for (String printerName : printerNames) {
        printersOpened[arrayIndex] = Winspool.INSTANCE.OpenPrinter( printerName, handleByReference, null );

        if (!printersOpened[arrayIndex]) {
            getLastError();
        }
        arrayIndex++;
    }

然后我访问打印机:

WinNT.HANDLE chgObject = Winspool.INSTANCE.FindFirstPrinterChangeNotification( handleByReference.getValue(),
            Winspool.PRINTER_CHANGE_JOB, 0, null );

    if (chgObject != null) {
        while (true) {
            Kernel32.INSTANCE.WaitForSingleObject( chgObject, 1000 );
            WinDef.DWORDByReference pdwChange = new WinDef.DWORDByReference();
            boolean fcnreturn = Winspool.INSTANCE.FindNextPrinterChangeNotification( chgObject, pdwChange,
                    null, null );

            if (fcnreturn) {
                Winspool.JOB_INFO_1[] jobInfo1 = WinspoolUtil.getJobInfo1( handleByReference );

                for (Winspool.JOB_INFO_1 aJobInfo1 : jobInfo1) {
                    System.out.println( "-----------xx-----------" );
                    System.out.println( aJobInfo1.pDocument );
                    System.out.println( aJobInfo1.pPrinterName );
                    System.out.println( "-----------xx-----------" );
                }
                break;
            }
        }
    }

但我只能得到最后一个实例化的打印机...如果有任何方法可以通过 JNA 捕获用户选择的打印机,我仍然没有找到它。

所以我问:如何在不依赖打印机的情况下监控后台处理程序?

您需要使用 EnumPrinters 获取所有打印机名称并监视它们的所有更改。