WMI Code Creator For Windows Forms For VB.Net

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 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.

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.

GUIWmiCodeCreator

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

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 Update:

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

About pcsxcetrasupport3

My part time Business, I mainly do system building and system repair. Over the last several years I have been building system utility's in vb script , HTA applications and VB.Net to be able to better find the information I need to better understand the systems problems in order to get the systems repaired and back to my customers quicker.
This entry was posted in CodeProject, VB.net and tagged . Bookmark the permalink.