Public Function up()
Dim hPrinter As Integer
Dim ByteBuf As Long
Dim BytesNeeded As Int32
Dim intCount As Long
Dim JI2(intCount) As JOB_INFO_2
Dim PrinterInfo() As Byte
Dim JobInfo() As Byte
Dim result As Long
Dim LastError As Long
Dim PrinterName As String
Dim tempStr As String
Dim NumJI2 As Int32
Dim CheckPrinter As String
Dim JobStr As String
Dim ppD As PRINTER_DEFAULTS
Dim pd As New PrintDocument
PrinterName = PD.PrinterSettings.PrinterName
result = OpenPrinter(PrinterName, hPrinter, ppD)
If result = 0 Then CheckPrinter = "Cannot open printer " & PrinterName & _
", Error: " & Marshal.GetLastWin32Error()
Exit Function
End If
result = EnumJobs(hPrinter, 0, &HFFFFFFFF, 2, JobInfo, 0, BytesNeeded, NumJI2)
If result = 0 Then
'Display the error, and then close the printer handle.
LastError = Marshal.GetLastWin32Error()
CheckPrinter = " > EnumJobs Failed on initial call! < Error = " _
& LastError
ClosePrinter(hPrinter)
Exit Function
End If If BytesNeeded <> 0 Then
'Resize the byte array to hold information about the print jobs.
ReDim JobInfo(BytesNeeded - 1) 'Call the API to obtain the print job information.
result = EnumJobs(hPrinter, 0, &HFFFFFFFF, 2, JobInfo, _
BytesNeeded, BytesNeeded, NumJI2) 'Check for errors.
If result = 0 Then
'Display the error, and then close the printer handle.
LastError = Marshal.GetLastWin32Error()
CheckPrinter = " > EnumJobs Failed on second call! < Error = " _
& LastError
ClosePrinter(hPrinter)
Exit Function
End If
ReDim JI2(NumJI2) 'Copy the contents of print job info byte array into a
'JOB_INFO_2 structure.
Try
For intCount = 0 To NumJI2 - 1 ' Loop through jobs and obtain the job information.
Dim test As Object
JI2(intCount) = CType(DatatoDeserial(JobInfo, _
GetType(JOB_INFO_2), intCount + 1), JOB_INFO_2) JobStr = JobStr & "Job ID = " & JI2(intCount).PrinterJobId & _
vbCrLf & "Total Pages = " & JI2(intCount).TotalPages & vbCrLf
tempStr = ""
'Check for a ready state.
If JI2(intCount).pStatus = 0& Then ' If pStatus is Null, check Status.
If JI2(intCount).pStatus = 0 Then
tempStr = tempStr & "Ready! " & vbCrLf
Else 'Check for the various print job states.
If (JI2(intCount).pStatus And JOB_STATUS_SPOOLING) Then
tempStr = tempStr & "Spooling "
End If If (JI2(intCount).pStatus And JOB_STATUS_OFFLINE) Then
tempStr = tempStr & "Off line "
End If If (JI2(intCount).pStatus And JOB_STATUS_PAUSED) Then
tempStr = tempStr & "Paused "
End If If (JI2(intCount).pStatus And JOB_STATUS_ERROR) Then
tempStr = tempStr & "Error "
End If If (JI2(intCount).pStatus And JOB_STATUS_PAPEROUT) Then
tempStr = tempStr & "Paper Out "
End If If (JI2(intCount).pStatus And JOB_STATUS_PRINTING) Then
tempStr = tempStr & "Printing "
End If If (JI2(intCount).pStatus And JOB_STATUS_USER_INTERVENTION) Then
tempStr = tempStr & "User Intervention Needed "
End If If Len(tempStr) = 0 Then
tempStr = "Unknown Status of " & JI2(intCount).PrinterStatus
End If
End If
Else tempStr = Pointer_to_String(JI2(intCount).pStatus)
End If 'Report the job status.
JobStr = JobStr & tempStr & vbCrLf
Next intCount
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
'Close the printer handle.
ClosePrinter(hPrinter)
End Function
End Module上面的代码 openprinter 这个api函数 就在vb6里面很方便就用了可是在 .net里面就死活报错
报错说 什么堆栈 类型不对
在网上找了很久 终于 把最后一个参数 改成了 integer 不报错了
可是他 开不不开打印机
hPrinter始终是0
郁闷
谁能帮忙
VB.NET与VB是两回事,内部机制都不同了
比如保留了一些VB的旧函数,但其实内部是用.NET Framewrok来实现的~