Beispiel #1
0
        /// <summary>
        /// An array of <see cref="MappingDataRange">MappingDataRange</see> objects containing all recognized text range results. This member is populated
        /// by MappingService.RecognizeText or asynchronously with
        /// MappingService.BeginRecognizeText.
        /// </summary>
        public MappingDataRange[] GetResultRanges()
        {
            var result = new MappingDataRange[_win32PropertyBag._rangesCount];

            for (int i = 0; i < result.Length; ++i)
            {
                result[i] = new MappingDataRange
                {
                    _win32DataRange = Win32NativeInteropTools.Unpack <Win32DataRange>(
                        (IntPtr)((ulong)_win32PropertyBag._ranges + ((ulong)i * InteropTools.SizeOfWin32DataRange)))
                };
            }
            return(result);
        }
Beispiel #2
0
        internal MappingPropertyBag(MappingOptions options, string text)
        {
            if (!(_serviceCache = ServiceCache.Instance).RegisterResource())
            {
                throw new LinguisticException();
            }

            _win32PropertyBag._size = InteropTools.SizeOfWin32PropertyBag;

            if (options != null)
            {
                _options = Win32NativeInteropTools.Pack(ref options._win32Options);
            }

            _text = GCHandle.Alloc(text, GCHandleType.Pinned);
        }
        /// <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;
            uint   serviceCount = 0;
            uint   hresult;

            // 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 = Win32NativeInteropTools.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(Win32NativeInteropTools.SizeOfGuid);
                    var enumOptions = new Win32EnumOptions
                    {
                        _size = InteropTools.SizeOfWin32EnumOptions
                    };
                    Marshal.StructureToPtr(serviceIdentifier, guidPtr, false);
                    enumOptions._pGuid = guidPtr;
                    hresult            = ExtendedLinguisticServicesNativeMethods.MappingGetServices(ref enumOptions, ref servicePointer, ref serviceCount);

                    if (hresult != 0)
                    {
                        throw new LinguisticException(hresult);
                    }

                    if (servicePointer == IntPtr.Zero)
                    {
                        throw new InvalidOperationException();
                    }

                    if (serviceCount != 1)
                    {
                        if (ExtendedLinguisticServicesNativeMethods.MappingFreeServices(servicePointer) == 0)
                        {
                            throw new InvalidOperationException();
                        }

                        else
                        {
                            throw new LinguisticException(hresult);
                        }
                    }

                    var services = new IntPtr[1];
                    ServiceCache.Instance.RegisterServices(ref servicePointer, services);
                    _service      = services[0];
                    _win32Service = Win32NativeInteropTools.Unpack <Win32Service>(_service);
                }
                finally
                {
                    if (servicePointer != IntPtr.Zero)
                    {
                        // Ignore the result if an exception is being thrown.
                        ExtendedLinguisticServicesNativeMethods.MappingFreeServicesVoid(servicePointer);
                    }

                    if (guidPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(guidPtr);
                    }
                }
            }
        }
 private MappingService(IntPtr pService)
 {
     _service      = pService;
     _win32Service = Win32NativeInteropTools.Unpack <Win32Service>(_service);
 }