Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
programming4us
Windows 7

Advanced Windows 7 Programming : Working in the Background - DEVELOPING TRIGGER-START SERVICES (part 4)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
3/8/2014 8:24:20 PM
4.2. Detecting the Operating System Version

Because a trigger-start service doesn't run under older versions of Windows, you want to be sure that users install it as a trigger-start service under only the right version of the operating system. The service will still work with other versions of Windows, but not as a trigger-start service. Consequently, you can still install it with older versions, but you can't configure it for a trigger-start service. To accommodate this need, the example handles the AfterInstall event. Use these steps to create the event handler:

  1. Select the TriggerStartServiceInstall entry in the TriggerStartServiceInstaller Design View window.

  2. In the Properties window, click Events. You'll see a list of events associated with the TriggerStartServiceInstall object.

  3. Double-click the AfterInstall event entry. Visual Studio automatically creates the required event handler for you.

Now that you have the event handler in place, it's time to add some code to it. Listing 2 shows the event handler code.

Example 2. Checking the operating system version
private void TriggerStartServiceInstaller_AfterInstall(
object sender, InstallEventArgs e)
{
// Check the operating sytem version.
if (Environment.OSVersion.Version >= new Version(6, 1))

// Configure the trigger for the service.
this.ConfigurePortTrigger(TriggerStartServiceInstall.ServiceName);
else

// This is the wrong version of the operating system.
throw new NotSupportedException("Wrong OS Version");
}

The code begins by checking the operating system version supplied by Environment.OSVersion.Version against a Version object that has a major version number of 6 and a minor version number of 1, which works for either Windows 7 or Windows Server 2008 R2 (but not the original version of Windows Server 2008). You can see a list of version numbers at http://msdn.microsoft.com/library/ms724832.aspx.

When the operating system version is new enough, the code calls ConfigurePortTrigger(), which is described in Listing 1, to create the required trigger-start service changes for the service. The TriggerStartServiceInstall.ServiceName property supplies the required service name. Otherwise, the code raises a NotSupportedException() exception and exits.

4.3. Defining the Service Code

The emphasis of this example is on creating a trigger-start service, so the service code isn't much to look at. In this case, the service code outputs one of two messages to the event log to describe the state of Port 23. The service code appears in TriggerStartService.CS. Listing 3 shows the code used in this case.

Example 3. Adding event log entries to monitor the port
protected override void OnStart(string[] args)
{
// Add a starting event log entry.
WriteEventLog("Telnet Port 23 Opened");
}

protected override void OnStop()
{
// Add a stopping event log entry.
WriteEventLog("Telnet Port 23 Closed");
}

private void WriteEventLog(String Message)
{
// Use the System event log.
EventLog.Log = "System";

// Send a message to the event log.
EventLog.WriteEntry(Message, EventLogEntryType.Information);
}

The code needs to override two event handlers: OnStart() and OnStop(). In both cases, the event handlers call WriteEventLog() with the message to place in the event log.

The default configuration for writing event log entries is to use the Application log. However, service entries normally appear in the System log, so the WriteEventLog() method begins by setting the EventLog.Log property to System. The code then calls EventLog.WriteEntry() with the message and the type of entry to create, which is EventLogEntryType.Information in this case.

Other -----------------
- Advanced Windows 7 Programming : Working in the Background - ADVANTAGES OF WORKING IN THE BACKGROUND
- Microsoft Visio 2010 : Importing Graphics (part 6) - Importing AutoCAD Drawings - Manipulating an Imported AutoCAD Drawing and Adding Furniture
- Microsoft Visio 2010 : Importing Graphics (part 5) - Importing AutoCAD Drawings - Inserting an AutoCAD File
- Microsoft Visio 2010 : Importing Graphics (part 4) - Adding Excel Charts to Your Diagrams, Importing Vector Graphics
- Microsoft Visio 2010 : Importing Graphics (part 3) - Adding Clip Art to Your Diagrams
- Microsoft Visio 2010 : Importing Graphics (part 2) - Using Images as Shapes in Visio - Handling Bitmaps and Jaggies
- Microsoft Visio 2010 : Importing Graphics (part 1) - Using Images as Shapes in Visio - Working with Images
- Microsoft Visio 2010 : Using Special Shape Features (part 2) - Control Handles , Hyperlinks, Action Tags
- Microsoft Visio 2010 : Using Special Shape Features (part 1) - Right-Click Actions,Shape Data Fields
- Microsoft Visio 2010 : Working with Text (part 3) - Text Resizing Behavior
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server