我正在尝试在热敏打印机上打印原始数据,但每次我尝试打印时都会得到一个
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
我做了一些研究,但我不知道发生了什么。任何帮助都会很棒。
预先感谢
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
希望这有帮助。
也许有人觉得这有帮助。我纠正了这个问题,取消选中项目属性 > 编译选项卡上的 32 位首选项