private bool DisposeInternal()
        {
            if (_win32PropertyBag._context == IntPtr.Zero)
            {
                return(true);
            }
            var hResult = Win32NativeMethods.MappingFreePropertyBag(ref _win32PropertyBag);

            if (hResult != 0)
            {
                throw new LinguisticException(hResult);
            }
            return(true);
        }
 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 (var servicePtr in _servicePointers)
         {
             Win32NativeMethods.MappingFreeServicesVoid(servicePtr);
         }
         _servicePointers = null;
         _guidToService   = null;
     }
 }
Пример #3
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 static void DoAction(MappingPropertyBag bag, int rangeIndex, string actionId)
        {
            if (bag == null)
            {
                throw new ArgumentNullException("bag");
            }

            if (rangeIndex < 0)
            {
                throw new LinguisticException(LinguisticException.InvalidArgs);
            }
            UInt32 hResult = Win32NativeMethods.MappingDoAction(ref bag._win32PropertyBag, (uint)rangeIndex, actionId);

            if (hResult != 0)
            {
                throw new LinguisticException(hResult);
            }
        }
Пример #4
0
        /// <summary>
        /// Constructs a new <see cref="MappingService">MappingService</see> object by instanciating an ELS service
        /// by its guid. For Windows 7, the only supported GUIDs are provided as
        /// readonly members of the <see cref="MappingAvailableServices">MappingAvailableServices</see> class.
        ///
        /// If the service
        /// with the specified guid doesn't exist, a <see cref="LinguisticException">LinguisticException</see> is thrown.
        /// </summary>
        /// <param name="serviceIdentifier">The guid of the service to instantiate.</param>
        public MappingService(Guid serviceIdentifier)
        {
            ThrowIfNotWin7();

            IntPtr servicePointer;
            UInt32 serviceCount = 0;
            UInt32 hresult      = 0;

            // First, check to see if we already have the service in the cache:
            servicePointer = ServiceCache.Instance.GetCachedService(ref serviceIdentifier);
            if (servicePointer != IntPtr.Zero)
            {
                _service      = servicePointer;
                _win32Service = InteropTools.Unpack <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(serviceIdentifier, guidPtr, false);
                    enumOptions._pGuid = guidPtr;
                    hresult            = Win32NativeMethods.MappingGetServices(ref enumOptions, ref servicePointer, ref serviceCount);
                    if (hresult != 0)
                    {
                        throw new LinguisticException(hresult);
                    }
                    if (servicePointer == IntPtr.Zero)
                    {
                        throw new InvalidOperationException();
                    }
                    if (serviceCount != 1)
                    {
                        hresult = Win32NativeMethods.MappingFreeServices(servicePointer);
                        if (hresult == 0)
                        {
                            throw new InvalidOperationException();
                        }
                        else
                        {
                            throw new LinguisticException(hresult);
                        }
                    }
                    IntPtr[] services = new IntPtr[1];
                    ServiceCache.Instance.RegisterServices(ref servicePointer, services);
                    _service      = services[0];
                    _win32Service = InteropTools.Unpack <Win32Service>(_service);
                }
                finally
                {
                    if (servicePointer != IntPtr.Zero)
                    {
                        // Ignore the result if an exception is being thrown.
                        Win32NativeMethods.MappingFreeServicesVoid(servicePointer);
                    }
                    if (guidPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(guidPtr);
                    }
                }
            }
        }