绕过 Windows 后台处理程序,直接将原始数据打印到 USB 打印机

发布于 2024-11-14 23:41:24 字数 633 浏览 4 评论 0原文

我正在尝试 Zebra TTP8200热敏打印机。对于我的应用程序,我需要连续打印绘图仪类型跟踪,直到用户按下停止按钮。我尝试过 ZPL 语言,我可以成功生成位图数据,并通过将 ZPL 作为原始数据输出,一次转储一行(或几行)位图。

我正在使用一些 Microsoft 演示代码 将原始数据输出到打印机,这效果很好,但有一个问题:假脱机程序。事实证明,每次我使用 MS rawprn.exe 代码输出一些数据时,它实际上都会作为打印作业进行假脱机处理,然后传输到打印机。这需要长达 10 秒的时间才能通过后台处理程序,显然太慢了。在驱动程序中禁用假脱机功能并没有帮助,它只是意味着在作业通过假脱机程序并完成打印时程序挂起。

有没有办法绕过假脱机程序并将数据直接输出到此 USB 打印机?到目前为止,我的研究还没有发现任何可能在 Windows API 中查找的内容。理想情况下,我希望能够像使用串行打印机一样使用打印机 - 打开端口并将数据推入。

提前非常感谢您提供任何提示!

I'm experimenting with a Zebra TTP8200 thermal printer. For my application I need to print plotter type traces continuously until the user hits a stop button. I've had a play with the ZPL language and I can successfully generate bitmap data and dump out my bitmap a line (or few lines) at a time by outputting the ZPL as raw data.

I'm using some Microsoft demo code to output the raw data to the printer and this works great, bar one issue: the spooler. It turns out that every time I output some data using the MS rawprn.exe code it is actually spooled as a print job and then transmitted to the printer. This takes up to 10 seconds to get through the spooler, obviously too slow. Disabling spooling in the driver doesn't help, it just means that the program hangs while the job is passed through the spooler and printing completes.

Is there a way to bypass the spooler and output data straight to this USB printer? My research so far hasn't turned up anything likely looking in the Windows API. Ideally, I'd like to be able to use the printer like it was a serial printer - open the port and shove data in.

Many thanks in advance for any hints!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

勿忘初心 2024-11-21 23:41:24

如果 USB 打印机可用作 COM 端口,则只需写入 COM 端口即可。像这样,在 DOS 提示符下:

dir > com1

前一个示例将 dir 命令的结果输出到打印机。

或者,这是另一个示例:

copy file.txt com1

前一个示例将 file.txt 的内容输出到打印机。

输出格式正确的 ZPL 数据比纯文本更困难。不过,我已经使用 Ruby(和 Epson/ESC 命令)在 Linux 上实现了这一点。

If the USB printer is available as a COM port, you can just write to the COM port. Like this, from the DOS prompt:

dir > com1

The former example will output the results of the dir command to the printer.

Or, here is another example:

copy file.txt com1

The former example will output the contents of file.txt to the printer.

Outputting properly formatted ZPL data will be harder than just plain text. I have gotten this to work from Linux using Ruby (and Epson/ESC commands), however.

心清如水 2024-11-21 23:41:24

有没有办法绕过假脱机程序并直接向此输出数据
USB打印机?

是的,绝对是。它内置于大多数操作系统中,通过 USB 打印原始数据比以太网和 COM/LPT 稍微不那么明显。请注意,许多应用程序(例如记事本)无法打印原始数据,因此您的应用程序也需要支持这一点。

  1. 为您的 USB 打印机安装适当的驱动程序。检查打印机属性以查看它正在使用哪个 USB 端口。它可能是 USB001 等。
  2. 使用设备和打印机,添加第二台打印机。本地端口,选择刚刚创建的端口(即USB001) 注意:某些版本的Windows 有一个自动检测复选框,如果有,请取消选中它。
  3. 制造商:Generic,打印机:Generic / 仅文本
  4. 使用当前安装的驱动程序
  5. 为打印机指定一个名称,以区别于已创建的打印机,即 Zebra TTP8200 Raw。
  6. 不与原始打印应用程序共享
  7. ,不打印测试页,

立即完成,使用新创建的打印机。

聚苯乙烯
这些说明也可在此处获取,并带有屏幕截图,作为 Java 开源原始打印教程的一部分。该项目还提供其他平台(Ubuntu、OS X)的教程。

http://qzindustries.com/TutorialRawWin

-Tres

Is there a way to bypass the spooler and output data straight to this
USB printer?

