Results 1 to 16 of 16

Thread: Printers' name in Password protected server

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    7

    Printers' name in Password protected server

    Hi everyone, I'm new here
    I looked hours on the net to find the answer to my prb...
    I need to check all the printers of my faculty by sending a test page to everyone of them. And I want to do that with a little program in VB...
    All the printers are in the same server.

    I've found this piece of code :
    It should get the names and put them in a list box (the following code) and them send the print request (any suggestion about that will be very welcome too )

    Code:
            Dim moReturn As Management.ManagementObjectCollection
            Dim moSearch As Management.ManagementObjectSearcher
            Dim mo As Management.ManagementObject
    
            moSearch.Scope.Path.DefaultPath.Server() = "server_name"
    
            Try
                moSearch = New Management.ManagementObjectSearcher("Select * from Win32_Printer")
                moReturn = moSearch.Get
                For Each mo In moReturn
                    ListBox1.Items.Add(mo("name"))
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                moReturn = Nothing
                moSearch = Nothing
                mo = Nothing
            End Try
    and when I'm running it I get " Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) "
    PS : I have the password of course, and I enter it in the terminal before executing the code.

    If someone can halp me it will great.
    Thanks a lot
    Last edited by daka90; Sep 9th, 2009 at 11:26 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Printers' name in Password protected server

    Thread moved from 'VB6 and Earlier' forum to 'VB.Net' (VB2002 and later) forum

    For the benefit of others, daka90's profile days VB 2008

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Printers' name in Password protected server

    When you say it is password protected, do you mean that you need to login to the server with a username and password?

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Printers' name in Password protected server

    Is the server and the computer running the query in a domain?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    7

    Re: Printers' name in Password protected server

    When you say it is password protected, do you mean that you need to login to the server with a username and password?
    Yes, exactly.

    Is the server and the computer running the query in a domain?
    Yes. I guess.
    This is how I connect to the server to print from my computer to 12 printers
    in the run window : \\prtw
    then it's asking for usr and psw.
    the username is built like that :usr= "TD-CSF\my_username"
    and then the psw

    Tks for replying

    PS: sorry for posting in the wrong section

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Printers' name in Password protected server

    Is "TD-CSF" a domain name or a computer name? If it's a computer name then you're not in a domain environment. The reason why I ask if you're in a domain because if you're, there is another way to get the shared printer names using System.DirectoryServices instead of System.Management namespace. Using directoryservices, you don't have to provide a username and password, and it's much faster too.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  7. #7
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Printers' name in Password protected server

    You'll need to pass the credentials as part of the query:

    Code:
                    Dim connection As New ConnectionOptions
                    connection.Username = userNameBox.Text
                    connection.Password = passwordBox.Text
                    connection.Authority = "ntlmdomain:DOMAIN"
    
                    Dim scope As New ManagementScope( _
                        "\\FullComputerName\root\CIMV2", connection)
                    scope.Connect()
    
                    Dim query As New ObjectQuery( _
                        "SELECT * FROM Win32_Printer") 
    
                    Dim searcher As New ManagementObjectSearcher(scope, query)
    Note: I created this code using the WMI Code Creator from Microsoft. If you plan on using a lot of WMI, I highly recommend getting that.

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    7

    Re: Printers' name in Password protected server

    Is "TD-CSF" a domain name or a computer name? If it's a computer name then you're not in a domain environment. The reason why I ask if you're in a domain because if you're, there is another way to get the shared printer names using System.DirectoryServices instead of System.Management namespace. Using directoryservices, you don't have to provide a username and password, and it's much faster too.
    I'm 100% sure now. I checked the property of each printer
    and it's always giving me the same domain "TD-CSF" and computer name"prtw"

    Negativ0 I tried your code like this:
    Code:
    Dim connection As New ConnectionOptions
                    connection.Username = userNameBox.Text
                    connection.Password = passwordBox.Text
                    connection.Authority = "ntlmdomain:TD-CSF"
    
                    Dim scope As New ManagementScope( _
                        "\\prtw\root\CIMV2", connection)
                    scope.Connect()
    
                    Dim query As New ObjectQuery( _
                        "SELECT * FROM Win32_Printer") 
    
                    Dim searcher As New ManagementObjectSearcher(scope, query)
    But still no success
    I'm getting "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))" at this line : scope.Connect()

    tks guys

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Printers' name in Password protected server

    I wrote this function a while ago to get data from all the printers at my work place. Again, this function will work only in a domain environment. If your network is a workgroup, it will not work. Try it out.
    Code:
    'ToDo: Add a reference of System.DirectoryServices to your project
    
    Imports System.DirectoryServices
    
    Public Shared Function GetNetworkPrintersInfo() As DataTable
            'Variable declaration
            Dim prtTable As New DataTable("NetworkPrinters")
            Dim dirEntry As DirectoryEntry = Nothing
            Dim dirSearcher As DirectorySearcher = Nothing
            Dim resultCollection As SearchResultCollection = Nothing
    
            'Prepare the printer datatable
            With prtTable.Columns
                .Add("DirectoryServiceName", GetType(String))   'cn
                .Add("ServerName", GetType(String))             'serverName
                .Add("Name", GetType(String))                   'printerName
                .Add("NetworkName", GetType(String))            'uNCName
                .Add("ShareName", GetType(String))              'printShareName
                .Add("Comments", GetType(String))               'description
                .Add("Model", GetType(String))                  'driverName
                .Add("Location", GetType(String))               'location
                .Add("Port", GetType(String))                   'portName
                .Add("PrinterLanguage", GetType(String))        'printLanguage
                .Add("InstalledMemory", GetType(Integer))       'printMemory
                .Add("PagesPerMinute", GetType(Integer))        'printPagesPerMinute
                .Add("MaxResolution", GetType(Integer))         'printMaxResolutionSupported      
                .Add("SupportsCollation", GetType(Boolean))     'printCollate
                .Add("SupportsColor", GetType(Boolean))         'printColor
                .Add("SupportsDuplex", GetType(Boolean))        'printDuplexSupported
                .Add("SupportsStapling", GetType(Boolean))      'printStaplingSupported
            End With
            Try
                'search for printers
                dirEntry = New DirectoryEntry("")
                dirSearcher = New DirectorySearcher(dirEntry)
                With dirSearcher
                    .PageSize = 50
                    .Filter = "objectCategory=printQueue" ' search filter
                    .PropertyNamesOnly = True
                    .PropertiesToLoad.Add("Name")
                    .SearchScope = SearchScope.Subtree
                End With
                resultCollection = dirSearcher.FindAll()
                'Get results
                Dim entry As DirectoryEntry = Nothing
                Dim row As DataRow = Nothing
                Dim obj As Object = Nothing
                For Each result As SearchResult In resultCollection
                    entry = result.GetDirectoryEntry
                    row = prtTable.NewRow()
    
                    obj = entry.Properties("cn").Value
                    If obj IsNot Nothing Then
                        row("DirectoryServiceName") = obj.ToString
                    Else
                        row("DirectoryServiceName") = String.Empty
                    End If
    
                    obj = entry.Properties("serverName").Value
                    If obj IsNot Nothing Then
                        row("ServerName") = obj.ToString
                    Else
                        row("ServerName") = String.Empty
                    End If
    
                    obj = entry.Properties("printerName").Value
                    If obj IsNot Nothing Then
                        row("Name") = obj.ToString
                    Else
                        row("Name") = String.Empty
                    End If
    
                    obj = entry.Properties("uNCName").Value
                    If obj IsNot Nothing Then
                        row("NetworkName") = obj.ToString
                    Else
                        row("NetworkName") = String.Empty
                    End If
    
                    obj = entry.Properties("printShareName").Value
                    If obj IsNot Nothing Then
                        row("ShareName") = obj.ToString
                    Else
                        row("ShareName") = String.Empty
                    End If
    
                    obj = entry.Properties("description").Value
                    If obj IsNot Nothing Then
                        row("Comments") = obj.ToString
                    Else
                        row("Comments") = String.Empty
                    End If
    
                    obj = entry.Properties("driverName").Value
                    If obj IsNot Nothing Then
                        row("Model") = obj.ToString
                    Else
                        row("Model") = String.Empty
                    End If
    
                    obj = entry.Properties("location").Value
                    If obj IsNot Nothing Then
                        row("Location") = obj.ToString
                    Else
                        row("Location") = String.Empty
                    End If
    
                    obj = entry.Properties("portName").Value
                    If obj IsNot Nothing Then
                        row("Port") = obj.ToString
                    Else
                        row("Port") = String.Empty
                    End If
    
                    obj = entry.Properties("printLanguage").Value
                    If obj IsNot Nothing Then
                        row("PrinterLanguage") = obj.ToString
                    Else
                        row("PrinterLanguage") = String.Empty
                    End If
    
                    obj = entry.Properties("printMemory").Value
                    If obj IsNot Nothing Then
                        row("InstalledMemory") = CInt(obj)
                    Else
                        row("InstalledMemory") = 0
                    End If
    
                    obj = entry.Properties("printPagesPerMinute").Value
                    If obj IsNot Nothing Then
                        row("PagesPerMinute") = CInt(obj)
                    Else
                        row("PagesPerMinute") = 0
                    End If
    
                    obj = entry.Properties("printMaxResolutionSupported").Value
                    If obj IsNot Nothing Then
                        row("MaxResolution") = CInt(obj)
                    Else
                        row("MaxResolution") = 0
                    End If
    
                    obj = entry.Properties("printCollate").Value
                    If obj IsNot Nothing Then
                        row("SupportsCollation") = CBool(obj)
                    Else
                        row("SupportsCollation") = False
                    End If
    
                    obj = entry.Properties("printColor").Value
                    If obj IsNot Nothing Then
                        row("SupportsColor") = CBool(obj)
                    Else
                        row("SupportsColor") = False
                    End If
    
                    obj = entry.Properties("printDuplexSupported").Value
                    If obj IsNot Nothing Then
                        row("SupportsDuplex") = CBool(obj)
                    Else
                        row("SupportsDuplex") = False
                    End If
    
                    obj = entry.Properties("printStaplingSupported").Value
                    If obj IsNot Nothing Then
                        row("SupportsStapling") = CBool(obj)
                    Else
                        row("SupportsStapling") = False
                    End If
    
                    prtTable.Rows.Add(row)
                    obj = Nothing
                    entry.Dispose()
                    entry = Nothing
                Next
                dirEntry.Dispose()
                dirSearcher.Dispose()
                resultCollection.Dispose()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            Return prtTable
        End Function
    When you call the function, it will return a datatable (if it works) which you can bind to your listbox to display the data.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  10. #10

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    7

    Re: Printers' name in Password protected server

    Tks for the code stanav. But the main prb is the connection to the server. I cant get any information if I cant connect to it...
    I don't get why the code of negative0 didn't work and return the same access denied...

  11. #11

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    7

    Re: Printers' name in Password protected server

    I need help people. Please any suggestion on the prb ? It should not be so difficult to get a list of printers' name in the same domain...

  12. #12
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Printers' name in Password protected server

    Have you tried with a user that has admin rights on the box? I got the same error when the user did not have admin rights on the box.

  13. #13

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    7

    Re: Printers' name in Password protected server

    I'm supposed tio have admin rights for those printers... It would be strange that a login via the "DOS" terminal works and a "programaticaly" login don't...
    ...
    Could the prb be that I'm already logged in? (the prg doesn't check that) And the server doesn't allow me in a second time so I get "access denied" ?
    how do I check that, it could be a good idea anyway?

  14. #14
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Printers' name in Password protected server

    What do you mean that a "DOS" login works?

  15. #15

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    7

    Re: Printers' name in Password protected server

    I have to do that
    in the run window : \\prtw
    then it's asking for usr and psw.
    the username is built like that :usr= "TD-CSF\my_username"
    and then the psw
    in order to get acces to the printers and send a test page.
    its a login to the domain td-csf and the computer name prtw... Or am I wrong?

  16. #16
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Printers' name in Password protected server

    The WMI code does not use the same security as the Shared Printers. The WMI code will get all printers on a machine, shared or not. I think you need to look at Stanav's code and see if you can provide credentials that way. You could also compile that into an exe and use the Run As functionality and run that under your domain credentials to see if that works.

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