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)

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
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
Trailers Game
- The Banner Saga 2 [PS4/XOne/PC] PC Launch Trailer
- Welkin Road [PC] Early Access Trailer
- 7th Dragon III Code: VFD [3DS] Character Creation Trailer
- Human: Fall Flat [PS4/XOne/PC] Coming Soon Trailer
- Battlefleet Gothic: Armada [PC] Eldar Trailer
- Neon Chrome [PS4/XOne/PC] PC Release Date Trailer
- Rocketbirds 2: Evolution [Vita/PS4] Launch Trailer
- Battleborn [PS4/XOne/PC] 12 Min Gameplay Trailer
- 7 Days to Die [PS4/XOne/PC] Console Trailer
- Total War: Warhammer [PC] The Empire vs Chaos Warriors Gameplay Trailer
- Umbrella Corps [PS4/PC] Mercenary Customization Trailer
- Niten [PC] Debut Trailer
- Stellaris [PC] Aiming for the Stars - Dev. Diary Trailer #1
- LawBreakers [PC] Dev Diary #4: Concept Art Evolutions
programming4us programming4us
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
 
programming4us
Natural Miscarriage
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Game Trailer