2015-01-12 100 views
0

我試圖在熱打印機上打印原始數據,但每次嘗試打印時,我都會得到一張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 我做了一些研究,但我不知道是怎麼回事。任何幫助都會很棒。 在此先感謝

回答

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 

希望這有助於。