Hi all,
I want to get the printer status notification. For example when printer runs low on toner or its jammed or some other problem that shows on the display.
I use this example of Wade Brooks, which uses getprinter I guess.
It shows much information about the printer but not the notification status.
Is there a way to add this to this program?
Thank you in future.
I want to get the printer status notification. For example when printer runs low on toner or its jammed or some other problem that shows on the display.
I use this example of Wade Brooks, which uses getprinter I guess.
Code:
'Purpose: Gets list of printers and then list of printer settings and information
'
'Author: Wade Brooks
'
'Change History:
'Date Developer Description
'1/24/2008 Wade Brooks Created
'11/11/2009 Wade Brooks Upgraded to VS2008, cleaned up code
'6/17/2010 Wade Brooks Upgraded to VS2010
Option Strict On
Option Explicit On
Imports System.Drawing.Printing 'Printer Settings
Imports System.Runtime.InteropServices 'Printer Information
Imports System.text 'String Builder
Public Class frmPrinterInfoExample
#Region "Imports for Printer Information"
<DllImport("winspool.drv", EntryPoint:="OpenPrinterW", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function OpenPrinter(ByVal pPrinterName As String, ByRef hPrinter As IntPtr, ByVal pDefault As IntPtr) As Boolean
End Function
<DllImport("winspool.drv", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function ClosePrinter(ByVal hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.drv", EntryPoint:="GetPrinterW", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function GetPrinter(ByVal hPrinter As IntPtr, ByVal dwLevel As Integer, ByVal pPrinter As IntPtr, ByVal cbBuf As Integer, ByRef pcbNeeded As Integer) As Boolean
End Function
#End Region
#Region "Variables"
Dim mstrInfo As StringBuilder 'Info for User
'Printer Info structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> Public Structure PRINTER_INFO_2
Dim pServerName As String
Dim pPrinterName As String
Dim pShareName As String
Dim pPortName As String
Dim pDriverName As String
Dim pComment As String
Dim pLocation As String
Dim pDevMode As Integer
Dim pSepFile As String
Dim pPrintProcessor As String
Dim pDatatype As String
Dim pParameters As String
Dim pSecurityDescriptor As Integer
Dim Attributes As Integer
Dim Priority As Integer
Dim DefaultPriority As Integer
Dim StartTime As Integer
Dim UntilTime As Integer
Dim Status As Integer
Dim cJobs As Integer
Dim AveragePPM As Integer
End Structure
Public Enum PrinterChangeNotifications
PRINTER_CHANGE_ADD_PRINTER = &H1
PRINTER_CHANGE_SET_PRINTER = &H2
PRINTER_CHANGE_DELETE_PRINTER = &H4
PRINTER_CHANGE_FAILED_CONNECTION_PRINTER = &H8
PRINTER_CHANGE_PRINTER = &HFF
PRINTER_CHANGE_ADD_JOB = &H100
PRINTER_CHANGE_SET_JOB = &H200
PRINTER_CHANGE_DELETE_JOB = &H400
PRINTER_CHANGE_WRITE_JOB = &H800
PRINTER_CHANGE_JOB = &HFF00
PRINTER_CHANGE_ADD_FORM = &H10000
PRINTER_CHANGE_SET_FORM = &H20000
PRINTER_CHANGE_DELETE_FORM = &H40000
PRINTER_CHANGE_FORM = &H70000
PRINTER_CHANGE_ADD_PORT = &H100000
PRINTER_CHANGE_CONFIGURE_PORT = &H200000
PRINTER_CHANGE_DELETE_PORT = &H400000
PRINTER_CHANGE_PORT = &H700000
PRINTER_CHANGE_ADD_PRINT_PROCESSOR = &H1000000
PRINTER_CHANGE_DELETE_PRINT_PROCESSOR = &H4000000
PRINTER_CHANGE_PRINT_PROCESSOR = &H7000000
PRINTER_CHANGE_ADD_PRINTER_DRIVER = &H10000000
PRINTER_CHANGE_SET_PRINTER_DRIVER = &H20000000
PRINTER_CHANGE_DELETE_PRINTER_DRIVER = &H40000000
PRINTER_CHANGE_PRINTER_DRIVER = &H70000000
PRINTER_CHANGE_TIMEOUT = &H80000000
End Enum
Public Enum JobChangeNotificationFields
JOB_NOTIFY_FIELD_PRINTER_NAME = &H0
JOB_NOTIFY_FIELD_MACHINE_NAME = &H1
JOB_NOTIFY_FIELD_PORT_NAME = &H2
JOB_NOTIFY_FIELD_USER_NAME = &H3
JOB_NOTIFY_FIELD_NOTIFY_NAME = &H4
JOB_NOTIFY_FIELD_DATATYPE = &H5
JOB_NOTIFY_FIELD_PRINT_PROCESSOR = &H6
JOB_NOTIFY_FIELD_PARAMETERS = &H7
JOB_NOTIFY_FIELD_DRIVER_NAME = &H8
JOB_NOTIFY_FIELD_DEVMODE = &H9
JOB_NOTIFY_FIELD_STATUS = &HA
JOB_NOTIFY_FIELD_STATUS_STRING = &HB
JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR = &HC
JOB_NOTIFY_FIELD_DOCUMENT = &HD
JOB_NOTIFY_FIELD_PRIORITY = &HE
JOB_NOTIFY_FIELD_POSITION = &HF
JOB_NOTIFY_FIELD_SUBMITTED = &H10
JOB_NOTIFY_FIELD_START_TIME = &H11
JOB_NOTIFY_FIELD_UNTIL_TIME = &H12
JOB_NOTIFY_FIELD_TIME = &H13
JOB_NOTIFY_FIELD_TOTAL_PAGES = &H14
JOB_NOTIFY_FIELD_PAGES_PRINTED = &H15
JOB_NOTIFY_FIELD_TOTAL_BYTES = &H16
JOB_NOTIFY_FIELD_BYTES_PRINTED = &H17
End Enum
#End Region
#Region "Private Methods"
''' <summary>
''' Populate printer list
''' </summary>
Private Sub btnPrinterList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrinterList.Click
Try
PopulateInstalledPrintersCombo()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Sub
''' <summary>
''' List of installed printers
''' </summary>
Private Sub PopulateInstalledPrintersCombo()
Try
cboInstalledPrinters.Items.Clear()
' Add list of installed printers found to the combo box.
For Each strPrinter As String In PrinterSettings.InstalledPrinters
cboInstalledPrinters.Items.Add(strPrinter)
Next
If cboInstalledPrinters.Items.Count > 0 Then
cboInstalledPrinters.SelectedIndex = 0
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Sub
''' <summary>
''' Show other printer info to user
''' </summary>
Private Sub OtherPrinterInfo()
Dim prtPrinter As PrinterSettings = Nothing
Try
prtPrinter = New PrinterSettings
prtPrinter.PrinterName = cboInstalledPrinters.Text
'Assumes textbox1 cleared in calling function
mstrInfo.Append(String.Format("SupportsColor: {0}{1}", prtPrinter.SupportsColor, vbCrLf))
mstrInfo.Append(String.Format("IsDefaultPrinter: {0}{1}", prtPrinter.IsDefaultPrinter, vbCrLf))
mstrInfo.Append(String.Format("CanDuplex: {0}{1}", prtPrinter.CanDuplex, vbCrLf))
mstrInfo.Append(String.Format("IsPlotter: {0}{1}", prtPrinter.IsPlotter, vbCrLf))
mstrInfo.Append(String.Format("MaximumCopies: {0}{1}", prtPrinter.MaximumCopies, vbCrLf))
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Sub
''' <summary>
''' Printer Information
''' </summary>
Private Function GetPrinterInfo(ByVal sName As String) As PRINTER_INFO_2
Dim hPrinter As IntPtr = IntPtr.Zero
Dim pPrinterInfo As IntPtr = IntPtr.Zero
Dim iNeed As Integer = -1
Dim SizeOf As Integer = -1
Try
'Open printer object
If (Not OpenPrinter(sName, hPrinter, IntPtr.Zero)) Then
Marshal.ThrowExceptionForHR(System.Runtime.InteropServices.Marshal.GetHRForLastWin32Error())
End If
Try
' Get the number of bytes needed.
GetPrinter(hPrinter, 2, IntPtr.Zero, 0, iNeed)
' Allocate enough memory.
pPrinterInfo = Marshal.AllocHGlobal(iNeed)
SizeOf = iNeed
If (Not GetPrinter(hPrinter, 2, pPrinterInfo, SizeOf, iNeed)) Then
Marshal.ThrowExceptionForHR(System.Runtime.InteropServices.Marshal.GetHRForLastWin32Error())
End If
' Now marshal the structure manually.
Dim PrinterInfo As PRINTER_INFO_2 = CType(Marshal.PtrToStructure(pPrinterInfo, GetType(PRINTER_INFO_2)), PRINTER_INFO_2)
Return PrinterInfo
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)
Finally
' Close the printer object.
ClosePrinter(hPrinter)
' Deallocate the memory.
Marshal.FreeHGlobal(pPrinterInfo)
End Try
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)
End Try
Return New PRINTER_INFO_2()
End Function
''' <summary>
''' Handles printer info button click
''' </summary>
Private Sub btnPrinterInfo_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrinterInfo.Click
Try
Cursor = Cursors.WaitCursor
If mstrInfo IsNot Nothing Then
mstrInfo = Nothing
End If
mstrInfo = New StringBuilder
Dim PrinterInformation As PRINTER_INFO_2 = GetPrinterInfo(cboInstalledPrinters.Text)
txtPrinterInfo.Clear()
'Show info in Text box
If PrinterInformation.pPrinterName IsNot Nothing Then
mstrInfo.Append(String.Format("PrinterName: {0}{1}", PrinterInformation.pPrinterName, vbCrLf))
End If
If PrinterInformation.pPortName IsNot Nothing Then
mstrInfo.Append(String.Format("PortName: {0}{1}", PrinterInformation.pPortName, vbCrLf))
End If
If PrinterInformation.pComment IsNot Nothing Then
mstrInfo.Append(String.Format("Comment: {0}{1}", PrinterInformation.pComment, vbCrLf))
End If
If PrinterInformation.pDatatype IsNot Nothing Then
mstrInfo.Append(String.Format("Datatype: {0}{1}", PrinterInformation.pDatatype, vbCrLf))
End If
If PrinterInformation.pDriverName IsNot Nothing Then
mstrInfo.Append(String.Format("DriverName: {0}{1}", PrinterInformation.pDriverName, vbCrLf))
End If
If PrinterInformation.pLocation IsNot Nothing Then
mstrInfo.Append(String.Format("Location: {0}{1}", PrinterInformation.pLocation, vbCrLf))
End If
If PrinterInformation.pParameters IsNot Nothing Then
mstrInfo.Append(String.Format("Parameters: {0}{1}", PrinterInformation.pParameters, vbCrLf))
End If
If PrinterInformation.pPrintProcessor IsNot Nothing Then
mstrInfo.Append(String.Format("PrintProcessor: {0}{1}", PrinterInformation.pPrintProcessor, vbCrLf))
End If
If PrinterInformation.pServerName IsNot Nothing Then
mstrInfo.Append(String.Format("ServerName: {0}{1}", PrinterInformation.pServerName, vbCrLf))
End If
If PrinterInformation.pShareName IsNot Nothing Then
mstrInfo.Append(String.Format("ShareName: {0}{1}", PrinterInformation.pShareName, vbCrLf))
End If
'Get other info
OtherPrinterInfo()
'Add info to textbox
txtPrinterInfo.Text = mstrInfo.ToString
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)
Finally
'Clean up
If (mstrInfo IsNot Nothing) Then
mstrInfo = Nothing
End If
Cursor = Cursors.Default
End Try
End Sub
''' <summary>
''' Close Form
''' </summary>
Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
Try
Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Sub
#End Region
End Class
Is there a way to add this to this program?
Thank you in future.