无法在原始打印机上打印,

时间:2015-01-12 02:52:37

标签: vb.net printing

我正在尝试在热敏打印机上打印原始数据,但每当我尝试打印时,我都会得到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 我做了一些研究,但我不知道发生了什么。任何帮助都会很棒。 提前致谢

1 个答案:

答案 0 :(得分:0)

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

希望这有帮助。