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;
DWORD dwStatus = 0;
DWORD dwSize = 0;
PRINTER_INFO_2* pPrnInfo2 = 0;
DEVMODE DevMode = {0};
PRINTER_DEFAULTS PrnDef = { 0, &DevMode, PRINTER_ACCESS_USE };
if( !OpenPrinter( szPrnName, &hHandle, &PrnDef ) )
return -1;
GetPrinter( hHandle, 2, 0, 0, &dwSize );
if( !dwSize )
{
ClosePrinter( hHandle );
return -1;
}
pPrnInfo2 = (PRINTER_INFO_2*)malloc( dwSize );
if(!GetPrinter( hHandle, 2, (LPBYTE)pPrnInfo2, dwSize, &dwSize ))
{
ClosePrinter( hHandle );
free( pPrnInfo2 );
return -1;
}
dwStatus = pPrnInfo2->Status;
free( pPrnInfo2 );
ClosePrinter( hHandle );
return dwStatus;
}
So when I run it for this printer, this is offline:

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?
source
share