设为首页 加入收藏

TOP

VC获取打印机与打印作业的状态(一)
2014-11-23 21:30:34 】 浏览:2126
Tags:获取 打印机 打印 作业 状态

VC 获取打印机与打印作业的状态
#pragma comment(lib, "winspool.lib")
#include "stdafx.h"
#include
#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(dwPri

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇VC资源文件版本解析类: (1) 头文.. 下一篇VC中创建DLL,导出全局变量,函数..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目