Yes, absolutely. It's built into most OSs, printing raw via USB is just a bit less obvious than Ethernet and COM/LPT. Note, many applications, such as notepad are incapable of printing raw, so your application needs to support this as well.

  1. Install the appropriate driver for your USB printer. Check the printer properties to see which USB port it is using. It may be USB001, etc.
  2. Using the Devices and Printers, add a second printer. Local Port, Select the port just created (i.e. USB001) Note: Some versions of Windows have a checkbox to autodetect, if it's there, uncheck this.
  3. Manufacturer: Generic, Printer: Generic / Text Only
  4. Use the driver that is currently installed
  5. Give the printer a name that differentiates it from the one already created, i.e. Zebra TTP8200 Raw.
  6. Do not share
  7. Do not print test page, Finish

Now with your raw printing application, use the newly created printer.

P.S.
These instructions are also available here, with screenshots, as part of a Java open source raw printing tutorial. The project provides tutorials for other platforms (Ubuntu, OS X) as well.

http://qzindustries.com/TutorialRawWin

-Tres

念﹏祤嫣 2024-11-21 23:41:24

感谢您的评论。

经过更多挖掘后,我发现 这篇关于使用 usbprint.sys 提供的 Windows 打印机功能的有趣文章。通过一些修改,示例代码似乎可以工作。我想我会走这条路。

文章中给出了最终的代码:

/* Code to find the device path for a usbprint.sys controlled
* usb printer and print to it
*/

#include <usb.h>
#include <usbiodef.h>
#include <usbioctl.h>
#include <usbprint.h>
#include <setupapi.h>
#include <devguid.h>
#include <wdmguid.h>

/* This define is required so that the GUID_DEVINTERFACE_USBPRINT variable is
 * declared an initialised as a static locally, since windows does not include it
 * in any of its libraries
 */

#define SS_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
static const GUID name \
= { l, w1, w2, { b1, b2,  b3,  b4,  b5,  b6,  b7,  b8 } }

SS_DEFINE_GUID(GUID_DEVINTERFACE_USBPRINT, 0x28d78fad, 0x5a12, 0x11D1, 0xae,
               0x5b, 0x00, 0x00, 0xf8, 0x03, 0xa8, 0xc2);

void SomeFunctionToWriteToUSB()
{
  HDEVINFO devs;
  DWORD devcount;
  SP_DEVINFO_DATA devinfo;
  SP_DEVICE_INTERFACE_DATA devinterface;
  DWORD size;
  GUID intfce;
  PSP_DEVICE_INTERFACE_DETAIL_DATA interface_detail;

  intfce = GUID_DEVINTERFACE_USBPRINT;
  devs = SetupDiGetClassDevs(&intfce, 0, 0, DIGCF_PRESENT |
                             DIGCF_DEVICEINTERFACE);
  if (devs == INVALID_HANDLE_VALUE) {
    return;
  }
  devcount = 0;
  devinterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  while (SetupDiEnumDeviceInterfaces(devs, 0, &intfce, devcount, &devinterface)) {
    /* The following buffers would normally be malloced to he correct size
     * but here we just declare them as large stack variables
     * to make the code more readable
     */
    char driverkey[2048];
    char interfacename[2048];
    char location[2048];
    char description[2048];

    /* If this is not the device we want, we would normally continue onto the
     * next one or so something like
     * if (!required_device) continue; would be added here
     */
    devcount++;
    size = 0;
    /* See how large a buffer we require for the device interface details */
    SetupDiGetDeviceInterfaceDetail(devs, &devinterface, 0, 0, &size, 0);
    devinfo.cbSize = sizeof(SP_DEVINFO_DATA);
    interface_detail = calloc(1, size);
    if (interface_detail) {
      interface_detail->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);
      devinfo.cbSize = sizeof(SP_DEVINFO_DATA);
      if (!SetupDiGetDeviceInterfaceDetail(devs, &devinterface, interface_detail,
                                           size, 0, &devinfo)) {
    free(interface_detail);
    SetupDiDestroyDeviceInfoList(devs);
    return;
      }
      /* Make a copy of the device path for later use */
      strcpy(interfacename, interface_detail->DevicePath);
      free(interface_detail);
      /* And now fetch some useful registry entries */
      size = sizeof(driverkey);
      driverkey[0] = 0;
      if (!SetupDiGetDeviceRegistryProperty(devs, &devinfo, SPDRP_DRIVER, &dataType,
                                            (LPBYTE)driverkey, size, 0)) {
    SetupDiDestroyDeviceInfoList(devs);
    return;
      }
      size = sizeof(location);
      location[0] = 0;
      if (!SetupDiGetDeviceRegistryProperty(devs, &devinfo,
                                            SPDRP_LOCATION_INFORMATION, &dataType,
                                            (LPBYTE)location, size, 0)) {
    SetupDiDestroyDeviceInfoList(devs);
    return;
      }
      usbHandle = CreateFile(interfacename, GENERIC_WRITE, FILE_SHARE_READ,
                 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL |
                 FILE_FLAG_SEQUENTIAL_SCAN, NULL);
      if (usbHandle != INVALID_HANDLE_VALUE) {
    /* Now perform all the writing to the device ie.
     * while (some condition) WriteFile(usbHandle, buf, size, &bytes_written);
     */
    CloseHandle(usbHandle);
      }
    }
  }
  SetupDiDestroyDeviceInfoList(devs);
}

