Attempting to determine printer status always returns 0 for offline and online printers

My goal is to determine the current status of the printer. I found the following code . Here's a slightly modified version for fixing memory leaks and errors:

#include <Winspool.h>

int GetPrinterStatus( char* szPrnName )
{
  HANDLE          hHandle = 0;   // Handle of the printer

  DWORD           dwStatus = 0;  // Printer status we should receive

  DWORD           dwSize = 0;    // Size of memory we should
                                 // allocate for PRINTER_INFO_2

  PRINTER_INFO_2* pPrnInfo2 = 0; // Structure specifies detailed
                                 // printer information

  DEVMODE         DevMode = {0}; // Structure contains information
                                 // about the device initialization
                                 // and environment of a printer

  PRINTER_DEFAULTS PrnDef = { 0, &DevMode, PRINTER_ACCESS_USE };

  // Open printer with name szPrnName
  if( !OpenPrinter( szPrnName, &hHandle, &PrnDef ) )
    return -1; // Error

  // How many memory should be allocated for printer data?
  GetPrinter( hHandle, 2, 0, 0, &dwSize );
  if( !dwSize )
  {
    ClosePrinter( hHandle );
    return -1; // Error
  }

  // Allocate memory
  pPrnInfo2 = (PRINTER_INFO_2*)malloc( dwSize );

  // Receive printer details
  if(!GetPrinter( hHandle, 2, (LPBYTE)pPrnInfo2, dwSize, &dwSize ))
  {
    ClosePrinter( hHandle );
    free( pPrnInfo2 );
    return -1; // Error
  }

  dwStatus = pPrnInfo2->Status;

  // Free allocated memory
  free( pPrnInfo2 );

  // Close printer
  ClosePrinter( hHandle );

  return dwStatus;
}

So when I run it for this printer, this is offline:

enter image description here

Like this:

int status = GetPrinterStatus("POS58");

The resulting status 0, which is exactly the same as when I call it for a functional printer

Then I tried to replace the OpenPrintercall OpenPrinter2Wand use the parameter PRINTER_OPTION_NO_CACHE, but that did not help.

What am I doing wrong?

+4
source share
1 answer

(, ) , PRINTER_ATTRIBUTE_WORK_OFFLINE pPrnInfo2- > . . KB.

USB- (USBMON) USB-, ", " " ":

enter image description here

FYI, , Windows 10:

  • ( ) - "Offline"
  • - "" ( )
  • ", " - " "

:

  • PRINTER_STATUS_OFFLINE - .
  • JOB_STATUS_OFFLINE - ( )

, , , . , , PRINTER_ATTRIBUTE_WORK_OFFLINE, , Epson PRINTER_STATUS_NOT_AVAILABLE.

+4

Source: https://habr.com/ru/post/1665579/


All Articles