【问题标题】:Cannot print on raw printer,无法在原始打印机上打印,
【发布时间】:2015-01-12 02:52:36
【问题描述】:

我正在尝试在热敏打印机上打印原始数据,但每次尝试打印时都会收到PInvokeStackImbalance

我正在使用this 示例进行打印。异常总是发生在这里:

    Public Shared Function SendBytesToPrinter(ByVal szPrinterName As String, ByVal pBytes As IntPtr, ByVal dwCount As Int32) As Boolean
    Dim hPrinter As IntPtr      ' The printer handle.
    Dim dwError As Int32        ' Last error - in case there was trouble.
    Dim di As DOCINFOW          ' Describes your document (name, port, data type).
    Dim dwWritten As Int32      ' The number of bytes written by WritePrinter().
    Dim bSuccess As Boolean     ' Your success code.

    ' Set up the DOCINFO structure.
    With di
        .pDocName = "My Visual Basic .NET RAW Document"
        .pDataType = "RAW"
    End With
    ' Assume failure unless you specifically succeed.
    bSuccess = False
    If OpenPrinter(szPrinterName, hPrinter, 0) Then
        If StartDocPrinter(hPrinter, 1, di) Then
            If StartPagePrinter(hPrinter) Then
                ' Write your printer-specific bytes to the printer.
                bSuccess = WritePrinter(hPrinter, pBytes, dwCount, dwWritten)
                EndPagePrinter(hPrinter)
            End If
            EndDocPrinter(hPrinter)
        End If
        ClosePrinter(hPrinter)
    End If
    ' If you did not succeed, GetLastError may give more information
    ' about why not.
    If bSuccess = False Then
        dwError = Marshal.GetLastWin32Error()
    End If
    Return bSuccess
End Function ' SendBytesToPrinter()

例外在这一行If OpenPrinter(szPrinterName, hPrinter, 0) Then 我做了一些研究,但我不知道发生了什么。任何帮助都会很棒。 提前致谢

【问题讨论】:

    标签: vb.net printing


    【解决方案1】:

    PInvokeStackImbalance 错误通常是编组问题,由不正确的声明引起。你没有说你正在运行什么,但如果你的 DLLImport 函数看起来像这样:

    Public Shared Function OpenPrinter(ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Long) As Boolean
    

    也许可以试试这个(最后一个参数是整数而不是长整数):

    Public Shared Function OpenPrinter(ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Integer) As Boolean
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-07
      相关资源
      最近更新 更多