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