Torna al Thread
[CODE]
Imports System.Runtime.InteropServices
#Region "Structure Printer Info"
'Printer Info structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
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
#End Region
#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
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("Errore: " & ex.Message, "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
' Close the printer object.
ClosePrinter(hPrinter)
' Deallocate the memory.
Marshal.FreeHGlobal(pPrinterInfo)
End Try
Catch ex As Exception
MessageBox.Show("Errore: " & ex.Message, "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return New PRINTER_INFO_2
End Function
[/CODE]