private void Initialize(IRecognizerInternal recognizer, ISpRecoResult recoResult, byte[] sapiResultBlob, int maxAlternates)
        {
            _recognizer    = recognizer;
            _maxAlternates = maxAlternates;
            try
            {
                _sapiRecoResult = (recoResult as ISpRecoResult2);
            }
            catch (COMException)
            {
                _sapiRecoResult = null;
            }
            GCHandle gCHandle = GCHandle.Alloc(sapiResultBlob, GCHandleType.Pinned);

            try
            {
                IntPtr intPtr = gCHandle.AddrOfPinnedObject();
                int    num    = Marshal.ReadInt32(intPtr, 4);
                if (num == Marshal.SizeOf(typeof(SPRESULTHEADER_Sapi51)))
                {
                    SPRESULTHEADER_Sapi51 source = (SPRESULTHEADER_Sapi51)Marshal.PtrToStructure(intPtr, typeof(SPRESULTHEADER_Sapi51));
                    _header         = new SPRESULTHEADER(source);
                    _isSapi53Header = false;
                }
                else
                {
                    _header         = (SPRESULTHEADER)Marshal.PtrToStructure(intPtr, typeof(SPRESULTHEADER));
                    _isSapi53Header = true;
                }
                _header.Validate();
                IntPtr             phraseBuffer = new IntPtr((long)intPtr + (int)_header.ulPhraseOffset);
                SPSERIALIZEDPHRASE phraseHeader = RecognizedPhrase.GetPhraseHeader(phraseBuffer, _header.ulPhraseDataSize, _isSapi53Header);
                bool hasIPAPronunciation        = (_header.fAlphabet & 1) != 0;
                InitializeFromSerializedBuffer(this, phraseHeader, phraseBuffer, (int)_header.ulPhraseDataSize, _isSapi53Header, hasIPAPronunciation);
                if (recoResult != null)
                {
                    ExtractDictationAlternates(recoResult, maxAlternates);
                    recoResult.Discard(255u);
                }
            }
            finally
            {
                gCHandle.Free();
            }
            _sapiAudioBlob = new byte[_header.ulRetainedDataSize];
            Array.Copy(sapiResultBlob, (int)_header.ulRetainedOffset, _sapiAudioBlob, 0, (int)_header.ulRetainedDataSize);
            _sapiAlternatesBlob = new byte[_header.ulPhraseAltDataSize];
            Array.Copy(sapiResultBlob, (int)_header.ulPhraseAltOffset, _sapiAlternatesBlob, 0, (int)_header.ulPhraseAltDataSize);
        }
 private void ExtractDictationAlternates(ISpRecoResult recoResult, int maxAlternates)
 {
     if (recoResult != null && base.Grammar is DictationGrammar)
     {
         _alternates = new Collection <RecognizedPhrase>();
         IntPtr[] array = new IntPtr[maxAlternates];
         try
         {
             recoResult.GetAlternates(0, -1, maxAlternates, array, out maxAlternates);
         }
         catch (COMException)
         {
             maxAlternates = 0;
         }
         for (uint num = 0u; num < maxAlternates; num++)
         {
             ISpPhraseAlt spPhraseAlt = (ISpPhraseAlt)Marshal.GetObjectForIUnknown(array[num]);
             try
             {
                 IntPtr ppCoMemPhrase;
                 spPhraseAlt.GetSerializedPhrase(out ppCoMemPhrase);
                 try
                 {
                     RecognizedPhrase   recognizedPhrase = new RecognizedPhrase();
                     SPSERIALIZEDPHRASE phraseHeader     = RecognizedPhrase.GetPhraseHeader(ppCoMemPhrase, uint.MaxValue, _isSapi53Header);
                     bool hasIPAPronunciation            = (_header.fAlphabet & 1) != 0;
                     recognizedPhrase.InitializeFromSerializedBuffer(this, phraseHeader, ppCoMemPhrase, (int)phraseHeader.ulSerializedSize, _isSapi53Header, hasIPAPronunciation);
                     _alternates.Add(recognizedPhrase);
                 }
                 finally
                 {
                     Marshal.FreeCoTaskMem(ppCoMemPhrase);
                 }
             }
             finally
             {
                 Marshal.Release(array[num]);
             }
         }
     }
 }
 private Collection <RecognizedPhrase> GetAlternates()
 {
     if (_alternates == null)
     {
         _alternates = ExtractAlternates((int)_header.ulNumPhraseAlts, _isSapi53Header);
         if (_alternates.Count == 0 && _maxAlternates > 0)
         {
             RecognizedPhrase recognizedPhrase = new RecognizedPhrase();
             GCHandle         gCHandle         = GCHandle.Alloc(_phraseBuffer, GCHandleType.Pinned);
             try
             {
                 recognizedPhrase.InitializeFromSerializedBuffer(this, _serializedPhrase, gCHandle.AddrOfPinnedObject(), _phraseBuffer.Length, _isSapi53Header, _hasIPAPronunciation);
             }
             finally
             {
                 gCHandle.Free();
             }
             _alternates.Add(recognizedPhrase);
         }
     }
     return(_alternates);
 }
        public IXPathNavigable ConstructSmlFromSemantics()
        {
            if (!string.IsNullOrEmpty(_smlContent))
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(_smlContent);
                return(xmlDocument);
            }
            if (_serializedPhrase.SemanticErrorInfoOffset != 0)
            {
                ThrowInvalidSemanticInterpretationError();
            }
            XmlDocument      xmlDocument2     = new XmlDocument();
            XmlElement       xmlElement       = xmlDocument2.CreateElement("SML");
            NumberFormatInfo numberFormatInfo = new NumberFormatInfo();

            numberFormatInfo.NumberDecimalDigits = 3;
            xmlDocument2.AppendChild(xmlElement);
            xmlElement.SetAttribute("text", Text);
            xmlElement.SetAttribute("utteranceConfidence", Confidence.ToString("f", numberFormatInfo));
            xmlElement.SetAttribute("confidence", Confidence.ToString("f", numberFormatInfo));
            if (Semantics.Count > 0)
            {
                AppendPropertiesSML(xmlDocument2, xmlElement, Semantics, numberFormatInfo);
            }
            else if (Semantics.Value != null)
            {
                XmlText newChild = xmlDocument2.CreateTextNode(Semantics.Value.ToString());
                xmlElement.AppendChild(newChild);
            }
            for (int i = 0; i < _recoResult.Alternates.Count; i++)
            {
                RecognizedPhrase recognizedPhrase = _recoResult.Alternates[i];
                recognizedPhrase.AppendSml(xmlDocument2, i + 1, numberFormatInfo);
            }
            _smlContent = xmlDocument2.OuterXml;
            return(xmlDocument2);
        }
        private Collection <RecognizedPhrase> ExtractAlternates(int numberOfAlternates, bool isSapi53Header)
        {
            Collection <RecognizedPhrase> collection = new Collection <RecognizedPhrase>();

            if (numberOfAlternates > 0)
            {
                GCHandle gCHandle = GCHandle.Alloc(_sapiAlternatesBlob, GCHandleType.Pinned);
                try
                {
                    IntPtr value = gCHandle.AddrOfPinnedObject();
                    int    num   = Marshal.SizeOf(typeof(SPSERIALIZEDPHRASEALT));
                    int    num2  = 0;
                    for (int i = 0; i < numberOfAlternates; i++)
                    {
                        IntPtr ptr = new IntPtr((long)value + num2);
                        SPSERIALIZEDPHRASEALT sPSERIALIZEDPHRASEALT = (SPSERIALIZEDPHRASEALT)Marshal.PtrToStructure(ptr, typeof(SPSERIALIZEDPHRASEALT));
                        num2 += num;
                        num2  = ((!isSapi53Header) ? (num2 + (int)sPSERIALIZEDPHRASEALT.cbAltExtra) : (num2 + (int)((sPSERIALIZEDPHRASEALT.cbAltExtra + 7) & -8)));
                        IntPtr             phraseBuffer      = new IntPtr((long)value + num2);
                        SPSERIALIZEDPHRASE phraseHeader      = RecognizedPhrase.GetPhraseHeader(phraseBuffer, (uint)((int)_header.ulPhraseAltDataSize - num2), _isSapi53Header);
                        int ulSerializedSize                 = (int)phraseHeader.ulSerializedSize;
                        RecognizedPhrase recognizedPhrase    = new RecognizedPhrase();
                        bool             hasIPAPronunciation = (_header.fAlphabet & 2) != 0;
                        recognizedPhrase.InitializeFromSerializedBuffer(this, phraseHeader, phraseBuffer, ulSerializedSize, isSapi53Header, hasIPAPronunciation);
                        num2 = ((!isSapi53Header) ? (num2 + ulSerializedSize) : (num2 + ((ulSerializedSize + 7) & -8)));
                        collection.Add(recognizedPhrase);
                    }
                    return(collection);
                }
                finally
                {
                    gCHandle.Free();
                }
            }
            return(collection);
        }