再次感谢您的建议。

Thanks for the comments.

After some more digging around, I found this interesting article on using Windows printer functions provided by usbprint.sys. With a bit of hacking the sample code there seemed to work. I think I'll take this route.

There is the final code given in the article:

/* Code to find the device path for a usbprint.sys controlled
* usb printer and print to it
*/

#include <usb.h>
#include <usbiodef.h>
#include <usbioctl.h>
#include <usbprint.h>
#include <setupapi.h>
#include <devguid.h>
#include <wdmguid.h>

/* This define is required so that the GUID_DEVINTERFACE_USBPRINT variable is
 * declared an initialised as a static locally, since windows does not include it
 * in any of its libraries
 */

#define SS_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
static const GUID name \
= { l, w1, w2, { b1, b2,  b3,  b4,  b5,  b6,  b7,  b8 } }

SS_DEFINE_GUID(GUID_DEVINTERFACE_USBPRINT, 0x28d78fad, 0x5a12, 0x11D1, 0xae,
               0x5b, 0x00, 0x00, 0xf8, 0x03, 0xa8, 0xc2);

void SomeFunctionToWriteToUSB()
{
  HDEVINFO devs;
  DWORD devcount;
  SP_DEVINFO_DATA devinfo;
  SP_DEVICE_INTERFACE_DATA devinterface;
  DWORD size;
  GUID intfce;
  PSP_DEVICE_INTERFACE_DETAIL_DATA interface_detail;

  intfce = GUID_DEVINTERFACE_USBPRINT;
  devs = SetupDiGetClassDevs(&intfce, 0, 0, DIGCF_PRESENT |
                             DIGCF_DEVICEINTERFACE);
  if (devs == INVALID_HANDLE_VALUE) {
    return;
  }
  devcount = 0;
  devinterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  while (SetupDiEnumDeviceInterfaces(devs, 0, &intfce, devcount, &devinterface)) {
    /* The following buffers would normally be malloced to he correct size
     * but here we just declare them as large stack variables
     * to make the code more readable
     */
    char driverkey[2048];
    char interfacename[2048];
    char location[2048];
    char description[2048];

    /* If this is not the device we want, we would normally continue onto the
     * next one or so something like
     * if (!required_device) continue; would be added here
     */
    devcount++;
    size = 0;
    /* See how large a buffer we require for the device interface details */
    SetupDiGetDeviceInterfaceDetail(devs, &devinterface, 0, 0, &size, 0);
    devinfo.cbSize = sizeof(SP_DEVINFO_DATA);
    interface_detail = calloc(1, size);
    if (interface_detail) {
      interface_detail->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);
      devinfo.cbSize = sizeof(SP_DEVINFO_DATA);
      if (!SetupDiGetDeviceInterfaceDetail(devs, &devinterface, interface_detail,
                                           size, 0, &devinfo)) {
    free(interface_detail);
    SetupDiDestroyDeviceInfoList(devs);
    return;
      }
      /* Make a copy of the device path for later use */
      strcpy(interfacename, interface_detail->DevicePath);
      free(interface_detail);
      /* And now fetch some useful registry entries */
      size = sizeof(driverkey);
      driverkey[0] = 0;
      if (!SetupDiGetDeviceRegistryProperty(devs, &devinfo, SPDRP_DRIVER, &dataType,
                                            (LPBYTE)driverkey, size, 0)) {
    SetupDiDestroyDeviceInfoList(devs);
    return;
      }
      size = sizeof(location);
      location[0] = 0;
      if (!SetupDiGetDeviceRegistryProperty(devs, &devinfo,
                                            SPDRP_LOCATION_INFORMATION, &dataType,
                                            (LPBYTE)location, size, 0)) {
    SetupDiDestroyDeviceInfoList(devs);
    return;
      }
      usbHandle = CreateFile(interfacename, GENERIC_WRITE, FILE_SHARE_READ,
                 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL |
                 FILE_FLAG_SEQUENTIAL_SCAN, NULL);
      if (usbHandle != INVALID_HANDLE_VALUE) {
    /* Now perform all the writing to the device ie.
     * while (some condition) WriteFile(usbHandle, buf, size, &bytes_written);
     */
    CloseHandle(usbHandle);
      }
    }
  }
  SetupDiDestroyDeviceInfoList(devs);
}

