Link to home
Start Free TrialLog in
Avatar of Shane Russell
Shane RussellFlag for United Kingdom of Great Britain and Northern Ireland

asked on

WMI in vb 2005 tutorials or examples ?

I've done quite a bit of WMI in vbscripts and vb 6 apps and now with the move into the dot net area I would like to know how you are supposed to do any wmi with it, Am I missing any references if so what are they and how do I add them and maybe a few examples of some wmi would be great :)

thx
Avatar of Mikael Jansson
Mikael Jansson
Flag of Sweden image

Hi, here is a small and simple example that just get the printernames from WIN32_Printer and adds them to a listbox.

Here is what to do:
1. Start a new project and add a listbox on the form (use standard name listbox1)
2. in solution explorer right click on the project and click on "Add reference", add "System.Management" to the project
3. put "Imports System.Management on top of the code outside the class definition
4. put the code below in a procedure for a button or something else that triggers it.

--- Code example start -----

        Dim query As ObjectQuery
        Dim searcher As ManagementObjectSearcher
        Dim scope As ManagementScope = New ManagementScope
        Dim strComputerName As String = "."
        Dim queryCollection As ManagementObjectCollection

        scope = New ManagementScope("\\" & strComputerName & "\root\cimv2")
        scope.Connect()
        query = New ObjectQuery("SELECT * FROM Win32_Printer")
        searcher = New ManagementObjectSearcher(scope, query)
        queryCollection = searcher.Get()
        For Each m As ManagementObject In queryCollection
            ListBox1.Items.Add(m("caption"))
        Next
--- Code example end ---

Good luck!!

br

/ Mikael
Avatar of Shane Russell

ASKER

I assume that to do a querry using WMI on the other WMI classes you just replace the win32_printer class with whatever class you want to use , right ?

Also I dont spose you have a tutorial site or an example on how to connect to a remote machine and do certain things using vb 2005, I saw on the ms site they have an example for regular vb or vbscript but obviously vb 2005 does it differently.
ASKER CERTIFIED SOLUTION
Avatar of Mikael Jansson
Mikael Jansson
Flag of Sweden image

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
Additional info (by incident I submitted before finnish ;-)

In the second example about TCPIPPrinterPort I havent declared all the variables that are used to fetch info from m(), but I think you get the point anyhow.

Good luck

br

/ Mikael
ok I have tried changing the win32 class but I get an error that says invalid query

I have changed it to win32_cdromdrive and have also tried

win32_NetworkAdaptor
win32_NetworkAdaptorSetting or settings not sure which one it was off the top of my head now

And I keep getting the same error message ( I am using the original code you posted above with the alteration of the class name
ie

        Dim query As ObjectQuery
        Dim searcher As ManagementObjectSearcher
        Dim scope As ManagementScope = New ManagementScope
        Dim strComputerName As String = "."
        Dim queryCollection As ManagementObjectCollection

        scope = New ManagementScope("\\" & strComputerName & "\root\cimv2")
        scope.Connect()
        query = New ObjectQuery("SELECT * FROM Win32_CDROMDrive")
        searcher = New ManagementObjectSearcher(scope, query)
        queryCollection = searcher.Get()
        For Each m As ManagementObject In queryCollection
            ListBox1.Items.Add(m("caption"))
        Next
ok Thanks for that I got it figured out now and I figured out why it was erroring out, lets just say PEBKAC :D
Im glad to hear that yo solved it, it is kind of confusing in the beginning and its easy to get lost where the actual error is,
for example you can get permission errors and other "unspecified errors", a tip there is if you get those errors running ¨
against a remote computer, try to run it locally first so you get rid of "simple" errors and can secure that its not a security issue.

Good luck with your WMI :-)

/ Mikael
Thanks :)

Going to ask another question regarding API's on vb 2005 :)

Just I dont have the core book for vb 2005 so it would be useful to know where and how to use the main parts of vb 2005 in order to make my app in vb 2005 obviously :)