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 string
s 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 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,
- 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) - change font size to 10 for the higher resolution screens.
- set scroll bars to both (solves problems later when something doesn’t fit)
- 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:
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