Solved

vb.net and wmi (win32_printer)

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

21 Experts available now in Live!

Get 1:1 Help Now