Link to home
Start Free TrialLog in
Avatar of derek7467
derek7467

asked on

vb.net and wmi (win32_printer)

I am running the below code to call remote workstation printers that are installed.  This works for my local machine but fails on remote machine.  It errors out with "object reference not set to an instance of this object".  Weird part is i call similar code for remote running processes and it works fine:

Printer Code:

 Dim RC As String = TextBox1.Text
        'Try
        Dim theScope As New ManagementScope("\\" & RC & "\root\cimv2")
        Dim objectQuery As New ObjectQuery("SELECT * FROM Win32_Printer")
        For Each managementObject As ManagementObject In (New ManagementObjectSearcher(theScope, objectQuery)).[Get]()
            Dim str As String = managementObject("Name").ToString()
            lvprinter.Groups.Add(str, str)

            Dim item As New ListViewItem("Driver")
            item.SubItems.Add(managementObject("DriverName").ToString())
            item.Group = lvprinter.Groups(str)
            lvprinter.Items.Add(item)

            item = New ListViewItem("PortName")
            item.SubItems.Add(managementObject("PortName").ToString())
            item.Group = lvprinter.Groups(str)
            lvprinter.Items.Add(item)

            item = New ListViewItem("Status")
            item.SubItems.Add(managementObject("Status").ToString())
            item.Group = lvprinter.Groups(str)
            lvprinter.Items.Add(item)
        Next

Open in new window


Code for calling remote processes that work fine:

 Dim RC As String = TextBox1.Text
        Try
            ListView2.Items.Clear()

            Dim theScope As New ManagementScope("\\" & RC & "\root\cimv2")
            Dim objectQuery As New ObjectQuery("SELECT * FROM Win32_Process")
            ListView2.Groups.Add(New ListViewGroup("Processes", "Process List"))
            For Each managementObject As ManagementObject In (New ManagementObjectSearcher(theScope, objectQuery)).[Get]()
                Dim item As ListViewItem = ListView2.Items.Add(managementObject("Name").ToString())
                item.SubItems.Add(managementObject("ProcessId").ToString())
                item.Group = ListView2.Groups("Processes")
            Next
        Catch ex As Exception
        End Try

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of derek7467
derek7467

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial