2017-08-09 63 views
0

我需要一種幫助!JNA - 獲取倍數OpenPrinter

我必須得到PrintJob(最後一個)的所有信息。 問題是:我公司的計算機每臺PC有多個打印機。 所以我需要在後臺打印程序級別監視多臺打印機。 我已經嘗試過這麼多的邏輯,最後一個是:

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,捕捉用戶選擇哪臺打印機,我還沒有找到它。

所以我問:如何監控假脫機程序而不依靠打印機?

回答

1

您需要使用EnumPrinters來獲取所有打印機名稱並監控它們的所有更改。