⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spoolutils.cpp

📁 虚拟打印机
💻 CPP
字号:
/* * * spoolutils.cpp * *   Copyright (C) 2006 Michael H. Overlin   This program is free software; you can redistribute it and/or modify   it under the terms of the GNU General Public License as published by   the Free Software Foundation; either version 2 of the License, or   (at your option) any later version.   This program is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.   You should have received a copy of the GNU General Public License   along with this program; if not, write to the Free Software   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA      Contact at poster_printer@yahoo.com */#include "spoolutils.h"#include "printResizerCommon.h"#include "utils.h"#include <strsafe.h>DWORD DoDeviceCapabilites(LPCTSTR lptstrPrinterName, PCDEVMODE pdm, WORD iDevCap, LPTSTR pvOutput) {	DWORD dwRetValue = -1;	::PPRINTER_INFO_2 pinfo = ::GetPrinterInfo2(lptstrPrinterName);	if (pinfo != NULL) {		dwRetValue = ::DeviceCapabilities(lptstrPrinterName, pinfo->pPortName, iDevCap, (LPTSTR) pvOutput, pdm);		free(pinfo);	}	return dwRetValue;}LONG DoDocumentProperties(	HWND hWnd,               // handle to parent window 	LPCTSTR pDeviceName,     // printer name	PDEVMODE pDevMode,       // devmode to serve as input or be validated	DWORD fMode              // mode options) {	LONG lRetValue = -1;	HANDLE hPrinter = NULL;	::OpenPrinter(const_cast<LPTSTR>(pDeviceName), &hPrinter, NULL);	if (hPrinter != NULL) {		PDEVMODE pdmIn = ( (fMode & DM_IN_BUFFER) != 0 ) ? pDevMode : NULL;		PDEVMODE pdmOut = ( (fMode & DM_OUT_BUFFER) != 0) ? pDevMode : NULL;		lRetValue = ::DocumentProperties(hWnd, hPrinter, const_cast<LPTSTR>(pDeviceName), pdmOut, pdmIn, fMode);		::ClosePrinter(hPrinter);	}	return lRetValue;}PJOB_INFO_2 GetJobInfo2(IN HANDLE hPrinter, IN DWORD dwJobID, OUT PDWORD pdwcBuff) {	PJOB_INFO_2 pjiRetValue = NULL;	DWORD dwcBuff = 0;	GetJob(hPrinter, dwJobID, 2, NULL, 0, &dwcBuff);	if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {		pjiRetValue = (PJOB_INFO_2) malloc(dwcBuff);		if (pjiRetValue != NULL) {			BOOL b = GetJob(hPrinter, dwJobID, 2, (LPBYTE) pjiRetValue, dwcBuff, &dwcBuff);			if (!b) {				free(pjiRetValue);				pjiRetValue = NULL;			}		}	}	if (pjiRetValue != NULL) {		*pdwcBuff = dwcBuff;	}	return pjiRetValue;}::PDRIVER_INFO_3 GetInstalledPrinterDrivers(OUT DWORD& dwcPrinterDrivers ) {	::PDRIVER_INFO_3 pinfo = NULL;	DWORD dwTemp;	DWORD dwCbNeeded;	::EnumPrinterDrivers(NULL, NULL, 3, NULL, 0, &dwCbNeeded, &dwTemp);	if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {		pinfo = (::PDRIVER_INFO_3) malloc(dwCbNeeded);		if (pinfo != NULL) {			if (! ::EnumPrinterDrivers(NULL, NULL, 3, (LPBYTE) pinfo, dwCbNeeded, &dwCbNeeded, &dwTemp)) {				free(pinfo);				pinfo = NULL;			} else {				dwcPrinterDrivers = dwTemp;			}		}	}	return pinfo;}PPRINTER_INFO_2 GetInstalledPrintersInfo2(OUT PDWORD pdwcCount) {	PPRINTER_INFO_2 pinfoRetValue = NULL;	DWORD dwcBuff = 0;	DWORD dwcEnum = 0;	BOOL bTemp = EnumPrinters(		PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, 		NULL, 2, NULL, 0, &dwcBuff, &dwcEnum		);	if (GetLastError() == ERROR_INSUFFICIENT_BUFFER || bTemp) {		pinfoRetValue = (PPRINTER_INFO_2) malloc(dwcBuff);		if (pinfoRetValue != NULL) {			BOOL b = EnumPrinters(				PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, 				NULL, 2, (LPBYTE) pinfoRetValue, dwcBuff, &dwcBuff, &dwcEnum				);			if (!b) {				free(pinfoRetValue);				pinfoRetValue = NULL;			} else {				*pdwcCount = dwcEnum;			}		}	}	return pinfoRetValue;}PDEVMODE GetPrinterDevMode(IN LPCTSTR lptstrPrinterName) {	PDEVMODE pdmRetValue = NULL;	PPRINTER_INFO_2 pinfo = GetPrinterInfo2(lptstrPrinterName);	if (pinfo != NULL) {		PDEVMODE pdm = pinfo->pDevMode;		if (pdm != NULL) {			DWORD dwcNeeded = pdm->dmSize + pdm->dmDriverExtra;			pdmRetValue = (PDEVMODE) malloc(dwcNeeded);			CopyMemory(pdmRetValue, pdm, dwcNeeded);		}		free(pinfo);	}	return pdmRetValue;}PPRINTER_INFO_2 GetPrinterInfo2(IN LPCTSTR lptstrPrinterName) {	PPRINTER_INFO_2 pinfoRetValue = NULL;	HANDLE hPrinter = NULL;	OpenPrinter((LPTSTR) lptstrPrinterName, &hPrinter, NULL);	if (hPrinter != NULL) {		pinfoRetValue = GetPrinterInfo2(hPrinter);		ClosePrinter(hPrinter);	}	return pinfoRetValue;}::PDRIVER_INFO_3 GetPrinterDriverInfo3(IN HANDLE hPrinter) {	::PDRIVER_INFO_3 pinfo = NULL;	DWORD dwCbNeeded;	::GetPrinterDriver(hPrinter, NULL, 3, 0, 0, &dwCbNeeded);	if ( ::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {		pinfo = (::PDRIVER_INFO_3) malloc(dwCbNeeded);		if (pinfo != NULL) {			if ( ! ::GetPrinterDriver(hPrinter, NULL, 3, (LPBYTE) pinfo, dwCbNeeded, &dwCbNeeded)) {				free(pinfo);				pinfo = NULL;			}		}	}	return pinfo;}PPRINTER_INFO_2 GetPrinterInfo2(IN HANDLE hPrinter) {	PPRINTER_INFO_2 pinfo = NULL;	DWORD dwcNeeded = 0;	GetPrinter(hPrinter, 2, NULL, 0, &dwcNeeded);	if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {		BOOL b;		pinfo = (PPRINTER_INFO_2) malloc(dwcNeeded);		b = GetPrinter(hPrinter, 2, (LPBYTE) pinfo, dwcNeeded, &dwcNeeded);		if (b) {			return pinfo;		}	}	if (pinfo != NULL) {		free(pinfo);	}			return NULL;}LPTSTR GetPrinterName(IN HANDLE hPrinter) {	LPTSTR lptstrRetValue = NULL;	PPRINTER_INFO_2 pinfo = GetPrinterInfo2(hPrinter);	if (pinfo != NULL) {		lptstrRetValue = ::StringDuplicate(pinfo->pPrinterName);		free(pinfo);	}	return lptstrRetValue;}// DOES NOT CHECK THAT PRINTER NAMED IS DOES NOT HAVE AS ITS DRIVER THIS DRIVERBOOL IsPrinterInstalled(IN LPCTSTR lptstrName) {	if (lptstrName != NULL) {		HANDLE hPrinter = NULL;		OpenPrinter((LPTSTR) lptstrName, &hPrinter, NULL);		if (hPrinter != NULL) {			ClosePrinter(hPrinter);			return TRUE;		}	}	return FALSE;}LPTSTR MyGetDefaultPrinter(void) {	LPTSTR lptstrRetValue = NULL;	DWORD dwCchNeeded = 0;	GetDefaultPrinter(NULL, &dwCchNeeded);	if ( ::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {		lptstrRetValue = (LPTSTR) malloc( dwCchNeeded * sizeof(TCHAR) );		if (lptstrRetValue != NULL) {			if (! GetDefaultPrinter(lptstrRetValue, &dwCchNeeded)) {				free(lptstrRetValue);				lptstrRetValue = NULL;			}		}	}	return lptstrRetValue;}LPTSTR MyGetPrinterDriverDirectory(void) {	LPTSTR lptstrRetValue = NULL;	DWORD dwCbNeeded;	::GetPrinterDriverDirectory(NULL, NULL, 1, 0, 0, &dwCbNeeded);	if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {		lptstrRetValue = (LPTSTR) malloc(dwCbNeeded);		if (lptstrRetValue != NULL) {			if ( ! ::GetPrinterDriverDirectory(NULL, NULL, 1, (LPBYTE) lptstrRetValue, dwCbNeeded, &dwCbNeeded)) {				free(lptstrRetValue);				lptstrRetValue = NULL;			}		}	} 	return lptstrRetValue;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -