Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 671
  • Last Modified:

How to detect Windows virtual printer from physical printer?

I have a Windows application.  How can I detect in code whether the printer is a physical printer or a virtual print (such as Adobe PDF, Mixrosoft XPS, FAX)?? I want to prevent the user from printing to a virtual printer.
0
abutler777
Asked:
abutler777
1 Solution
 
grayeCommented:
Unforuneately, there's no easy way to do it...   But below is a brute-force approach that I've been using.  This example is just cut-n-pasted from working code, so please disregard things that are not applicable.
'
    ' Get information about installed printers (includes networked printers).
    '
    Public Sub GetPrinters()
        Dim obj As ManagementObject
        Dim cnt As Integer
        Dim temp As String
        Dim dr As SOSOSDataset.PrintersRow

        Try
            cnt = 0
            wmi.Path.RelativePath = "Win32_Printer"
            For Each obj In wmi.GetInstances()
                dr = ds.Printers.NewPrintersRow
                dr.ID_Printer = ID
                dr.Printer_ID = cnt
                dr.Printer = Left(obj("Caption").ToString, ds.Printers.PrinterColumn.MaxLength)
                dr.Printer_Port = Left(obj("PortName").ToString, ds.Printers.Printer_PortColumn.MaxLength)
                ' Is this a *real* local printer?
                temp = Left(dr.Printer_Port, 3)
                If (temp = "LPT" Or temp = "COM" Or temp = "USB" Or temp = "DOT") And dr.Printer.StartsWith("Acrobat") = False And dr.Printer.StartsWith("Adobe") = False And dr.Printer.StartsWith("Generic") = False And dr.Printer.StartsWith("\\") = False Then
                    If Not IsNothing(obj("ShareName")) Then
                        dr.Printer_SharedAs = Left(obj("ShareName").ToString, ds.Printers.Printer_SharedAsColumn.MaxLength)
                    End If
                    ds.SOS(0).Printer = dr.Printer
                End If
                ds.Printers.AddPrintersRow(dr)
                cnt += 1
            Next
        Catch ex As Exception
            dr = ds.Printers.NewPrintersRow
            dr.ID_Printer = ID
            dr.Printer_ID = -1
            dr.Printer = Left("Error: " & ex.Message, ds.Printers.PrinterColumn.MaxLength)
            ds.Printers.AddPrintersRow(dr)

            ' Check to see if this is an error that is fairly common.  If so,
            ' let's not clog up the error log with it.
            If ex.GetType Is GetType(System.Management.ManagementException) Then
                If CType(ex, ManagementException).ErrorCode = ManagementStatus.Failed Then
                    Misc.ErrorLog(ex, MachineName, "Printers", Misc.ErrLogLevel.Full)
                    Exit Try
                End If
            End If
            Misc.ErrorLog(ex, MachineName, "Printers", Misc.ErrLogLevel.Errors)
        End Try
    End Sub

Open in new window

0
 
hdhondtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now