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

Re: EnumJobs/GetJob problem... by Lucas

Lucas
Tue Dec 16 15:42:48 CST 2003

Why you use goto / malloc / free ?
Your code is problematic, under some circumstances you do double free and/or
free(0) and/or do not free the memory.
Use try...catch and/or auto_ptr

Lucas/

"Juvenal León" <anonymous@discussions.microsoft.com> wrote in message
news:00cc01c3c408$92576790$a501280a@phx.gbl...
Hello everybody,
I'm trying to monitor the Windows printing queue. It is
very importan for my app to gather
the paper size and if the printing is color or monochrome.
I call the following function
after the user has chosen the printer:




BOOL CEnumJobsDlg::GetQueue(HANDLE hPrinter)
{

DWORD cByteNeeded,
nReturned=0,
cByteUsed,
i;
JOB_INFO_2 *pJobStorage = NULL,
*pJob_Info_2=NULL;
PRINTER_INFO_2 *pPrinterInfo = NULL;



if (hPrinter)
{
/* Get the buffer size needed */
if (!GetPrinter(hPrinter, 2, NULL, 0,
&cByteNeeded))
{
if (GetLastError() !=
ERROR_INSUFFICIENT_BUFFER)
goto Fail;
}

pPrinterInfo = (PRINTER_INFO_2 *)malloc
(cByteNeeded);
if (!(pPrinterInfo))
/* failure to allocate memory */
goto Fail;

/* get the printer info */

if (!GetPrinter(hPrinter, 2, (LPBYTE)
pPrinterInfo, cByteNeeded, &cByteUsed))
{
// failure to access the printer
free(pPrinterInfo);
pPrinterInfo = NULL;
goto Fail;
}


/*At this point I can see the configuration of the
printer with valid data in
the pPrinterInfo->pDevMode pointer to the DEVMODE
structure.

*/

/* Get job storage space */
if (!EnumJobs(hPrinter,
0,
(pPrinterInfo)->cJobs,
2,
NULL,
0,
(LPDWORD)&cByteNeeded,
(LPDWORD)&nReturned))
{
if (GetLastError() !=
ERROR_INSUFFICIENT_BUFFER)
goto Fail;
}
pJobStorage = (JOB_INFO_2 *)malloc
(cByteNeeded);
if (!pJobStorage)
/* failure to allocate Job storage
space */
goto Fail;

ZeroMemory(pJobStorage, cByteNeeded);

/* get the list of jobs */
if (!EnumJobs(hPrinter,
0,
(pPrinterInfo)->cJobs,
2,
(LPBYTE)pJobStorage,
cByteNeeded,
(LPDWORD)&cByteUsed,
(LPDWORD)&nReturned))
{
goto Fail;
}

/* I send a print job with the Notepad.exe, I check the
information pointed by
pJobStorage debugging the execution, and I see the right
information (like the
document name, tha machine name, etc., I mean the
information in the JOB_INFO_2 structure)
but the pDevMode value is NULL this time.
*/
(pPrinterInfo)->cJobs = nReturned;

}
else
goto Fail;

/*Then I try to look at each individual print job with the
GetJob API function with the
following code, but the same: I cannot get de information
pointed by pDevMode because
its value is NULL. I wonder what is wrong in this code.
*/

for (i = 0; i < pPrinterInfo->cJobs; i++)
{
if (!GetJob(hPrinter,pJobStorage
[i].JobId,2,NULL,0,&cByteNeeded))
if (GetLastError() !=
ERROR_INSUFFICIENT_BUFFER)
goto Fail;

pJob_Info_2 = (JOB_INFO_2 *)malloc
(cByteNeeded);
ZeroMemory(pJob_Info_2, cByteNeeded);

if (!pJob_Info_2)
/* failure to allocate Job storage
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);

return TRUE;

Fail:

free(pJobStorage);
free(pPrinterInfo);
return FALSE;

}
/* end of function GetQueue */

Any ideas?

Thanks in advance.

Juvenal León