Hello everybody,
I'm trying to monitor the Windows printing queue. It is=20
very importan for my app to gather=20
the paper size and if the printing is color or monochrome.=20
I call the following function=20
after the user has chosen the printer:
BOOL CEnumJobsDlg::GetQueue(HANDLE hPrinter)
{
DWORD cByteNeeded,=20
nReturned=3D0,=20
cByteUsed,=20
i;
JOB_INFO_2 *pJobStorage =3D NULL,=20
*pJob_Info_2=3DNULL;
PRINTER_INFO_2 *pPrinterInfo =3D NULL;
if (hPrinter)
{
/* Get the buffer size needed */
if (!GetPrinter(hPrinter, 2, NULL, 0,=20
&cByteNeeded))
{
if (GetLastError() !=3D=20
ERROR_INSUFFICIENT_BUFFER)
goto Fail;
}
pPrinterInfo =3D (PRINTER_INFO_2 *)malloc
(cByteNeeded);
if (!(pPrinterInfo))
/* failure to allocate memory */
goto Fail;
=09
/* get the printer info */
if (!GetPrinter(hPrinter, 2, (LPBYTE)
pPrinterInfo, cByteNeeded, &cByteUsed))
{
// failure to access the printer=20
free(pPrinterInfo);
pPrinterInfo =3D NULL;
goto Fail;
}=20
/*At this point I can see the configuration of the=20
printer with valid data in=20
the pPrinterInfo->pDevMode pointer to the DEVMODE=20
structure.
*/
=09
/* Get job storage space */
if (!EnumJobs(hPrinter,=20
0,=20
(pPrinterInfo)->cJobs,=20
2,=20
NULL,=20
0,
(LPDWORD)&cByteNeeded,
(LPDWORD)&nReturned))
{
if (GetLastError() !=3D=20
ERROR_INSUFFICIENT_BUFFER)
goto Fail;
}
pJobStorage =3D (JOB_INFO_2 *)malloc
(cByteNeeded);
if (!pJobStorage)
/* failure to allocate Job storage=20
space */
goto Fail;
=09
ZeroMemory(pJobStorage, cByteNeeded);
/* get the list of jobs */
if (!EnumJobs(hPrinter,=20
0,=20
(pPrinterInfo)->cJobs,=20
2,=20
(LPBYTE)pJobStorage,=20
cByteNeeded,
(LPDWORD)&cByteUsed,
(LPDWORD)&nReturned))
{
goto Fail;
}
/* I send a print job with the Notepad.exe, I check the=20
information pointed by
pJobStorage debugging the execution, and I see the right=20
information (like the=20
document name, tha machine name, etc., I mean the=20
information in the JOB_INFO_2 structure)=20
but the pDevMode value is NULL this time.=20
*/
(pPrinterInfo)->cJobs =3D nReturned;
}
else
goto Fail;
/*Then I try to look at each individual print job with the=20
GetJob API function with the=20
following code, but the same: I cannot get de information=20
pointed by pDevMode because=20
its value is NULL. I wonder what is wrong in this code.
*/
=09
for (i =3D 0; i < pPrinterInfo->cJobs; i++)
{
if (!GetJob(hPrinter,pJobStorage
[i].JobId,2,NULL,0,&cByteNeeded))
if (GetLastError() !=3D=20
ERROR_INSUFFICIENT_BUFFER)
goto Fail;
pJob_Info_2 =3D (JOB_INFO_2 *)malloc
(cByteNeeded);
ZeroMemory(pJob_Info_2, cByteNeeded);
if (!pJob_Info_2)
/* failure to allocate Job storage=20
space */
goto Fail;
if (!GetJob(hPrinter,pJobStorage
[i].JobId,2,LPBYTE(pJob_Info_2),cByteNeeded,&nReturned))
goto Fail;
}
free(pJob_Info_2);
free(pJobStorage);
free(pPrinterInfo);
=09
return TRUE;
Fail:
free(pJobStorage);
free(pPrinterInfo);
return FALSE;
}=20
/* end of function GetQueue */
Any ideas?
Thanks in advance.
Juvenal Le=F3n