Closed sonal-maniya closed 4 months ago
Is there any example code, please share
Can you also share the code you use to call the OpenPrinter
function?
I use this code - https://github.com/dart-windows/win32/blob/main/example/printer_raw.dart My Printer is this one - https://www.bocasystems.com/sample.htm
Future<String> readFromPrinterAsync(
String printerName, int bufferSize) async {
final completer = Completer<String>();
// Start a new isolate to perform the blocking operation
final phPrinter = calloc<HANDLE>();
await Isolate.spawn((_) {
try {
// Open the printer
final pPrinterName = printerName.toNativeUtf16();
final success = OpenPrinter(pPrinterName, phPrinter, nullptr);
if (success == 0) {
completer.completeError(
WindowsException(HRESULT_FROM_WIN32(GetLastError())));
return;
}
final pReadBuf = calloc<Uint8>(bufferSize);
final pBytesRead = calloc<Uint32>();
const command = '<S92>'; // Command to read print status
final res = _printRawData(phPrinter, command);
if (!res) {
throw Exception('Failed to send command to read print status');
}
try {
// Read from the printer
final success =
ReadPrinter(phPrinter.value, pReadBuf, bufferSize, pBytesRead);
if (success == 0) {
completer.completeError(
WindowsException(HRESULT_FROM_WIN32(GetLastError())));
return;
}
// Convert the read buffer to a Dart string
final readData = String.fromCharCodes(
pReadBuf.cast<Uint8>().asTypedList(pBytesRead.value));
completer.complete(readData);
} finally {
calloc.free(pReadBuf);
calloc.free(pBytesRead);
}
} finally {
// Close the printer handle
if (phPrinter.value != NULL) {
ClosePrinter(phPrinter.value);
}
calloc.free(phPrinter);
}
}, null);
return completer.future;
}
The documentation for the ReadPrinter function says:
hPrinter [in] A handle to the printer object for which to retrieve data. Use the OpenPrinter function to retrieve a printer object handle. Use the format: Printername, Job xxxx.
This implies that you must provide both the name of the printer and a job number when calling the OpenPrinter
function. It's unclear whether this job number is something you define or a value obtained from another API call.
I also couldn't find an example online that uses ReadPrinter
, sorry!
Hi @halildurmus Thanks for the reply, but I already pass the handle that we are using in OpenPrinter.
The same question is posted here - https://stackoverflow.com/q/78080791/5766072 any update?
I'm sorry, I can't help you as I don't have access to a printer that supports bi-directional communication.
I'm turning this into a discussion as I don't believe there is a problem with package:win32
.