Click here to Skip to main content
15,879,613 members
Articles / Desktop Programming / Windows Forms

WMI Code Creator For Windows Forms for VB.NET

Rate me:
Please Sign up or sign in to vote.
4.93/5 (15 votes)
20 Sep 2012CPOL8 min read 80.4K   4.8K   21   30
This is a project for creating code like the WMI code creator but the output is for VB.NET and Windows Forms instead of for a Console Application.

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, it 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. I am 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 Namespaces 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:

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

To get this in the output box:

VB.NET
"SELECT * FROM CIM_CDROMDrive")

Here is what the current version looks like:

Image 1

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 namespaces 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 listbox 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 your 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 onload 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:

VB.NET
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 without 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 applications to see what gets returned before building a new application, and to help pick any properties you may want to 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 HTAs .

The sample applications took about 20 minutes 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

Other Links

Downloads

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 requires 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)


Written By
CEO 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 Pin
Sueh128-Aug-13 2:16
Sueh128-Aug-13 2:16 
GeneralRe: My vote of 5 Pin
ledtech38-Aug-13 2:51
ledtech38-Aug-13 2:51 
GeneralVS 2012 Version Pin
ledtech318-Sep-12 18:59
ledtech318-Sep-12 18:59 
QuestionMy Vote of 5 Pin
Ted Goulden18-Sep-12 14:06
Ted Goulden18-Sep-12 14:06 
GeneralRe: My Vote of 5 Pin
ledtech318-Sep-12 15:38
ledtech318-Sep-12 15:38 
GeneralRe: My Vote of 5 Pin
Ted Goulden18-Sep-12 18:20
Ted Goulden18-Sep-12 18:20 
GeneralRe: My Vote of 5 Pin
ledtech318-Sep-12 18:25
ledtech318-Sep-12 18:25 
GeneralMy vote of 5 Pin
ILikeCake15-Sep-12 10:45
ILikeCake15-Sep-12 10:45 
GeneralRe: My vote of 5 Pin
ledtech315-Sep-12 10:59
ledtech315-Sep-12 10:59 
GeneralMy vote of 5 Pin
rspercy652-Sep-12 11:06
rspercy652-Sep-12 11:06 
GeneralRe: My vote of 5 Pin
ledtech310-Sep-12 19:15
ledtech310-Sep-12 19:15 
GeneralMy vote of 5 Pin
fredatcodeproject17-Aug-12 2:43
professionalfredatcodeproject17-Aug-12 2:43 
GeneralRe: My vote of 5 Pin
ledtech317-Aug-12 13:58
ledtech317-Aug-12 13:58 
GeneralRe: My vote of 5 Pin
fredatcodeproject21-Aug-12 7:07
professionalfredatcodeproject21-Aug-12 7:07 
GeneralRe: My vote of 5 Pin
Dr Bob22-Aug-12 15:20
Dr Bob22-Aug-12 15:20 
GeneralRe: My vote of 5 Pin
ledtech322-Aug-12 17:02
ledtech322-Aug-12 17:02 
GeneralRe: My vote of 5 Pin
fredatcodeproject22-Aug-12 20:54
professionalfredatcodeproject22-Aug-12 20:54 
GeneralRe: My vote of 5 Pin
ledtech323-Aug-12 1:20
ledtech323-Aug-12 1:20 
GeneralRe: My vote of 5 Pin
ledtech321-Apr-14 5:48
ledtech321-Apr-14 5:48 
GeneralRe: My vote of 5 Pin
fredatcodeproject29-Apr-14 10:43
professionalfredatcodeproject29-Apr-14 10:43 
great
thanks
GeneralRe: My vote of 5 Pin
ledtech329-Apr-14 12:40
ledtech329-Apr-14 12:40 
GeneralMy vote of 5 Pin
Sergio Andrés Gutiérrez Rojas16-Aug-12 15:31
Sergio Andrés Gutiérrez Rojas16-Aug-12 15:31 
GeneralRe: My vote of 5 Pin
ledtech316-Aug-12 19:41
ledtech316-Aug-12 19:41 
Questioncan't find the downloads, skydrive link not working Pin
andeezlemoyo16-Aug-12 9:27
andeezlemoyo16-Aug-12 9:27 
AnswerRe: can't find the downloads, skydrive link not working Pin
ledtech316-Aug-12 10:01
ledtech316-Aug-12 10:01 

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

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