If the printer supports PostScript directly you can spool a raw print jobs like this:
HANDLE ph;
OpenPrinter(&ph, "Printer Name", NULL);
di1.pDatatype = IsV4Driver("Printer Name") ? "XPS_PASS" : "RAW";
di1.pDocName = "Raw print document";
di1.pOutputFile = NULL;
StartDocPrinter(ph, 1, (LPBYTE)&di1);
StartPagePrinter(ph);
WritePrinter(ph, buffer, dwRead, &dwWritten);
EndPagePrinter(ph);
EndDocPrinter(ph)
Repeat the WritePrinter until you have spooled the whole file.
IsV4Driver() Checks for version 4 drivers, this is necessary in Windows 8 and Server 2012:
bool IsV4Driver(wchar_t* printerName)
{
HANDLE handle;
PRINTER_DEFAULTS defaults;
defaults.DesiredAccess = PRINTER_ACCESS_USE;
defaults.pDatatype = L"RAW";
defaults.pDevMode = NULL;
if (::OpenPrinter(printerName, &handle, &defaults) == 0)
{
return false;
}
DWORD version = GetVersion(handle);
ClosePrinter(handle);
return version == 4;
}
DWORD GetVersion(HANDLE handle)
{
DWORD needed;
GetPrinterDriver(handle, NULL, 2, NULL, 0, &needed);
std::vector<char> buffer(needed);
return ((DRIVER_INFO_2*) &buffer[0])->cVersion;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…