Solved

vb.net and wmi (win32_printer)

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

805 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