VC获取打印机与打印作业的状态
VC 获取打印机与打印作业的状态
#pragma comment(lib, "winspool.lib")
#include "stdafx.h"
#include <winspool.h>
#ifndef _MAX_PATH
#define _MAX_PATH 260
#endif
TCHAR m_tchPrinterMessage[_MAX_PATH];///< 打印结果的返回信息
/// 获取打印作业的状态
BOOL GetJobs(HANDLE hPrinter,
JOB_INFO_2 **ppJobInfo,
int *pcJobs,
DWORD *pStatus)
{
DWORD cByteNeeded, nReturned, cByteUsed;
JOB_INFO_2 *pJobStorage = NULL;
PRINTER_INFO_2 *pPrinterInfo = NULL;
if (!GetPrinter(hPrinter, 2, NULL, 0, &cByteNeeded))
{
DWORD dwErrorCode = ::GetLastError();
if (dwErrorCode != ERROR_INSUFFICIENT_BUFFER)
return FALSE;
}
pPrinterInfo = (PRINTER_INFO_2 *)malloc(cByteNeeded);
if (!(pPrinterInfo))
return FALSE;
if (!GetPrinter(hPrinter,
2,
(LPBYTE)pPrinterInfo,
cByteNeeded,
&cByteUsed))
{
free(pPrinterInfo);
pPrinterInfo = NULL;
return FALSE;
}
if (!EnumJobs(hPrinter,
0,
pPrinterInfo->cJobs,
2,
NULL,
0,
(LPDWORD)&cByteNeeded,
(LPDWORD)&nReturned))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
free(pPrinterInfo);
pPrinterInfo = NULL;
return FALSE;
}
}
pJobStorage = (JOB_INFO_2 *)malloc(cByteNeeded);
if (!pJobStorage)
{
free(pPrinterInfo);
pPrinterInfo = NULL;
return FALSE;
}
ZeroMemory(pJobStorage, cByteNeeded);
if (!EnumJobs(hPrinter,
0,
pPrinterInfo->cJobs,
2,
(LPBYTE)pJobStorage,
cByteNeeded,
(LPDWORD)&cByteUsed,
(LPDWORD)&nReturned))
{
free(pPrinterInfo);
free(pJobStorage);
pJobStorage = NULL;
pPrinterInfo = NULL;
return FALSE;
}
*pcJobs = nReturned;
*pStatus = pPrinterInfo->Status;
*ppJobInfo = pJobStorage;
free(pPrinterInfo);
return TRUE;
}
/// 检测打印机的状态
/// hPrinter表示打印机Handle
BOOL CheckPrinterStatus(HANDLE hPrinter)
{
BOOL bResult = PRINT_NOT_EXIST;
if(NULL == hPrinter)
{
_tcscpy(m_tchPrinterMessage,_TEXT("打印机不存在!"));
return bResult;
}
bResult = PRINT_FALSE;
JOB_INFO_2 *pJobs;
int cJobs,i;
DWORD dwPrinterStatus;
if (!GetJobs(hPrinter, &pJobs, &cJobs, &dwPrinterStatus))
{
_tcscpy(m_tchPrinterMessage,_TEXT("打印失败!"));
return bResult;
}
//
//if (dwPrinterStatus &
// (PRINTER_STATUS_ERROR | //打印出错
// PRINTER_STATUS_PAPER_JAM | //卡纸
// PRINTER_STATUS_PAPER_OUT |
// PRINTER_STATUS_PAPER_PROBLEM | //打印纸出现问题
// PRINTER_STATUS_OUTPUT_BIN_FULL | //打印输出已满
// PRINTER_STATUS_NOT_AVAILABLE | //打印机不可用
// PRINTER_STATUS_NO_TONER | //没有墨粉
// PRINTER_STATUS_OUT_OF_MEMORY | //打印内存出错
// PRINTER_STATUS_OFFLINE | //未联机
// PRINTER_STATUS_DOOR_OPEN)) //打印机的门是开的
//{
// free( pJobs );
// _tcscpy(m_tchPrinterMessage,_TEXT("打印出错!"));
// return bResult;
//}
/// 正在打印
if(dwPrinterStatus & PRINTER_STATUS_PRINTING)
{
_tcscpy(m_tchPrinterMessage,_TEXT("正在打印..."));
bResult = PRINT_GOING;
}
else if(dwPrinterStatus & PRINTER_STATUS_PAUSED)
{
free( pJobs );
_tcscpy(m_tchPrinterMessage,_TEXT("打印暂停中..."));
return bResult;
}
else if(dwPrinterStatus & PRINTER_STATUS_BUSY)
{
free( pJobs );
_tcscpy(m_tchPrinterMessage,_TEXT("打印机繁忙,/n被临时占用"));
return bResult;
}
else if(dwPrinterStatus & PRINTER_STATUS_WAITING)
{
free( pJobs );
_tcscpy(m_tchPrinterMessage,_TEXT("打印等待中..."));
return bResult;
}
else if(dwPrinterStatus & PRINTER_STATUS_SERVER_OFFLINE)
{
free( pJobs );
_tcscpy(m_tchPrinterMessage,_TEXT("未连接打印机服务器"));
return bResult;
}
else if(dwPrinterStatus & PRINTER_STATUS_ERROR)
{
free( pJobs );
_tcscpy(m_tchPrinterMessage,_TEXT("打印出错!"));
return bResult;
}
else if(dwPrinterStatus & PRINTER_STATUS_PAPER_JAM)
{
free( pJobs );
_tcscpy(m_tchPrinterMessage,_TEXT("打印机卡纸"));
return bResult;
}
else if(dwPrinterStatus & PRINTER_STATUS_PAPER_OUT)
{
free( pJobs );
_tcscpy(m_tchPrinterMessage,_TEXT("打印纸位置不正确"));
return bResult;
}
else if(dwPrinterStatus & PRINTER_STATUS_PAPER_PROBLEM)
{
free( pJobs );
_tcscpy(m_tchPrinterMessage,_TEXT("打印纸出现问题"));
return bResult;
}
else if(dwPrinterStatus & PRINTER_STATUS_OUTPUT_BIN_FULL)
{
free( pJobs );
_tcscpy(m_tchPrinterMessage,_TEXT("打印输出已满"));
return bResult;
}
else if(dwPrinterStatus & PRINTER_STATUS_NOT_AVAILABLE)
{
free( pJobs );
_tcscpy(m_tchPrinterMessage,_TEXT("打印机不可用"));
return bResult;
}
else if(dwPrinterStatus & PRINTER_STATUS_TONER_LOW)
{
free( pJobs );
_tcscpy(m_tchPrinterMessage,_TEXT("打印机墨粉不足"));
return bResult;
}
else if(dwPrinterStatus & PRINTER_STATUS_NO_TONER)
{
free( pJobs );
_tcscpy(m_tchPrinterMessage,_TEXT("打印机没有墨粉"));
return bResult;
}
else if(dwPrinterStatus & PRINTER_STATUS_OUT_OF_MEMORY)
{
free( pJobs );
_tcscpy
补充:软件开发 , Vc ,