Visual Basic.NET
  Home arrow Visual Basic.NET arrow WMI Programming with Visual Basic.NET: Tip...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

WMI Programming with Visual Basic.NET: Tips and Tricks
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2005-07-19

    Table of Contents:
  • WMI Programming with Visual Basic.NET: Tips and Tricks
  • Retrieving all properties of a WMI class without knowing their names
  • How does the program work?
  • How can we extend the above program to work with all objects?
  • How does this program work?

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    WMI Programming with Visual Basic.NET: Tips and Tricks


    (Page 1 of 5 )

    In my previous article, part four of this series, we looked into managing OS using WMI methods. In this article, we will look into some tips and tricks for using WMI.


    You can download the zip of entire Visual Studio.NET solution (developed for this article) here.

    The first part of my series gives you the details for creating a Visual Studio.NET solution for WMI from scratch.  It will not be repeated anymore in any of the future articles in this series.

    Can we start another application (process) through WMI?

    In all of my examples in the previous part, we worked with windows services (or system services). Now I will start talking about processes. A process is something like a thread which starts execution in its only memory area (application domain). In general, every application starts in the form of a process. Almost all processes can be viewed using the task manager. Some processes may be system processes which run in the background (without having a user interface). Every process gets identified by a process id internally.  We can get the full list of processes by using WMI class “Win32_process”. You can refer to part three to get an entire example of listing processes.

    Now, the question is, can we start our own process (or even an application) using WMI? I hope anyone who works with .NET would definitely know how to do it using the native .NET framework. But now, I would like to do the same thing using WMI. The following program fragment demonstrates this:

    ' Get the object on which the method will be invoked

           DimprocessClassAsNewManagementClass("Win32_Process")

           ' Get an input parameters object for this method

           DiminParamsAsManagementBaseObject = processClass.GetMethodParameters("Create")

           ' Fill in input parameter values

            inParams("CommandLine") = "calc.exe"

           ' Execute the method

           DimoutParamsAsManagementBaseObject = processClass.InvokeMethod("Create", inParams,Nothing)

           DimobjAsObject= outParams("ReturnValue")

           Ifobj.ToString <> "0"Then

                MessageBox.Show("Could not Start, Returned code:" & obj.ToString)

           EndIf

    A brief explanation of above program is included below.

    We create a new process by using WMI class “Win32_Process”. There exists a method “create” within the class “Win32_Process”. That method creates a process at the OS through WMI and not our application. We provide parameters to that object using "inParams" (such as which application to execute as part of the process).

    We execute the “create” method using the “InvokeMethod” of the “ManagementClass” object (currently the object points to a new process). The result of the “InvokeMethod” is returned into “outParams”. If the result is zero, it has successfully managed to invoke an external process. A non-zero result is a failure which would be shown through a message box. A detailed explanation of every return code is provided in the WMI SDK documentation.

    You can understand the above program very easily, if you go through my previous article (part four).  All information about calling a WMI method (including the “InvokeMethod” member) was discussed in detail in that article. 

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


     

    VISUAL BASIC.NET ARTICLES

    - Clocks and Countdowns
    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek