Click here to Skip to main content
Click here to Skip to main content

WMI Code Creator For Windows Forms for VB.NET

By , 20 Sep 2012
Rate this:
Please Sign up or sign in to vote.

The Project Reason

Anyone that has used the WMI Code Creator knows how fast you can get up and going. Since most of my programs that use WMI are Windows forms it was becoming a time consuming thing to create the code with the selected properties from the WMI Code Creator and then port that code to a Windows forms style application.

So I decided to see if I could build my own code creator for faster builds. When you have 15 properties is takes a lot of time, and to make sure you spell them correctly or you end up with a "Not Found" error for a property name that was misspelled.

This project is still a work in progress. There are a few known Issues.

Known Issues

If you start the program on a Windows Vista system with UAC Enabled and just click on the exe or click the run button in the IDE then it takes Approximately 60 seconds for the main form to load versus about 4 seconds If you right click and "Run as Administrator" from the Release folder.

If UAC is enabled you may need to Run The Code Creator as a Administrator, or it may error out. I have not yet added any Code to Auto Elevate.

(something keeps re-enabling my UAC, still looking into that problem)

Not All classes return Information.

The current version only works with the text box output. Still working on the ability to output for the data grid view and list box.

Update:

5/6/2012

Now supports datagrid view, and listbox.

The Building of the Program

I first started out by looking at the WMI Code Creator wanting to keep some of the same feel of that program.

My first version had a checked list box, but you had to click twice for every property you wanted to select. So I later changed that to a regular list box. One left click does it.

Then I used a rich text box but you don’t get the instant right click copy ability so I changed that to a regular text box.(was having to use CTL+ C to do it while testing)

I borrowed the code from a previous project for filling the Name spaces and the Classes.

The code formatting was a matter of looking at a regular programs code and then looking to see how to display the code on output from the Code Creator. (lots of trial and error)

Another problem I ran into was loading the property’s into the list box. The first version would pop up a "Not Found " error, it took a bit to figure out it wasn’t finding the class if it wasn’t a CimV2 class, not the property name that usually pops up that error. So I went back to the MSDN site for the System. Management and was able to figure it out from there. (See the project code in the download for better understanding)

As I was building it I had to look at it as 2 different sets of code. The first for building the strings for the output and the second for how the output would need to display.

One of the problems I ran into was, where the output needed to have double quotes around it, a quick internet search led me in the right direction but didn’t give the exact answer.

The Answer was to,

Do this:

strbuilder.AppendLine(ChrW(34) & "SELECT * FROM " & SCL.ToString & ChrW(34) & ")")

To get this in the output box:

"SELECT * FROM CIM_CDROMDrive")

Here is what the current version looks like.

Using the program

To use this program right click and run as admin (just to avoid some problems).

After the splash screen closes, the main form will open and the Namespace list will be filled.

Select a namespace, then the Classes list will fill. The number above tells how many classes or name spaces were found.(still need to work on that part)

Once you have selected a class, the list box will fill with the properties. Select the properties you want by left clicking on them(unless your buttons are reversed), if the property name is highlighted, then it is selected. You can left click again to unselect.(the select method in the list box properties page is multi simple)

Then Just click on the Create code button and the output text box will fill.

Next:

Create a project, add a button control and a Text box to your new form. Name you button and set the text for it.

Then on the textbox make it a multiline and size it to your form. Next set the textbox properties.

For me I ,

  1. Set Anchor to all 4 after setting the size of the textbox. (so the text box with grow or shrink as the form is resized)
  2. change font size to 10 For the higher resolution screens.
  3. Set scroll bars to both (Solves problems later when something doesn’t fit)
  4. Set Word wrap to false (Output gets confusing at times when it wraps to the next line)

Next:

If you want to use an on load event for your program then double click on the form to create it. I prefer to have mine at the top of the code.

Then click on your button control to get the handler for that.

Next:

Add some room above the "Public Class Form1" to add the Import Statements.

Then copy the Import Statements from the Code Creator to the area you just made above.

Then Add the References for the Import Statements. (Doing that now will eliminate the squiggly lines that would appear otherwise in the next step.)

Next:

Copy the code from the code creator to the button click handler.

Starting at: "Dim strbuilder As New StringBuilder"

Ending at:

MessageBox.Show("An error occurred while querying for WMI data:"& err.Message)
End Try

Next:

If your program (Class) uses the DTMF date time (Output Looks like this with out conversion , 20120426215915.359000-300 )then copy the function for converting it to a normal date/time. I am including a Program that shows how to use the function. Not all classes use that form of date, you may not need to add the function. (The TS app errored when I added it for that one, helps to know what the output should look like.)

As long as there are no errors, build and run your new program.

Tweak as desired : )

Again These are management programs if you are running Vista or above and get an initialization error then right click and run the program as Administrator.

Conclusion:

Again, not all classes will return information, you may want to use one of the following application to see what gets returned before building a new application, and to help pick any properties you may want use. Some property names never return anything, so no point cluttering up your output with the extra property names. The selected class also may not return anything at all.

Programs to test out:

The WMI Code creator, Scriptomatic 2.0, or My ClassNFOMatic24.HTA that I will add as a separate download. My ClassNFOMatic24.HTA is a Highly modified version of the Scriptomatic 2.0 that will list all classes. I will also add in that download a registry file for adding the right click "Run as Admin" for HTA’s .

