gpt4 book ai didi

java - JNA - 获取多个 OpenPrinter

转载 作者:行者123 更新时间:2023-12-02 12:23:40 24 4
gpt4 key购买 nike

我需要帮助!

我必须获取 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 捕获用户选择的打印机,我仍然没有找到它。

所以我问:如何在不依赖打印机的情况下监控假脱机程序?

最佳答案

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

关于java - JNA - 获取多个 OpenPrinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45593334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com