【问题标题】:Reading printer data using ReadPrinter Winspool.Drv使用 ReadPrinter Winspool.Drv 读取打印机数据
【发布时间】:2018-08-06 07:41:15
【问题描述】:

我通过 USB 连接了 Zebra 打印机,我正在尝试使用命令 ^XA^HWR:^XZ 读取打印机的内存。该命令适用于 TCP/IP。

我什至不确定我的方法头是否正确。

[DllImport("winspool.Drv", EntryPoint = "ReadPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
static extern bool ReadPrinter(IntPtr hPrinter, IntPtr pBuf, int cbBuf, out int pNoBytesRead);

方法 ReadPrinter 总是返回 false 和 0 个读取字节。当我尝试获取 LastWin32Error 时,我会得到各种 0、6 或 63(ERROR_SUCCESS - 尽管它返回错误且没有数据,ERROR_INVALID_HANDLEERROR_PRINT_CANCELLED),这取决于我我正在尝试。我尝试了几种方法标头和不同的方法,但都没有导致成功读取数据。我启用了“双向支持”并安装了打印机驱动程序。它可能看起来与其他线程重复,但我已经通过它们并且它们都没有帮助。

代码sn-p:

    private static void SendBytesToPrinter(string printerName, IntPtr pointerBytes, int bytesCount)
    {
        int written = 0;
        PrintResult printResult = new PrintResult();
        IntPtr pointerPrinter = new IntPtr(0);
        DOCINFOA docInfo = new DOCINFOA();
        bool success = false;

        docInfo.DocName = "RAW Document";
        docInfo.DataType = "RAW";

        try
        {
            if (OpenPrinter(printerName.Normalize(), out pointerPrinter, IntPtr.Zero))
            {
                if (StartDocPrinter(pointerPrinter, 1, docInfo))
                {
                    if (StartPagePrinter(pointerPrinter))
                    {
                        success = WritePrinter(pointerPrinter, pointerBytes, bytesCount, out written);
                        EndPagePrinter(pointerPrinter);
                    }

                    EndDocPrinter(pointerPrinter);
                 }

                 // READ HERE
                 Int32 bufLen = 32;
                 IntPtr pStatus = Marshal.AllocCoTaskMem(bufLen);
                 Int32 statusLen = 0;
                 bool bSuccess = ReadPrinter(hPrintJob, pStatus, bufLen, out statusLen);

                 ClosePrinter(pointerPrinter);
             } 
        } catch (Exception ex) { }
    }

【问题讨论】:

  • 那里 documentation.
  • @IInspectable 这对我没有帮助。我从未与外部人员进行过深入的合作,我不知道LPVOIDDWORD 是什么。我以前看过这个页面,但对我来说没有意义。我没有找到任何可行的例子。
  • 它是否对您有帮助并没有什么不同。有 文档。当然,您需要了解 C 才能理解 C 接口。您是在寻求基本的 P/Invoke 帮助吗?
  • @IInspectable 那么我道歉。我已经设法使用 WritePrinter 进行打印,所以我认为 ReadPrinter 会类似。显然,我错了。我想知道为什么 ReadPrinter 返回 false/0 字节。

标签: c# windows printing zpl


【解决方案1】:

我使用 Visual Basic 示例解决了这个问题,使用了 FileStreamsStreamReaders。感谢@kunif 指出示例代码。

kernel32.dll 是要走的路,而不是 winspool.Drv

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern SafeFileHandle CreateFile(string lpFileName, EFileAccess dwDesiredAccess, 
                                                EFileShare dwShareMode, IntPtr lpSecurityAttributes, 
                                                ECreationDisposition dwCreationDisposition, 
                                                EFileAttributes dwFlagsAndAttributes, 
                                                IntPtr hTemplateFile);

private static string ReadUsbPort(StreamReader sr)
{
    string readResult = string.Empty;
    if (sr != null && sr.BaseStream != null)
    {
       do
       {
            readResult += sr.ReadLine();
            readResult += "\r";
        }
        while (sr.EndOfStream == false);
     }
   return readResult;
}

非常有用的是结合 GUID_DEVINTERFACE_USB_DEVICEWinUSBNet

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 2013-07-05
    • 1970-01-01
    • 2017-12-28
    • 2017-09-12
    相关资源
    最近更新 更多