Ejemplo n.º 1
0
        /// <summary>
        /// Retrieves a list of available ELS platform-supported services, along with associated
        /// information, according to application-specified criteria.
        /// </summary>
        /// <param name="options">Optional. A <see cref="MappingEnumOptions">MappingEnumOptions</see> object containing criteria to use during
        /// enumeration of services. The application specifies null for this parameter to retrieve all
        /// installed services.</param>
        /// <returns>An array of <see cref="MappingService">MappingService</see> objects matching the criteria supplied in the options
        /// parameter.</returns>
        public static MappingService[] GetServices(MappingEnumOptions options)
        {
            // Throw PlatformNotSupportedException if not running on Win7 or greater
            ThrowIfNotWin7();

            IntPtr pService       = IntPtr.Zero;
            UInt32 dwServiceCount = 0;
            UInt32 hResult        = 0;
            IntPtr guidPtr        = IntPtr.Zero;

            try
            {
                if (options != null)
                {
                    Win32EnumOptions enumOptions = options._win32EnumOption;
                    Nullable <Guid>  pGuid       = options._guid;
                    if (pGuid != null)
                    {
                        Guid guid = (Guid)pGuid;
                        guidPtr = Marshal.AllocHGlobal(InteropTools.SizeOfGuid);
                        Marshal.StructureToPtr(guid, guidPtr, false);
                        enumOptions._pGuid = guidPtr;
                    }
                    hResult = Win32Methods.MappingGetServices(ref enumOptions, ref pService, ref dwServiceCount);
                }
                else
                {
                    hResult = Win32Methods.MappingGetServices(IntPtr.Zero, ref pService, ref dwServiceCount);
                }
                if (hResult != 0)
                {
                    throw new LinguisticException(hResult);
                }
                if ((pService == IntPtr.Zero) != (dwServiceCount == 0))
                {
                    throw new InvalidOperationException();
                }
                IntPtr[] services = new IntPtr[dwServiceCount];
                ServiceCache.Instance.RegisterServices(ref pService, services);
                MappingService[] result = new MappingService[dwServiceCount];
                for (int i = 0; i < dwServiceCount; ++i)
                {
                    result[i] = new MappingService(services[i]);
                }
                return(result);
            }
            finally
            {
                if (pService != IntPtr.Zero)
                {
                    // Ignore the result if an exception is being thrown.
                    Win32Methods.MappingFreeServices(pService);
                }
                if (guidPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(guidPtr);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Causes an ELS service to perform an action after text recognition has occurred. For example,
        /// a phone dialer service first must recognize phone numbers and then can perform the "action"
        /// of dialing a number.
        /// </summary>
        /// <param name="bag">A <see cref="MappingPropertyBag">MappingPropertyBag</see> object containing the results of a previous call to
        /// MappingService.MappingRecognizeText. This parameter cannot be set to null.</param>
        /// <param name="rangeIndex">A starting index inside the text recognition results for a recognized
        /// text range. This value should be between 0 and the range count.</param>
        /// <param name="actionId">The identifier of the action to perform.
        /// This parameter cannot be set to null.</param>
        public void DoAction(MappingPropertyBag bag, int rangeIndex, string actionId)
        {
            if (rangeIndex < 0)
            {
                throw new LinguisticException(LinguisticException.E_INVALIDARG);
            }
            UInt32 hResult = Win32Methods.MappingDoAction(ref bag._win32PropertyBag, (uint)rangeIndex, actionId);

            if (hResult != 0)
            {
                throw new LinguisticException(hResult);
            }
        }
Ejemplo n.º 3
0
        private bool DisposeInternal()
        {
            if (_win32PropertyBag._context == IntPtr.Zero)
            {
                return(true);
            }
            UInt32 hResult = Win32Methods.MappingFreePropertyBag(ref _win32PropertyBag);

            if (hResult != 0)
            {
                throw new LinguisticException(hResult);
            }
            return(true);
        }
Ejemplo n.º 4
0
 private void FreeAllServices()
 {
     // Don't use synchronization here.
     // This will only be called during finalization
     // and at that point synchronization doesn't matter.
     // Also, the lock object might have already been finalized.
     if (_servicePointers != null)
     {
         foreach (IntPtr servicePtr in _servicePointers)
         {
             // Ignore the result, we need to continue anyway.
             Win32Methods.MappingFreeServices(servicePtr);
         }
         _servicePointers = null;
         _guidToService   = null;
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Calls an ELS service to recognize text. For example, the Microsoft Language Detection service
        /// will attempt to recognize the language in which the input text is written.
        /// </summary>
        /// <param name="text">The text to recognize. The text must be UTF-16, but some services have additional
        /// requirements for the input format. This parameter cannot be set to null.</param>
        /// <param name="length">Length, in characters, of the text specified in text.</param>
        /// <param name="index">Index inside the specified text to be used by the service. This value should be
        /// between 0 and length-1. If the application wants to process the entire text, it should set this
        /// parameter to 0.</param>
        /// <param name="options">Optional. A <see cref="MappingOptions">MappingOptions</see> object containing options that affect the result and
        /// behavior of text recognition. The application does not have to specify values for all object members.
        /// This parameter can be set to null to use the default mapping options.</param>
        /// <returns>A <see cref="MappingPropertyBag">MappingPropertyBag</see> object in which the service has stored its results. The structure is filled
        /// with information produced by the service during text recognition.</returns>
        public MappingPropertyBag RecognizeText(string text, int length, int index, MappingOptions options)
        {
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }
            if (length > text.Length || length < 0)
            {
                throw new ArgumentOutOfRangeException("length");
            }
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            UInt32             hResult = LinguisticException.E_FAIL;
            MappingPropertyBag bag     = new MappingPropertyBag(options, text);

            try
            {
                hResult = Win32Methods.MappingRecognizeText(
                    _service, bag._text.AddrOfPinnedObject(), (uint)length, (uint)index,
                    bag._options, ref bag._win32PropertyBag);
                if (hResult != 0)
                {
                    throw new LinguisticException(hResult);
                }
                return(bag);
            }
            finally
            {
                if (hResult != 0) // S_OK
                {
                    bag.Dispose();
                }
            }
        }
Ejemplo n.º 6
0
        public MappingService(Guid serviceGuid)
        {
            // Throw PlatformNotSupportedException if not running on Win7 or greater
            ThrowIfNotWin7();

            IntPtr pService;
            UInt32 dwServiceCount = 0;
            UInt32 hResult        = 0;

            // First, check to see if we already have the service in the cache:
            pService = ServiceCache.Instance.GetCachedService(ref serviceGuid);
            if (pService != IntPtr.Zero)
            {
                _service = pService;
                InteropTools.Unpack(out _win32Service, _service);
            }
            else // pService is IntPtr.Zero in this case.
            {
                // If we don't, we must find it via MappingGetServices:
                IntPtr guidPtr = IntPtr.Zero;
                try
                {
                    guidPtr = Marshal.AllocHGlobal(InteropTools.SizeOfGuid);
                    Win32EnumOptions enumOptions = new Win32EnumOptions();
                    enumOptions._size = InteropTools.SizeOfWin32EnumOptions;
                    Marshal.StructureToPtr(serviceGuid, guidPtr, false);
                    enumOptions._pGuid = guidPtr;
                    hResult            = Win32Methods.MappingGetServices(ref enumOptions, ref pService, ref dwServiceCount);
                    if (hResult != 0)
                    {
                        throw new LinguisticException(hResult);
                    }
                    if (pService == IntPtr.Zero)
                    {
                        throw new InvalidOperationException();
                    }
                    if (dwServiceCount != 1)
                    {
                        hResult = Win32Methods.MappingFreeServices(pService);
                        if (hResult == 0)
                        {
                            throw new InvalidOperationException();
                        }
                        else
                        {
                            throw new LinguisticException(hResult);
                        }
                    }
                    IntPtr[] services = new IntPtr[1];
                    ServiceCache.Instance.RegisterServices(ref pService, services);
                    _service = services[0];
                    InteropTools.Unpack(out _win32Service, _service);
                }
                finally
                {
                    if (pService != IntPtr.Zero)
                    {
                        // Ignore the result if an exception is being thrown.
                        Win32Methods.MappingFreeServices(pService);
                    }
                    if (guidPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(guidPtr);
                    }
                }
            }
        }