Re: Migrating from VB6 and Printer object



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

.



Relevant Pages

  • Load registry hive (AdjustTokenPrivileges error)
    ... Public PrivilegeCount As Int32 ... Public Function RegLoadKey(ByVal hKey As Int32, ... String, ByVal lpFile As String) As Int32 ... Dim strKeyName As String ...
    (microsoft.public.vb.winapi)
  • Load registry hive (AdjustTokenPrivileges error)
    ... Public PrivilegeCount As Int32 ... Public Function RegLoadKey(ByVal hKey As Int32, ... String, ByVal lpFile As String) As Int32 ... Dim strKeyName As String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: thrown exception in Marshal.PtrToStructure
    ... "Bill McCarthy" wrote: ... Public Const PRINTER_ENUM_SHARED As Int32 = 20 ... Dim pDescription As String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: MAC Address
    ... Also, note that it is easy to read the MAC addresses on the computer on which the code resides, but the server cannot read the MAC address of the client unless there is a program that reads this information on the client and passes it to the server. ... Dim pWinStationName As String ' integer LPTSTR - Pointer to a null-terminated string containing the name of the WinStation for this session ... ByVal SessionId As Int32, ByVal WTSInfoClass As Int32, ByRef ppBuffer As String, ByRef pCount As Int32) As Boolean ...
    (microsoft.public.windows.terminal_services)
  • Re: Convert long number to ascii
    ... Since you said that the rest of the MSComm setup looked OK, I assumed that you were opening the port, setting the baud rate and so on. ... Dim AddrNum As Long ... Dim Addr As String ... Dim Valu As String ...
    (microsoft.public.vb.general.discussion)