The sample applications took about 20 minuets or less to complete using the code creator.

Please let me know what you think about this. I will get the rest of it going as I find time.

References

MSDN:

ManagementBaseObject.SystemProperties

ManagementDateTimeConverter.ToDateTime

ManagementObject.Path Property

ManagementPath Class

Win32_TSAccount class

Using double quotes in strings

Other links:

Run AS For HTA

Downloads:

Scriptomatic 2.0 download

WMI Code Creator v1.0 Link to CNet Download.com (No Longer On Microsoft downloads)

Sample Files from my Sky drive

(File Names On my Sky Drive: ClassNFOMatic24.zip, GUI WMI exe file.zip, GUIWMI Project FIles.zip, SampleAppProjectFiles.zip, SampleApps-exes.zip

Latest project file names are:

GUI WMI Code Creator-1.4.2.8.zip (new EXE file only)

GUI_WMI_Code_Creator_1418_Project.zip

Latest Update:

Update 9/19/2012

Converted the project Using VS 2012 Pro (Trial) (still VB.net) and Added to downloads.

Update 9/11/2012

Updated link for the Microsoft Wmi Code Creator.

Now points to Download.com was pointing at Microsoft downloads.

Updated 8/16/2012

New version 1.4.2.9

Added require Administrator, uploaded files to Code Project.

Updated 5/23/2012

New version 1.4.2.8

Added an If statement to the convert date function to handle the error when a null date is encountered. Now will return N/A for a null date.
In the Text box version only, implemented the code for working with Array list and for the date/time
So user doesn’t have to add that part ,If that type of property was selected. Works more like the scriptomatic now.

New file name listed above for the exe only. Now Available from the skydrive.

Updated:5/9/2012

Had some formatting issues, I apologies to everyone for all of the updates.

Latest Version: 1.4.1.8  5/6/2012

Initial program version is : 1.4.1.0 Date: 5/5/2012

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

ledtech3
PC's Xcetra
United States United States
My first experience with computers was when my mom gave a Timex Sinclair 1000 to me for Christmas some time in the late 70's (I still have it)There I learned to copy code from magazines to save to cassette tapes for playing games.
 
Since then I have dabbled in:
Basic,Qbasic,ruby,python,Java Script, HTML, CSS, C#, C++, Perl, and a few other I can't think of off hand.
Now I Mainly work with VB Script and VB.Net
I Prefer to build programs that make use of the GUI so I don't have to remember all of the syntax for console apps. I realy don't care much for HTML because of the way you build and then run to see if it looks right. Also the new WPF is to much like HTML so I steer clear of it for now.
Most of what I build is for getting information from a system to use in system repair.I make heavy use of the WMI classes. Why reinvent something.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberSueh128-Aug-13 2:16 
GeneralRe: My vote of 5 Pinmemberledtech38-Aug-13 2:51 
GeneralVS 2012 Version Pinmemberledtech318-Sep-12 18:59 
QuestionMy Vote of 5 PinmemberTed Goulden18-Sep-12 14:06 
GeneralRe: My Vote of 5 Pinmemberledtech318-Sep-12 15:38 
GeneralRe: My Vote of 5 PinmemberTed Goulden18-Sep-12 18:20 
GeneralRe: My Vote of 5 [modified] Pinmemberledtech318-Sep-12 18:25 
GeneralMy vote of 5 PinmemberILikeCake15-Sep-12 10:45 
GeneralRe: My vote of 5 Pinmemberledtech315-Sep-12 10:59 
GeneralMy vote of 5 Pinmemberrspercy652-Sep-12 11:06 
GeneralRe: My vote of 5 Pinmemberledtech310-Sep-12 19:15 
GeneralMy vote of 5 Pinmemberfredatcodeproject17-Aug-12 2:43 
GeneralRe: My vote of 5 Pinmemberledtech317-Aug-12 13:58 
GeneralRe: My vote of 5 Pinmemberfredatcodeproject21-Aug-12 7:07 
GeneralRe: My vote of 5 PinmemberDr Bob22-Aug-12 15:20 
GeneralRe: My vote of 5 Pinmemberledtech322-Aug-12 17:02 
GeneralRe: My vote of 5 Pinmemberfredatcodeproject22-Aug-12 20:54 
GeneralRe: My vote of 5 Pinmemberledtech323-Aug-12 1:20 
GeneralMy vote of 5 PinmemberSergio Andrés Gutiérrez Rojas16-Aug-12 15:31 
GeneralRe: My vote of 5 Pinmemberledtech316-Aug-12 19:41 
Questioncan't find the downloads, skydrive link not working Pinmemberandeezlemoyo16-Aug-12 9:27 
AnswerRe: can't find the downloads, skydrive link not working Pinmemberledtech316-Aug-12 10:01 
GeneralRe: can't find the downloads, skydrive link not working Pinmemberandeezlemoyo16-Aug-12 10:22 
Questionnice PinmemberCIDev7-Jun-12 4:41 
AnswerRe: nice Pinmemberledtech37-Jun-12 4:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 20 Sep 2012
Article Copyright 2012 by ledtech3
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid