description | ms.assetid | title | ms.topic | ms.date |
---|---|---|---|---|
This topic describes how to send printer control data directly to printers that use XPSDrv printer drivers. |
7e98e08a-d572-451d-befb-18fc86f0e505 |
How To: Send Data Directly to an XPS Printer |
article |
05/31/2018 |
This topic describes how to send printer control data directly to printers that use XPSDrv printer drivers.
The following steps describe how to send data directly to a printer. These steps are also illustrated in the code example that follows.
- Call OpenPrinter to get a handle to the printer.
- Initialize a DOCINFO structure with the printer data.
- Call StartDocPrinter to indicate that the application will be sending document data to the printer.
- Call WritePrinter to send the data.
- Call EndDocPrinter to indicate that all data for this document has been sent.
- Call ClosePrinter to release the resources.
Send printer control data directly to printers that use XPSDrv printer drivers.
//
// RawDataToXpsPrinter - sends binary data directly to a printer
// with an XPSDrv Printer Driver
//
// szPrinterName: NULL-terminated string specifying printer name
// lpData: Pointer to raw data bytes
// dwCount Length of lpData in bytes
//
// Returns: TRUE for success, FALSE for failure.
//
BOOL RawDataToXpsPrinter (LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount)
{
BOOL bStatus = FALSE;
HANDLE hPrinter = NULL;
DOC_INFO_1 DocInfo;
DWORD dwPrtJob = 0L;
DWORD dwBytesWritten = 0L;
// Open a handle to the printer.
bStatus = OpenPrinter (szPrinterName, &hPrinter, NULL);
if (bStatus) {
// Fill in the structure with info about this "document."
DocInfo.pDocName = (LPTSTR)_T("My Document");
DocInfo.pOutputFile = NULL;
// Enter the datatype of this buffer.
// Use "XPS_PASS" when the data buffer should bypass the
// print filter pipeline of the XPSDrv printer driver.
// This datatype would be used to send the buffer directly
// to the printer, such as when sending print head alignment
// commands. Normally, a data buffer would be sent as the
// "RAW" datatype.
//
DocInfo.pDatatype = (LPTSTR)_T("XPS_PASS");
dwPrtJob = StartDocPrinter (
hPrinter,
1,
(LPBYTE)&DocInfo);
if (dwPrtJob > 0) {
// Send the data to the printer.
bStatus = WritePrinter (
hPrinter,
lpData,
dwCount,
&dwBytesWritten);
}
EndDocPrinter (hPrinter);
// Close the printer handle.
bStatus = ClosePrinter(hPrinter);
}
if (!bStatus || (dwCount != dwBytesWritten)) {
bStatus = FALSE;
} else {
bStatus = TRUE;
}
return bStatus;
}