以下函数主要是使用API函数从打印机中获取打印任务,然后使用SetJob来取消删除任务,在使用OpenPrinter函数打开打印机时,需要在最后一个参数中指定权限为可PRINTER_ALL_ACCESS,否则会出现无操作权限
char szPrinterName[100];
BOOL bOK;
HANDLE hPrinter = NULL;
PRINTER_INFO_2 *pPrinterInfo = NULL;
DWORD dwReturned, dwNeeded, dwJobCount;
JOB_INFO_2 *pJobInfo;
dwSize = 100;
// default printer
GetProfileString("windows", "device", "", szPrinterName, dwSize);
// Printer name precedes first "," character...
strtok(szPrinterName, ",");
// get the printer handle
PRINTER_DEFAULTS Defaults = { NULL, NULL, PRINTER_ALL_ACCESS};
bOK = OpenPrinter(szPrinterName, &hPrinter, &Defaults );
if (!bOK)
return 1;
// get byte count needed for buffer
GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded);
// alocate the buffer
pPrinterInfo = new PRINTER_INFO_2[dwNeeded +5];
// get an information about the printer
bOK = GetPrinter(hPrinter, 2, (LPBYTE)pPrinterInfo, dwNeeded, &dwReturned);
if (!bOK)
return 1;
// alocate the structure
EnumJobs(hPrinter, 0, pPrinterInfo->cJobs, 2, NULL, 0,
(LPDWORD)&dwNeeded, (LPDWORD)&dwReturned);
pJobInfo = new JOB_INFO_2[dwNeeded];
// enum the jobs
if (!EnumJobs(hPrinter, 0, pPrinterInfo->cJobs, 2, (LPBYTE)pJobInfo,
dwNeeded, &dwReturned, &dwJobCount))
{
return 1;
}
// delete all jobs
for (DWORD i=0; i<dwJobCount; i++)
{
bOK = SetJob(hPrinter, pJobInfo[i].JobId, 2, (LPBYTE)pJobInfo, JOB_CONTROL_DELETE);
if (!bOK)
{
MessageBox(NULL, "Error", "Error", MB_OK);
}
}
阅读(2958) | 评论(0) | 转发(0) |