Solved

vb.net and wmi (win32_printer)

Posted on 2014-11-10
1
311 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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:…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A short film showing how OnPage and Connectwise integration works.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

948 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now