使用 Win32 API 将原始数据发送到打印机

原文地址::https://docs.microsoft.com/zh-cn/troubleshoot/windows/win32/win32-raw-data-to-printer

相关文章

1、USB口的打印机接计算机,CreateFile函数的第一个参数该如何写----https://bbs.youkuaiyun.com/topics/370169850

2、USB 用CreateFile()打开usb设备时,如何填写devicepath这个值?----https://bbs.youkuaiyun.com/topics/392451132

3、USB接口打印机直接打印(无需驱动)----https://download.youkuaiyun.com/download/qq_34431829/10954749

 

文介绍如何使用 Win32 API 将原始数据发送到打印机。

原始产品版本:  Windows API
原始 KB 数:  138594

摘要

有时,不需要将打印机特定的数据直接发送到打印机,而是绕过驱动程序。 Win32 API 提供了它在本地和网络打印机上的工作方式。 此方法可用于替换 PASSTHROUGH SpoolFile() 在早期版本的 Windows API 中使用的转义和方法。

代码示例

您可以使用以下代码将原始数据直接发送到 Windows NT 或 Windows 95 中的打印机。

C++复制

// RawDataToPrinter - sends binary data directly to a printer
// Params:
//   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(LPSTR szPrinterName, LPBYTE lpData, DWORD dwCount)
{
    HANDLE     hPrinter;
    DOC_INFO_1 DocInfo;
    DWORD      dwJob;
    DWORD      dwBytesWritten;

    // Need a handle to the printer.
    if(!OpenPrinter( szPrinterName, &hPrinter, NULL))
    return FALSE;

    // Fill in the structure with info about this "document."
    DocInfo.pDocName = "My Document";
    DocInfo.pOutputFile = NULL;
    DocInfo.pDatatype = "RAW";
    // Inform the spooler the document is beginning.
    if((dwJob = StartDocPrinter(hPrinter, 1, (LPSTR)&DocInfo)) == 0)
    {
      ClosePrinter(hPrinter);
      return FALSE;
    }
    // Start a page.
    if(!StartPagePrinter(hPrinter))
    {
      EndDocPrinter(hPrinter);
      ClosePrinter(hPrinter);
      return FALSE;
    }
    // Send the data to the printer.
    if(!WritePrinter(hPrinter, lpData, dwCount, &dwBytesWritten))
    {
      EndPagePrinter(hPrinter);
      EndDocPrinter(hPrinter);
      ClosePrinter(hPrinter);
      return FALSE;
    }
    // End the page.
    if(!EndPagePrinter(hPrinter))
    {
      EndDocPrinter(hPrinter);
      ClosePrinter(hPrinter);
      return FALSE;
    }
    // Inform the spooler that the document is ending.
    if(!EndDocPrinter(hPrinter))
    {
      ClosePrinter(hPrinter);
      return FALSE;
    }
    // Tidy up the printer handle.
    ClosePrinter(hPrinter);
    // Check to see if correct number of bytes were written.
    if(dwBytesWritten != dwCount)
      return FALSE;
      return TRUE;
}

以下文件可从 Microsoft 下载中心下载:

RAWPRN.EXE

有关如何下载 Microsoft 支持文件的详细信息,请参阅如何从联机服务获取 Microsoft 支持文件

Microsoft 已对此文件进行病毒扫描。 Microsoft 使用了最新的病毒检测软件,该软件在文件发布之日起可用。 文件存储在安全增强的服务器上,可帮助防止对文件进行任何未经授权的更改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值