?
Solved

vb.net and wmi (win32_printer)

Posted on 2014-11-10
1
Medium Priority
?
335 Views
Last Modified: 2014-11-10
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

0
Comment
Question by:derek7467
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
derek7467 earned 0 total points
ID: 40433015
Figured it out, status isnt always a result so i had to wrap in a try-catch

try
query printers
Catch exception As Exception
End Try
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question