dart-windows / win32

Build Win32 apps with Dart!
https://win32.pub
BSD 3-Clause "New" or "Revised" License
735 stars 118 forks source link

ReadPrinter does not work any suggestion or example code please share #818

Closed sonal-maniya closed 4 months ago

sonal-maniya commented 4 months ago

     // HERE Open Printer - Works fine with status 1

     // Write Printer also works fine 

    // Read the response from the printer
    const bufferSize = 1024;
    final buffer = calloc<Uint8>(bufferSize);
    final bytesRead = calloc<DWORD>();

    final successRead = ReadPrinter(
      printerHandle.value,
      buffer.cast(),
      bufferSize,
      bytesRead,
    );

    if (successRead == 0) {
      final error = GetLastError();
      throw Exception('Failed to read print status, error: $error');
    }`
sonal-maniya commented 4 months ago

Is there any example code, please share

halildurmus commented 4 months ago

Can you also share the code you use to call the OpenPrinter function?

sonal-maniya commented 4 months ago

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;
  }
halildurmus commented 4 months ago

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!

sonal-maniya commented 4 months ago

Hi @halildurmus Thanks for the reply, but I already pass the handle that we are using in OpenPrinter.

sonal-maniya commented 4 months ago

The same question is posted here - https://stackoverflow.com/q/78080791/5766072 any update?

halildurmus commented 4 months ago

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.