Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - WMI Programming with Visual Basic.NET: Com...
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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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: Combining with Windows Services
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2005-08-08

    Table of Contents:
  • WMI Programming with Visual Basic.NET: Combining with Windows Services
  • Designing and preparing the essentials
  • Implementing the logic
  • Adding the installers and setup
  • Installing, executing and testing the windows service

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    WMI Programming with Visual Basic.NET: Combining with Windows Services - Implementing the logic


    (Page 3 of 5 )

    Add these two statements at the class level (above OnStart method):

    DimqueryAsWqlEventQuery

         DimwatcherAsManagementEventWatcher

    Add the following code within the “OnStart” method

    query =NewWqlEventQuery( _

                          "__InstanceModificationEvent", _

                         NewTimeSpan(0, 0, 1), _

                          "TargetInstance isa ""Win32_Service""")

            watcher =NewManagementEventWatcher(query)

           AddHandlerwatcher.EventArrived,AddressOfHandleEvent

     

           ' Start listening

            watcher.Start()

            EventLog.WriteEntry("WMIService", "Started")

    Add the following event handler with the same class:

    PublicSubHandleEvent(ByValsenderAsObject,ByValeAsEventArrivedEventArgs)

           Try

               DimevAsManagementBaseObject = e.NewEvent

               DimServiceNameAsString=CType(ev("TargetInstance"), ManagementBaseObject)("DisplayName")

               DimStatusAsString=CType(ev("TargetInstance"), ManagementBaseObject)("State")

               DimcnAsNewSqlConnection("data source=.;initial catalog=northwind;user id=sa")

                cn.Open()

               DimcmdAsNewSqlCommand("insert into ServiceHistory(ServiceName,Status) values ('" & ServiceName & "','" & Status & "')", cn)

                cmd.ExecuteNonQuery()

                cmd.Dispose()

                cn.Close()

           CatchexAsException

                EventLog.WriteEntry("WMIService", ex.Message)

           EndTry

       EndSub

    Add the following lines to the “OnStop” method:

    watcher.Stop()

                EventLog.WriteEntry("WMIService", "Stopped")

    Make sure you change the connection string according to your requirements. Rebuild the solution to ensure that there exist no errors.  The project should be built successfully at this moment.

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


       · Hello guys, this is my article on working with WMI together with Windows Services....
       · Very well explained and detailed article. I have tried to build services and the...
       · Hi, can you try with some simple windows services and then go for the article!
     

    VISUAL BASIC.NET ARTICLES

    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...
    - More on Controlling Windows Fax Services Usi...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway