Click here to Skip to main content
15,883,883 members
Articles / Programming Languages / Visual Basic

Windows 7 new features: Step by step in VB.NET and C#

Rate me:
Please Sign up or sign in to vote.
4.85/5 (88 votes)
31 Aug 2010CPOL9 min read 179.1K   4.7K   211  
Explaining all the new must have features in Windows 7 to make your application look shiny and professional, like the new features of the task bar and more.
// Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Runtime.ConstrainedExecution;
using System.Collections.Generic;

namespace Microsoft.WindowsAPICodePack.ExtendedLinguisticServices
{

    /// <summary>
    /// Converts byte arrays containing Unicode null-terminated strings into .NET string objects.
    /// </summary>
    public class NullTerminatedStringFormatter : IMappingFormatter<string>
    {
        /// <summary>
        /// Converts a single <see cref="MappingDataRange">MappingDataRange</see> into a string, stripping the trailing null character.
        /// If the string doesn't contain null characters, the empty string is returned.
        /// </summary>
        /// <param name="dataRange">The <see cref="MappingDataRange">MappingDataRange</see> to convert</param>
        /// <returns>The resulting string</returns>
        public string Format(MappingDataRange dataRange)
        {
            byte[] data = dataRange.GetData();
            if ((data.Length & 1) != 0)
            {
                throw new LinguisticException(LinguisticException.E_INVALIDARG);
            }
            int nullIndex = data.Length;
            for (int i = 0; i < data.Length; i += 2)
            {
                if (data[i] == 0 && data[i + 1] == 0)
                {
                    nullIndex = i;
                    break;
                }
            }
            string resultText = Encoding.Unicode.GetString(data, 0, nullIndex);
            return resultText;
        }

        /// <summary>
        /// Uses <see cref="Format(MappingDataRange)">Format</see> to format all the ranges of the supplied
        /// <see cref="MappingPropertyBag">MappingPropertyBag</see>.
        /// </summary>
        /// <param name="bag">The property bag to convert.</param>
        /// <returns>An array of strings, one per <see cref="MappingDataRange">MappingDataRange</see>.</returns>
        public string[] FormatAll(MappingPropertyBag bag)
        {
            MappingDataRange[] dataRanges = bag.GetResultRanges();
            string[] results = new string[dataRanges.Length];
            for (int i = 0; i < results.Length; ++i)
            {
                results[i] = Format(dataRanges[i]);
            }
            return results;
        }
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer IDS
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions