PInvoke GetPrinterData() 以获取打印机状态或错误

发布于 2024-11-14 12:23:57 字数 1438 浏览 2 评论 0原文

我正在尝试获取 Zebra 标签打印机的打印机状态。我尝试过 WMI,检查 PrinterState、PrinterStatus、ExtendedPrinterStatus、DetectedErrorState、ExtendedDetectedErrorState,但我总是得到诸如 Unknown 或 Idle 之类的值。打印机实际上缺纸了,我想获得该状态。 Windows 打印机区域本身也报告“就绪”状态。我安装了一个小型 Zebra Status Monitor 应用程序,它正确报告“缺纸”。

我想我可能需要调用一些Windows API来直接查询打印机。 Zebra 有此参考 显示了 GetPrinter() 和 GetPrinterData() 的使用。我发现这个使用 GetPrinter() 的 C# 示例 可以正常工作并包含有用的信息,但状态为 0,并且我没有看到任何指示缺纸的信息。

现在,我想尝试使用 GetPrinterData() ,它 Zebra 文档 使用和检查PRINTER_STATUS_PAPER_OUT。使用 GetPrinter() 代码 到 OpenPrinter()(这是成功的),我尝试了 GetPrinterData() 的一些变体。这里有一个:

uint pType = 0;
uint pcbNeeded = 0;
uint result1 = GetPrinterData(pHandle, "Error", out pType, null, 0, out pcbNeeded);

result1 的值始终为 2,pcbNeeded 的值始终为 0。即使我关闭打印机,或者打印机有纸,我也总是得到这些相同的值。

有关如何正确 pInvoke GetPrinterData() 或如何获取实际打印机状态的任何帮助都会非常有帮助。

I'm trying to get the printer status of a Zebra label printer. I've tried WMI, checking PrinterState, PrinterStatus, ExtendedPrinterStatus, DetectedErrorState, ExtendedDetectedErrorState, but I'm always getting values such as Unknown, or Idle. The printer is actually out of paper, and I want to get that status. The Windows Printers area itself is also reporting "Ready" for the status. I installed a small Zebra Status Monitor app and it correctly reports "Out of Paper".

I think I may need to pInvoke some Windows APIs to directly query the printer. Zebra has this reference which shows the use of GetPrinter() and GetPrinterData(). I found this C# example of using GetPrinter() which works and includes good information, but the Status is 0 and I don't see anything that indicates Out of Paper.

Now, I'd like to try to use GetPrinterData() which that Zebra document uses and checks for PRINTER_STATUS_PAPER_OUT. Using that GetPrinter() code to OpenPrinter() (which is successful), I've tried a few variations of GetPrinterData(). Here's one:

uint pType = 0;
uint pcbNeeded = 0;
uint result1 = GetPrinterData(pHandle, "Error", out pType, null, 0, out pcbNeeded);

result1 is always a value of 2 and pcbNeeded is value of 0. Even if I turn off the printer, or if the printer has paper, I always get these same values.

Any help on how to correctly pInvoke GetPrinterData() or on how to get the actual printer status would be very helpful.

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

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

发布评论

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

评论(2

七度光 2024-11-21 12:23:57

虽然对您来说已经晚了,但帖子 与 Zebra 的 USB 通信Printers in C# 展示了如何通过 USB 查询打印机。

Although it is late for you, the post USB communications with Zebra printers in C# shows how to query the printer over USB.

黒涩兲箜 2024-11-21 12:23:57

看来 GetPrinterData 的 pinvoke.net 声明是正确的,但并不总是有效。如果将声明更改为:

    [DllImport("winspool.drv",
    SetLastError = true,
    CharSet = CharSet.Ansi,
    CallingConvention = CallingConvention.StdCall)]
    static extern uint GetPrinterData(
        IntPtr hPrinter,
        string pValueName,
        out uint pType,
        out UInt32 pData,
        uint nSize,
        out uint pcbNeeded);

使用 UInt32 而不是建议的 byte[] 似乎会像 C++ 应用程序一样返回“错误”状态值。

As it appears the pinvoke.net declaration for GetPrinterData is correct but doesn't work all the time. If you change the declaration to:

    [DllImport("winspool.drv",
    SetLastError = true,
    CharSet = CharSet.Ansi,
    CallingConvention = CallingConvention.StdCall)]
    static extern uint GetPrinterData(
        IntPtr hPrinter,
        string pValueName,
        out uint pType,
        out UInt32 pData,
        uint nSize,
        out uint pcbNeeded);

using UInt32 instead of the suggested byte[] seems to return "Error" status values as from a C++ application.

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