Thanks again for the suggestions.

泅人 2024-11-21 23:41:24

下面的 C# 类是我从 Microsoft 知识库文章中改编而来的。此类中有一些方法可以将打印作业作为字符串字节[]发送。请注意,其中有一些对 log4net 的引用,可以将其删除/替换为您选择的日志记录框架。 :

/// <summary>
/// Class used to aid in sending raw printer data (PS, PRN, etc) directly to the printer.
/// This class was taken from http://support.microsoft.com/kb/322091
/// </summary>
public class PrintQueueUtility
{
    private static ILog log = LogManager.GetLogger(typeof(PrintQueueUtility));

    [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

    [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool ClosePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

    [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndDocPrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);

    /// <summary>Method which sends a <see langword="byte"/> array to a printer queue with a specific document name.</summary>
    /// <param name="bytes">Byte array to send to the printer.</param>
    /// <param name="printerName">Name of the printer to send the <paramref name="bytes"/> to.</param>
    /// <param name="documentName">The document Name.</param>
    /// <returns><see cref="bool"/> indicating whether or not the method succeeded at adding something to the print queue.</returns>
    public static bool SendBytesToPrinter(byte[] bytes, string printerName, string documentName)
    {
        bool success;

        // Allocate some unmanaged memory for those bytes into an unmanaged pointer.
        IntPtr unmanagedBytes = Marshal.AllocCoTaskMem(bytes.Length);

        // Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, unmanagedBytes, bytes.Length);

        // Send the unmanaged bytes to the printer.
        success = SendUnmanagedBytesToPrinter(unmanagedBytes, printerName, documentName, bytes.Length);

        // Free the unmanaged memory that you allocated earlier.
        Marshal.FreeCoTaskMem(unmanagedBytes);

        return success;
    }

    /// <summary>Method which sends a string to the printer queue with a specific document name.</summary>
    /// <param name="data"><see cref="String"/> data to send to the printer.</param>
    /// <param name="printerName">Name of the printer to send the data to.</param>
    /// <param name="documentName">Name of the document in the printer queue.</param>
    /// <returns><see cref="bool"/> indicating whether or not the method succeeded at adding something to the print queue.</returns>
    public static bool SendStringToPrinter(string data, string printerName, string documentName)
    {
        bool success;
        IntPtr unmanagedBytes;

        // How many characters are in the string?
        var characterCount = data.Length;

        // Assume that the printer is expecting ANSI text, and then convert
        // the string to ANSI text.
        unmanagedBytes = Marshal.StringToCoTaskMemAnsi(data);

        // Send the converted ANSI string to the printer.
        success = SendUnmanagedBytesToPrinter(unmanagedBytes, printerName, documentName, characterCount);
        Marshal.FreeCoTaskMem(unmanagedBytes);

        return success;
    }

    private static bool SendUnmanagedBytesToPrinter(IntPtr unmanagedBytes, string printerName, string documentName, int count)
    {
        int error; 
        int written;
        IntPtr printer;
        var di = new DOCINFOA();
        var success = false;

        di.pDocName = documentName;
        di.pDataType = "RAW";

        // Open the printer.
        if (OpenPrinter(printerName.Normalize(), out printer, IntPtr.Zero))
        {
            // Start a document.
            if (StartDocPrinter(printer, 1, di))
            {
                // Start a page.
                if (StartPagePrinter(printer))
                {
                    // Write the bytes.
                    success = WritePrinter(printer, unmanagedBytes, count, out written);
                    EndPagePrinter(printer);
                }

                EndDocPrinter(printer);
            }

            ClosePrinter(printer);
        }

        // If you did not succeed, GetLastError may give more information
        // about why not.
        if (!success)
        {
            error = Marshal.GetLastWin32Error();

            log.ErrorFormat("Sending bytes to printer {0} failed. Last Win32 error = {1}", printerName, error);
        }

        return success;
    }

    // Structure and API declarations:
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class DOCINFOA
    {
        [MarshalAs(UnmanagedType.LPStr)]
        public string pDocName;

        [MarshalAs(UnmanagedType.LPStr)]
        public string pOutputFile;

        [MarshalAs(UnmanagedType.LPStr)]
        public string pDataType;
    }

The below class in C# is something I've adapted from a Microsoft Knowledgebase article. There are methods in this class to send the print job as a string and byte[]. Please note there are some references to log4net in there that can be removed/replaced with the logging framework of your choice. :

/// <summary>
/// Class used to aid in sending raw printer data (PS, PRN, etc) directly to the printer.
/// This class was taken from http://support.microsoft.com/kb/322091
/// </summary>
public class PrintQueueUtility
{
    private static ILog log = LogManager.GetLogger(typeof(PrintQueueUtility));

    [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

    [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool ClosePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

    [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndDocPrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);

    /// <summary>Method which sends a <see langword="byte"/> array to a printer queue with a specific document name.</summary>
    /// <param name="bytes">Byte array to send to the printer.</param>
    /// <param name="printerName">Name of the printer to send the <paramref name="bytes"/> to.</param>
    /// <param name="documentName">The document Name.</param>
    /// <returns><see cref="bool"/> indicating whether or not the method succeeded at adding something to the print queue.</returns>
    public static bool SendBytesToPrinter(byte[] bytes, string printerName, string documentName)
    {
        bool success;

        // Allocate some unmanaged memory for those bytes into an unmanaged pointer.
        IntPtr unmanagedBytes = Marshal.AllocCoTaskMem(bytes.Length);

        // Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, unmanagedBytes, bytes.Length);

        // Send the unmanaged bytes to the printer.
        success = SendUnmanagedBytesToPrinter(unmanagedBytes, printerName, documentName, bytes.Length);

        // Free the unmanaged memory that you allocated earlier.
        Marshal.FreeCoTaskMem(unmanagedBytes);

        return success;
    }

    /// <summary>Method which sends a string to the printer queue with a specific document name.</summary>
    /// <param name="data"><see cref="String"/> data to send to the printer.</param>
    /// <param name="printerName">Name of the printer to send the data to.</param>
    /// <param name="documentName">Name of the document in the printer queue.</param>
    /// <returns><see cref="bool"/> indicating whether or not the method succeeded at adding something to the print queue.</returns>
    public static bool SendStringToPrinter(string data, string printerName, string documentName)
    {
        bool success;
        IntPtr unmanagedBytes;

        // How many characters are in the string?
        var characterCount = data.Length;

        // Assume that the printer is expecting ANSI text, and then convert
        // the string to ANSI text.
        unmanagedBytes = Marshal.StringToCoTaskMemAnsi(data);

        // Send the converted ANSI string to the printer.
        success = SendUnmanagedBytesToPrinter(unmanagedBytes, printerName, documentName, characterCount);
        Marshal.FreeCoTaskMem(unmanagedBytes);

        return success;
    }

    private static bool SendUnmanagedBytesToPrinter(IntPtr unmanagedBytes, string printerName, string documentName, int count)
    {
        int error; 
        int written;
        IntPtr printer;
        var di = new DOCINFOA();
        var success = false;

        di.pDocName = documentName;
        di.pDataType = "RAW";

        // Open the printer.
        if (OpenPrinter(printerName.Normalize(), out printer, IntPtr.Zero))
        {
            // Start a document.
            if (StartDocPrinter(printer, 1, di))
            {
                // Start a page.
                if (StartPagePrinter(printer))
                {
                    // Write the bytes.
                    success = WritePrinter(printer, unmanagedBytes, count, out written);
                    EndPagePrinter(printer);
                }

                EndDocPrinter(printer);
            }

            ClosePrinter(printer);
        }

        // If you did not succeed, GetLastError may give more information
        // about why not.
        if (!success)
        {
            error = Marshal.GetLastWin32Error();

            log.ErrorFormat("Sending bytes to printer {0} failed. Last Win32 error = {1}", printerName, error);
        }

        return success;
    }

    // Structure and API declarations:
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class DOCINFOA
    {
        [MarshalAs(UnmanagedType.LPStr)]
        public string pDocName;

        [MarshalAs(UnmanagedType.LPStr)]
        public string pOutputFile;

        [MarshalAs(UnmanagedType.LPStr)]
        public string pDataType;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文