Chinaunix首页 | 论坛 | 博客
  • 博客访问: 337225
  • 博文数量: 64
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 589
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-08 15:50
文章分类

全部博文(64)

文章存档

2015年(52)

2014年(3)

2013年(9)

我的朋友

分类: WINDOWS

2015-07-20 17:07:44

以下函数主要是使用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) |
给主人留下些什么吧!~~