CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    May 2009
    Location
    London
    Posts
    51

    [RESOLVED] Retrieve Printer Name

    OK, This should be very simple but.....

    Does anybody know how to retrive a list of the names of the currently installed printers. This seems simple enough using the printers collection and the DeviceName property but on network printers this property is returning the network path and not the description/name of the printer. I have tried Google on this but it is returning far too many results which are not related. I am using Windows 7 if this makes any odds.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Retrieve Printer Name

    So, you want to know all the names and details of all the printers. Connected via LAN? I suppose next, you'll want to be able to find all the names of all the printers ON THE INTERNET...

    See the issue? We post code, bad guys use it to print on my printer tomorrow.

    Printers are controlled by PERMISSIONS. If the printer is mapped (NET USE) it will be LPT1, LPT2, or LPT3.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    May 2009
    Location
    London
    Posts
    51

    Re: Retrieve Printer Name

    dglienna,

    I am sorry if I did not make myself clear. I have a number of printers installed on my PC. Some of these printers are local and some are network printers. All of these printers are installed and have the relevant network addresses and passwords.

    I would like to retrieve a list of the names of the printers as they are shown in my control panel.

    Using the DeviceName property I can retrieve the names of the local printers exactly as they are shown in the control panel, however, all of the network printers return a network path and not the printer name.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Retrieve Printer Name

    Here is a little piece of code I wrote years ago that populates a combo with the list of installed printers.
    Code:
    Dim x As Integer
    
    Combo1.Clear
    Combo1.Text = Printer.DeviceName ' This is the default printer
    For x = 0 To Printers.Count - 1
        Combo1.AddItem Printers(x).DeviceName ' this will show all printers including the default one above.
    Next
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    May 2009
    Location
    London
    Posts
    51

    Re: Retrieve Printer Name

    Thanks for this dglienna, but this brings us back to the original problem which is that the DeviceName function is returning the network path and not the name as displayed in the control panel.

    For example my default printer is displayed in the control panel as 'HP LaserJet 6P on ronan' but the Printer.DeviceName method returns '\\ronan\HP LaserJet 6P'. I am not sure if this is something specific to Windows 7 or not but still very odd.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Retrieve Printer Name

    I'm afraid the way it comes up is the way it is. That is the actuall device name. Windows is simply changing the way it is displayed in the area you are looking at. You could do this yourself if you want to write the code.

    Parse the string in devicename, if it begins with \\ drop those off split on \ then assign to a new string from the resulting array
    Code:
    MyPrinterName=MyArray(1) & " on " & MyArray(0)
    Then display MyPrinterName rather than the actual device name.


    So
    \\p42800\HP LaserJet 1020

    becomes
    HP LaserJet 1020 on p42800
    Last edited by DataMiser; September 14th, 2011 at 02:10 PM.
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    May 2009
    Location
    London
    Posts
    51

    Re: Retrieve Printer Name

    OK, Thanks for your help.

    I will have to go back to the drawing board and check the API. Your idea with the string manipulation wont help in this scenario as the printer could have been called anything. There must be some way of accessing this informaion as the control panel and the standard printer common dialog both have the correct printer name.

    ARGHHH!!

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Retrieve Printer Name

    Always use [code][/code] tags when posting code.

  9. #9
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Retrieve Printer Name

    I had this sophisticated, yet simple little VB Script, which makes use of the WMI mechanism to obtain a list of all printers and shows some of their attributes.
    Code:
    On Error Resume Next
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_Printer",,48)
    
    For Each objItem in colItems
        if MsgBox( _
    	  "Attributes: " & objItem.Attributes & chr(13) & chr(10) _
    	& "Availability: " & objItem.Availability & chr(13) & chr(10) _
    	& "Caption: " & objItem.Caption & chr(13) & chr(10) _
    	& "Comment: " & objItem.Comment & chr(13) & chr(10) _
    	& "DeviceID: " & objItem.DeviceID & chr(13) & chr(10) _
    	& "DriverName: " & objItem.DriverName & chr(13) & chr(10) _
    	& "ExtendedPrinterStatus: " & objItem.ExtendedPrinterStatus & chr(13) & chr(10) _
    	& "Location: " & objItem.Location & chr(13) & chr(10) _
    	& "Name: " & objItem.Name & chr(13) & chr(10) _
    	& "Network: " & objItem.Network & chr(13) & chr(10) _
    	& "PortName: " & objItem.PortName & chr(13) & chr(10) _
    	& "ServerName: " & objItem.ServerName & chr(13) & chr(10) _
    	& "Shared: " & objItem.Shared & chr(13) & chr(10) _
    	& "ShareName: " & objItem.ShareName & chr(13) & chr(10) _
    	& "Status: " & objItem.Status & chr(13) & chr(10) _
    	& "StatusInfo: " & objItem.StatusInfo & chr(13) & chr(10) _
    	& "SystemCreationClassName: " & objItem.SystemCreationClassName & chr(13) & chr(10) _
    	& "SystemName: " & objItem.SystemName,1 _
    	) = 2 then Exit For
    	
    Next
    The required name should be within the display.
    There are much more properties available, but I have only displayed the most important for identifying.
    The little script runs directly from within a text file with the .vbs ending, but the wMI query mechanism can surely be adapted easily to run within a VB program.

  10. #10
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Retrieve Printer Name

    I've used a XEROX COLOR LASER PRINTER that printed 10 pages per second, and could print 5 different inks on each side at the same time (including magnetic ink). My point, is that PERMISSIONS are the issue.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Retrieve Printer Name

    In my case permissions are not the issue running under XP in admin mode, still get the same results. The printer object just does not return the value keyed in by the user. Instead it gets the physical path\name of the printer. If permissions were the issue then this is what would be blocked as this is what you would need to access the printer. The name keyed in by the user is just for show no permission issues there.
    Always use [code][/code] tags when posting code.

  12. #12
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Retrieve Printer Name

    Please try again with my little script and take regard to the following.
    The caption and the DeviceID seem to be the same always. They change both if you rename the printer.
    If you share the printer you may give it a share name which appears in the network.
    I inspected all our printers and the Caption property reflected always the name as shown to me in the control panel, not the network path.

  13. #13
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Retrieve Printer Name

    Just to be clear I did not try the script as I have no need for this at present. I was referring only to the printer object and permissions.

    The script should be of use to the OP
    Always use [code][/code] tags when posting code.

  14. #14
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Retrieve Printer Name

    That's what I hoped.

  15. #15
    Join Date
    May 2009
    Location
    London
    Posts
    51

    Resolved Re: Retrieve Printer Name

    Thanks guys for your efforts, I have used a bit of WOF's idea with the WIN32_Printer collection (Network Property) to let me know if it is a network printer and then build the printer desciption accordingly (as per DataMiser's suggestion). I tried all of the string properties of the Win32_Printer but none of them gave me the name as displayed in the control panel/printer dialog. Looks like this is as close as I am going to get for the minute.
    Once again thanks.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured