Re: Migrating from VB6 and Printer object
- From: "Duncan Jones" <Duncan@xxxxxxxxxxxxxxxxxxx>
- Date: Wed, 26 Dec 2007 21:21:28 -0000
You can use the GetPrinter API call to get all the information (such as Port name) - e.g.
(Code snippet extracted from http://www.codeplex.com/PUMA in case I miss any references or declarations)
'- - 8< - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<DllImport("winspool.drv", EntryPoint:="OpenPrinter", _
SetLastError:=True, CharSet:=CharSet.Ansi, _
ExactSpelling:=False, _
CallingConvention:=CallingConvention.StdCall)> _
Public Function OpenPrinter(<InAttribute()> ByVal pPrinterName As String, _
<OutAttribute()> ByRef phPrinter As Int32, _
<InAttribute(), MarshalAs(UnmanagedType.LPStruct)> ByVal pDefault As PRINTER_DEFAULTS _
) As Boolean
End Function
<DllImport("winspool.drv", EntryPoint:="GetPrinter", _
SetLastError:=True, CharSet:=CharSet.Ansi, _
ExactSpelling:=False, _
CallingConvention:=CallingConvention.StdCall)> _
Public Function GetPrinter _
(<InAttribute()> ByVal hPrinter As IntPtr, _
<InAttribute()> ByVal Level As Int32, _
<OutAttribute()> ByVal lpPrinter As IntPtr, _
<InAttribute()> ByVal cbBuf As Int32, _
<OutAttribute()> ByRef lpbSizeNeeded As Int32) As Boolean
End Function
#Region "PRINTER_INFO_2 STRUCTURE"
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi), System.Security.SuppressUnmanagedCodeSecurity()> _
Friend Class PRINTER_INFO_2
<MarshalAs(UnmanagedType.LPStr)> Public pServerName As String
<MarshalAs(UnmanagedType.LPStr)> Public pPrinterName As String
<MarshalAs(UnmanagedType.LPStr)> Public pShareName As String
<MarshalAs(UnmanagedType.LPStr)> Public pPortName As String
<MarshalAs(UnmanagedType.LPStr)> Public pDriverName As String
<MarshalAs(UnmanagedType.LPStr)> Public pComment As String
<MarshalAs(UnmanagedType.LPStr)> Public pLocation As String
<MarshalAs(UnmanagedType.U4)> Public lpDeviceMode As Int32
<MarshalAs(UnmanagedType.LPStr)> Public pSeperatorFilename As String
<MarshalAs(UnmanagedType.LPStr)> Public pPrintProcessor As String
<MarshalAs(UnmanagedType.LPStr)> Public pDataType As String
<MarshalAs(UnmanagedType.LPStr)> Public pParameters As String
<MarshalAs(UnmanagedType.U4)> Public lpSecurityDescriptor As Int32
Public Attributes As Int32
Public Priority As Int32
Public DefaultPriority As Int32
Public StartTime As Int32
Public UntilTime As Int32
Public Status As Int32
Public JobCount As Int32
Public AveragePPM As Int32
#Region "Private member variables"
Dim dmOut As New DEVMODE
#End Region
#Region "Public constructors"
Public Sub New(ByVal hPrinter As IntPtr)
Dim BytesWritten As Int32 = 0
Dim ptBuf As IntPtr
ptBuf = Marshal.AllocHGlobal(1)
If Not GetPrinter(hPrinter, 2, ptBuf, 1, BytesWritten) Then
If BytesWritten > 0 Then
'\\ Free the buffer allocated
Marshal.FreeHGlobal(ptBuf)
ptBuf = Marshal.AllocHGlobal(BytesWritten)
If GetPrinter(hPrinter, 2, ptBuf, BytesWritten, BytesWritten) Then
Marshal.PtrToStructure(ptBuf, Me)
'\\ Fill any missing members
If pServerName Is Nothing Then
pServerName = ""
End If
'\\ If the devicemode is available, get it
If lpDeviceMode > 0 Then
Dim ptrDevMode As New IntPtr(lpDeviceMode)
Marshal.PtrToStructure(ptrDevMode, dmOut)
End If
End If
'\\ Free this buffer again
Marshal.FreeHGlobal(ptBuf)
Else
End If
End If
End Sub
Public ReadOnly Property DeviceMode() As DEVMODE
Get
Return dmOut
End Get
End Property
Public Sub New()
End Sub
#End Region
End Class
#End Region
''' -----------------------------------------------------------------------------
''' Project : PrinterQueueWatch
''' Class : PrinterInformation
'''
''' -----------------------------------------------------------------------------
''' <summary>
''' Class which holds the settings for a printer
''' </summary>
''' <remarks>
''' These settings can apply to physical printers and also to virtual print devices
''' </remarks>
''' <history>
''' [Duncan] 20/11/2005 Created
''' </history>
''' -----------------------------------------------------------------------------
<System.Security.SuppressUnmanagedCodeSecurity()> _
<System.Runtime.InteropServices.ComVisible(False)> _
Public Class PrinterInformation
Implements IDisposable
Private mhPrinter As IntPtr
'\\ PRINTER_INFO_ structures
Private mPrinter_Info_2 As New PRINTER_INFO_2
#Region "PortName"
''' -----------------------------------------------------------------------------
''' <summary>
''' The name of the port the printer is connected to
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <example>Prints the name of the port that the named printer is installed on
''' <code>
''' Dim pi As New PrinterInformation("Microsoft Office Document Image Writer", SpoolerApiConstantEnumerations.PrinterAccessRights.PRINTER_ALL_ACCESS, True)
''' Trace.WriteLine(pi.PortName)
''' </code>
''' </example>
''' <history>
''' [Duncan] 20/11/2005 Created
''' </history>
''' -----------------------------------------------------------------------------
<Diagnostics.MonitoringDescription("The name of the port the printer is connected to")> _
Public Overridable ReadOnly Property PortName() As String
Get
RefreshPrinterInformation(PrinterInfoLevels.PrinterInfoLevel2)
If mPrinter_Info_2.pPortName Is Nothing Then
Return ""
Else
Return mPrinter_Info_2.pPortName
End If
End Get
End Property
#End Region
''' -----------------------------------------------------------------------------
''' <summary>
''' Creates a new printer information class for the named printer
''' </summary>
''' <param name="DeviceName">The name of the print device</param>
''' <param name="DesiredAccess">The required access rights for that printer</param>
''' <param name="GetSecurityInfo"></param>
''' <param name="GetJobs">True to return the collection of print jobs
''' queued against this print device
''' </param>
''' <remarks>
'''
''' </remarks>
''' <history>
''' [Duncan] 20/11/2005 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Sub New(ByVal DeviceName As String, ByVal DesiredAccess As SpoolerApiConstantEnumerations.PrinterAccessRights, ByVal GetSecurityInfo As Boolean, ByVal GetJobs As Boolean)
Dim hPrinter As Integer = 0
If OpenPrinter(DeviceName, hPrinter, New PRINTER_DEFAULTS(DesiredAccess)) Then
mhPrinter = New IntPtr(hPrinter)
mPrinter_Info_2 = New PRINTER_INFO_2(mhPrinter)
Else
Throw New Win32Exception
End If
End Sub
'- - 8< - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Hope this helps,
Duncan Jones
"J.B. Moreno" <planB@xxxxxxxxxxxxxxx> wrote in message news:201220071912257789%planB@xxxxxxxxxxxxxxxxxx
Hello,
I'm migrating a program from VB6 that uses the Printer object to get
the name, driver and port of a printer and then passes it along to a
Crystal 8.0 viewer (this allows printing to non-default printers).
But I can't figure out how to get the port (a string like "ne04:").
The VB6 code looks like:
Dim PrinterLoop As Printer
For Each PrinterLoop In Printers
If PrinterLoop.DeviceName = "namefromconfigfile" Then
crv.PrinterName = PrinterLoop.DeviceName
crv.PrinterPort = PrinterLoop.Port
crv.PrinterDriver = PrinterLoop.DriverName
End If
Next
If I hard code the values from VB6 into VB.Net, the Crystal works as
expected. But while the printname and drivername are easy to obtain. I
have no idea where to find the port, and I really don't want to the
port to the config file, particularly since I don't know how it's
determined.
I tried win32_print and WMI, but that didn't have it. Anyone know the
answer? Or even a good suggestion?
--
J.B. Moreno
.
- References:
- Migrating from VB6 and Printer object
- From: J.B. Moreno
- Migrating from VB6 and Printer object
- Prev by Date: datagridview: how to resize the whole grid
- Next by Date: Re: populating gridview using arraylist of structure
- Previous by thread: Re: Migrating from VB6 and Printer object
- Next by thread: RE: Color Dialog
- Index(es):
Relevant Pages
|