代码之家  ›  专栏  ›  技术社区  ›  Bukk94

使用ReadPrinter Winspool.Drv读取打印机数据

  •  -1
  • Bukk94  · 技术社区  · 8 年前

    我已经通过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 -尽管它返回false并且没有数据, ERROR_INVALID_HANDLE 或 ERROR_PRINT_CANCELLED ),这取决于我正在努力。我尝试了几种方法头和不同的方法,但都没有成功读取数据。我启用了“双向支持”,并安装了打印机驱动程序。它看起来像是其他线程的副本,但我已经通过了它们,没有一个是有用的。

    代码段:

        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) { }
        }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Bukk94    8 年前

    我用Visual Basic的例子,用 FileStreams 和 StreamReaders . 感谢@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_DEVICE 和 WinUSBNet