static HB_BOOL hb_GetJobs( HANDLE hPrinter, JOB_INFO_2 ** ppJobInfo, DWORD * pdwJobs ) { HB_BOOL bResult = HB_FALSE; DWORD dwNeeded = 0; GetPrinter( hPrinter, 2, NULL, 0, &dwNeeded ); if( dwNeeded ) { PRINTER_INFO_2 * pPrinterInfo = ( PRINTER_INFO_2 * ) hb_xgrabz( dwNeeded ); DWORD dwUsed = 0; if( GetPrinter( hPrinter, 2, ( LPBYTE ) pPrinterInfo, dwNeeded, &dwUsed ) ) { DWORD dwReturned = 0; EnumJobs( hPrinter, 0, pPrinterInfo->cJobs, 2, NULL, 0, &dwNeeded, &dwReturned ); if( dwNeeded ) { JOB_INFO_2 * pJobInfo = ( JOB_INFO_2 * ) hb_xgrabz( dwNeeded ); if( EnumJobs( hPrinter, 0, dwReturned, 2, ( LPBYTE ) pJobInfo, dwNeeded, &dwUsed, &dwReturned ) ) { *pdwJobs = dwReturned; *ppJobInfo = pJobInfo; bResult = HB_TRUE; } else hb_xfree( pJobInfo ); } } hb_xfree( pPrinterInfo ); } return bResult; }
static BOOL test_EnumJobs(struct torture_context *tctx, LPSTR printername, HANDLE handle) { DWORD levels[] = { 1, 2, 3, 4 }; DWORD success[] = { 1, 1, 1, 1 }; DWORD i; LPBYTE buffer = NULL; for (i=0; i < ARRAY_SIZE(levels); i++) { DWORD needed = 0; DWORD returned = 0; DWORD err = 0; char tmp[1024]; torture_comment(tctx, "Testing EnumJobs level %d", levels[i]); EnumJobs(handle, 0, 100, levels[i], NULL, 0, &needed, &returned); err = GetLastError(); if (err == ERROR_INSUFFICIENT_BUFFER) { err = 0; buffer = malloc(needed); torture_assert(tctx, buffer, "malloc failed"); if (!EnumJobs(handle, 0, 100, levels[i], buffer, needed, &needed, &returned)) { err = GetLastError(); } } if (err) { sprintf(tmp, "EnumJobs failed level %d on [%s] (buffer size = %d), error: %s\n", levels[i], printername, needed, errstr(err)); if (success[i]) { torture_fail(tctx, tmp); } else { torture_warning(tctx, tmp); } } free(buffer); buffer = NULL; } return TRUE; }
int main (int argc, char* argv[]) { HANDLE printer; DWORD returned, needed, i; PJOB_INFO_1 buffer1 = NULL; PJOB_INFO_2 buffer2 = NULL; if (argc < 2) { fprintf (stderr, "useage: %s <printername>\n", argv[0]); exit (-1); } /* open the printer */ if (!OpenPrinter (argv[1], &printer, NULL)) { fprintf (stderr, "Unable to open %s!\n", argv[1]); exit (-1); } else { printf ("Printer [%s] opened successfully.\n\n", argv[1]); } /* INFO LEVEL 1 */ EnumJobs(printer, START_JOB, MAX_JOBS, 1, (LPBYTE)buffer1, 0, &needed, &returned); if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { if (needed != 0) { fprintf (stderr, "Error getting job information for [%s] using NULL buffer.\n", argv[2]); exit (-1); } else { printf ("No jobs returned for [%s]\n", argv[1]); ClosePrinter (printer); exit (0); } } if ((buffer1 = (LPBYTE)malloc(needed)) == NULL) { fprintf (stderr, "Unable to malloc memory for JOB_INFO_1 buffer!\n"); exit (-1); } if (!EnumJobs(printer, START_JOB, MAX_JOBS, 1, (LPBYTE)buffer1, needed, &needed, &returned)) { fprintf (stderr, "Error getting form information for [%s].\nSize of buffer = %d\n", argv[2], needed); exit (-1); } printf ("Job Info Level 1:\n"); for (i=0; i<returned; i++) { print_job_info_1 (&buffer1[i]); } printf ("\n"); free (buffer1); /* INFO LEVEL 2 */ EnumJobs(printer, START_JOB, MAX_JOBS, 2, (LPBYTE)buffer2, 0, &needed, &returned); if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { if (needed != 0) { fprintf (stderr, "Error getting job information for [%s] using NULL buffer.\n", argv[2]); exit (-1); } else { printf ("No jobs returned for [%s]\n", argv[1]); ClosePrinter (printer); exit (0); } } if ((buffer2 = (LPBYTE)malloc(needed)) == NULL) { fprintf (stderr, "Unable to malloc memory for JOB_INFO_2 buffer!\n"); exit (-1); } if (!EnumJobs(printer, START_JOB, MAX_JOBS, 2, (LPBYTE)buffer2, needed, &needed, &returned)) { fprintf (stderr, "Error getting form information for [%s].\nSize of buffer = %d\n", argv[2], needed); exit (-1); } printf ("Job Info Level 2:\n"); for (i=0; i<returned; i++) { print_job_info_2 (&buffer2[i]); } printf ("\n"); free (buffer2); /* close the printer */ if (!ClosePrinter(printer)) { fprintf (stderr, "Error closing printer!\n"); exit (-1); } else { printf ("Printer [%s] closed successfully.\n", argv[1]); } return 0; }
BOOL EnumGeneric( IN PROC fnEnum, IN DWORD Level, IN PBYTE *ppEnumData, IN DWORD cbBuf, OUT LPDWORD pcbReturned, OUT LPDWORD pcReturned, IN PVOID Arg1, IN PVOID Arg2, IN PVOID Arg3 ) { BOOL rc; BOOL UnknownFunction = FALSE; DWORD cbRealloc; if( fnEnum == (PROC)EnumPrinters ) rc = EnumPrinters( (DWORD)Arg1, (LPTSTR)Arg2, COMMON_ARGS ); // Flags Name else if( fnEnum == (PROC)EnumJobs ) rc = EnumJobs( (HANDLE)Arg1, (DWORD)Arg2, (DWORD)Arg3, COMMON_ARGS ); // hPrinter FirstJob NoJobs else if( fnEnum == (PROC)EnumPrinterDrivers ) rc = EnumPrinterDrivers( (LPTSTR)Arg1, (LPTSTR)Arg2, COMMON_ARGS ); // pName pEnvironment else if( fnEnum == (PROC)EnumForms ) rc = EnumForms( (HANDLE)Arg1, COMMON_ARGS ); // hPrinter else if( fnEnum == (PROC)EnumMonitors ) rc = EnumMonitors( (LPTSTR)Arg1, COMMON_ARGS ); // pName else if( fnEnum == (PROC)EnumPorts ) rc = EnumPorts( (LPTSTR)Arg1, COMMON_ARGS ); // pName else if( fnEnum == (PROC)EnumPrintProcessors ) rc = EnumPrintProcessors( (LPTSTR)Arg1, (LPTSTR)Arg2, COMMON_ARGS ); // pName pEnvironment else if( fnEnum == (PROC)EnumPrintProcessorDatatypes ) rc = EnumPrintProcessorDatatypes( (LPTSTR)Arg1, (LPTSTR)Arg2, COMMON_ARGS ); // pName pPrintProcessorName else { *ppEnumData = NULL; UnknownFunction = TRUE; DBGMSG( DBG_ERROR, ( "EnumGeneric called with unknown function\n" ) ); rc = FALSE; } if( ( rc == FALSE ) && ( UnknownFunction == FALSE ) ) { if( GetLastError( ) == ERROR_INSUFFICIENT_BUFFER ) { cbRealloc = *pcbReturned; DBGMSG( DBG_TRACE, ( "EnumGeneric: Reallocating %d (0x%x) bytes @%08x\n", cbBuf, cbBuf, *ppEnumData ) ); if( cbBuf == 0 ) *ppEnumData = AllocSplMem( cbRealloc ); else *ppEnumData = ReallocSplMem( *ppEnumData, cbRealloc ); cbBuf = cbRealloc; if( *ppEnumData ) { DBGMSG( DBG_TRACE, ( "EnumGeneric: %d (0x%x) bytes reallocated @%08x\n", cbBuf, cbBuf, *ppEnumData ) ); if( fnEnum == (PROC)EnumPrinters ) rc = EnumPrinters( (DWORD)Arg1, (LPTSTR)Arg2, COMMON_ARGS ); // Flags Name else if( fnEnum == (PROC)EnumJobs ) rc = EnumJobs( (HANDLE)Arg1, (DWORD)Arg2, (DWORD)Arg3, COMMON_ARGS ); // hPrinter FirstJob NoJobs else if( fnEnum == (PROC)EnumPrinterDrivers ) rc = EnumPrinterDrivers( (LPTSTR)Arg1, (LPTSTR)Arg2, COMMON_ARGS ); // pName pEnvironment else if( fnEnum == (PROC)EnumForms ) rc = EnumForms( (HANDLE)Arg1, COMMON_ARGS ); // hPrinter else if( fnEnum == (PROC)EnumMonitors ) rc = EnumMonitors( (LPTSTR)Arg1, COMMON_ARGS ); // pName else if( fnEnum == (PROC)EnumPorts ) rc = EnumPorts( (LPTSTR)Arg1, COMMON_ARGS ); // pName else if( fnEnum == (PROC)EnumPrintProcessors ) rc = EnumPrintProcessors( (LPTSTR)Arg1, (LPTSTR)Arg2, COMMON_ARGS ); // pName pEnvironment else if( fnEnum == (PROC)EnumPrintProcessorDatatypes ) rc = EnumPrintProcessorDatatypes( (LPTSTR)Arg1, (LPTSTR)Arg2, COMMON_ARGS ); // pName pPrintProcessorName /* If things haven't worked out, free up the buffer. * We do this because otherwise the caller will not know * whether the pointer is valid any more, * since ReallocSplMem might have failed. */ if( rc == FALSE ) { if( *ppEnumData ) FreeSplMem( *ppEnumData ); *ppEnumData = NULL; *pcbReturned = 0; *pcReturned = 0; } /* Don't rely on pcbReturned having the same value * that was passed in: */ else *pcbReturned = cbRealloc; } } else { if( *ppEnumData ) FreeSplMem( *ppEnumData ); *ppEnumData = NULL; *pcbReturned = 0; *pcReturned = 0; rc = FALSE; } } else *pcbReturned = cbBuf; return rc; }