大网数据广告活动
打印与设备

How To: Send Data Directly to a GDI Printer

· 52CE 我爱测

The code sample later in this topic shows how to send printer control data directly to printers that use GDI-based printer drivers.

The code sample later in this topic shows how to send printer control data directly to printers that use GDI-based 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.

  1. Call OpenPrinter to get a handle to the printer.
  2. Initialize a DOCINFO structure with the printer data.
  3. Call StartDocPrinter to indicate that the application will be sending document data to the printer.
  4. Call StartPagePrinter to indicate that the application will be sending a new page to the printer.
  5. Call WritePrinter to send the data.
  6. Call EndPagePrinter to indicate that all data for the current page has been sent.
  7. Call EndDocPrinter to indicate that all data for this document has been sent.
  8. Call ClosePrinter to release the resources.

Send printer control data directly to printers that use GDI-based printer drivers.

// 
// RawDataToPrinter - sends binary data directly to a printer 
//  
// 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 RawDataToPrinter(LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount)
{
    BOOL     bStatus = FALSE;
    HANDLE     hPrinter = NULL;
    DOC_INFO_1 DocInfo;
    DWORD      dwJob = 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;
        DocInfo.pDatatype = (LPTSTR)_T("RAW");

        // Inform the spooler the document is beginning. 
        dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo );
        if (dwJob > 0) {
            // Start a page. 
            bStatus = StartPagePrinter( hPrinter );
            if (bStatus) {
                // Send the data to the printer. 
                bStatus = WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten);
                EndPagePrinter (hPrinter);
            }
            // Inform the spooler that the document is ending. 
            EndDocPrinter( hPrinter );
        }
        // Close the printer handle. 
        ClosePrinter( hPrinter );
    }
    // Check to see if correct number of bytes were written. 
    if (!bStatus || (dwBytesWritten != dwCount)) {
        bStatus = FALSE;
    } else {
        bStatus = TRUE;
    }
    return bStatus;
}

Related topics

ClosePrinter

EndDocPrinter

EndPagePrinter

OpenPrinter

StartDocPrinter

StartPagePrinter

WritePrinter

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 05/31/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。