【发布时间】:2014-05-14 13:23:30
【问题描述】:
我正在尝试从打印机中删除队列中的所有文件。我找到了this piece of code,这看起来很简单。
我尝试使用以下代码删除队列。它可以编译,但 SetPrinter 返回 false。我收到的错误消息是 5,我尝试使用 from this question 方法将其解码为“正常”错误消息。但我无法编译,因为 STR_ELEMS 未定义。在 Google 上搜索“STR_ELEMS 未定义”,但遇到了死胡同。
有人可以帮我解码错误消息并删除打印机队列吗?
BOOL bStatus = false;
HANDLE hPrinter = NULL;
DOC_INFO_1 DocInfo;
bStatus = OpenPrinter((LPTSTR)_T("CN551A"), &hPrinter, NULL);
if(bStatus) {
DWORD dwBufsize=0;
GetPrinterA(hPrinter, 2, NULL, 0, &dwBufsize); // Edit: Returns false
PRINTER_INFO_2* pinfo = (PRINTER_INFO_2*)malloc(dwBufsize);
long result = GetPrinterA(hPrinter, 2,
(LPBYTE)pinfo, dwBufsize, &dwBufsize);
if ( pinfo->cJobs==0 ) // Edit: pinfo->cJobs is not 0
{
printf("No printer jobs found.");
}
else
{
if ( SetPrinter(hPrinter, 0, 0, PRINTER_CONTROL_PURGE)==0 )
printf("SetPrinter call failed: %x\n", GetLastError() );
else printf("Number of printer jobs deleted: %u\n",
pinfo->cJobs);
}
ClosePrinter( hPrinter );
}
我的包括:
#include <windows.h>
#include <winspool.h>
【问题讨论】:
-
GetPrinter返回什么?应该是BOOL,0表示失败。 -
实际返回false。没查那个。那么我是否以某种方式定义了错误的 hPrinter 句柄?
-
dwBufsize的值是多少?顺便说一句,没有理由mallocPRINTER_INFO_2结构。只需将其声明为非指针并将其地址传递给GetPrinter,如(LPBYTE)&pinfo。 -
如果我将其更改为 PRINTER_INFO2 pinfo,然后按照您的建议检索地址 &pinfo,那么我认为没有足够的可用内存(我收到异常错误)。如果我计算 dwBufSize 我得到 3080,我猜这是 pinfo 的字节数?
-
PRINTER_INFO_2是对的。但是有两个电话给GetPrinter。两次调用后打印出返回值和dwBufsize的值。
标签: c++ winapi printing error-handling