/// <summary> /// Invokes GetAlternateList in the native dll /// </summary> /// <returns></returns> private GestureRecognitionResult[] InvokeGetAlternateList() { GestureRecognitionResult[] recResults = new GestureRecognitionResult[] { }; int hr = 0; MS.Win32.Recognizer.RECO_RANGE recoRange; recoRange.iwcBegin = 0; recoRange.cCount = 1; uint countOfAlternates = IRAS_DefaultCount; IntPtr[] pRecoAlternates = new IntPtr[IRAS_DefaultCount]; try { hr = MS.Win32.Recognizer.UnsafeNativeMethods.GetAlternateList(_hContext, ref recoRange, ref countOfAlternates, pRecoAlternates, MS.Win32.Recognizer.ALT_BREAKS.ALT_BREAKS_SAME); if (HRESULT.Succeeded(hr) && countOfAlternates != 0) { List <GestureRecognitionResult> resultList = new List <GestureRecognitionResult>(); for (int i = 0; i < countOfAlternates; i++) { uint size = 1; // length of string == 1 since gesture id is a single WCHAR StringBuilder recoString = new StringBuilder(1); RecognitionConfidence confidenceLevel; if (HRESULT.Failed(MS.Win32.Recognizer.UnsafeNativeMethods.GetString(pRecoAlternates[i], out recoRange, ref size, recoString)) || HRESULT.Failed(MS.Win32.Recognizer.UnsafeNativeMethods.GetConfidenceLevel(pRecoAlternates[i], out recoRange, out confidenceLevel))) { // Fail to retrieve the reco result, skip this one continue; } ApplicationGesture gesture = (ApplicationGesture)recoString[0]; Debug.Assert(ApplicationGestureHelper.IsDefined(gesture)); if (ApplicationGestureHelper.IsDefined(gesture)) { resultList.Add(new GestureRecognitionResult(confidenceLevel, gesture)); } } recResults = resultList.ToArray(); } } finally { // Destroy the alternates for (int i = 0; i < countOfAlternates; i++) { if (pRecoAlternates[i] != IntPtr.Zero) { #pragma warning suppress 6031, 56031 // Return value ignored on purpose. MS.Win32.Recognizer.UnsafeNativeMethods.DestroyAlternate(pRecoAlternates[i]); pRecoAlternates[i] = IntPtr.Zero; } } } return(recResults); }
/// <summary> /// Invokes GetLatticePtr in the native dll /// </summary> /// <returns></returns> private GestureRecognitionResult[] InvokeGetLatticePtr() { GestureRecognitionResult[] recResults = new GestureRecognitionResult[] { }; // int hr = 0; IntPtr ptr = IntPtr.Zero; // NOTICE-2005/07/11-WAYNEZEN, // There is no need to free the returned the structure. // The memory will be released when ResetContext, which is invoked in the callee - Recognize, is called. if (HRESULT.Succeeded( MS.Win32.Recognizer.UnsafeNativeMethods.GetLatticePtr( _hContext, ref ptr))) { unsafe { MS.Win32.Recognizer.RECO_LATTICE *pRecoLattice = (MS.Win32.Recognizer.RECO_LATTICE *)ptr; uint bestResultColumnCount = pRecoLattice->ulBestResultColumnCount; Debug.Assert(!(bestResultColumnCount != 0 && pRecoLattice->pLatticeColumns == IntPtr.Zero), "Invalid results!"); if (bestResultColumnCount > 0 && pRecoLattice->pLatticeColumns != IntPtr.Zero) { List <GestureRecognitionResult> resultList = new List <GestureRecognitionResult>(); MS.Win32.Recognizer.RECO_LATTICE_COLUMN *pLatticeColumns = (MS.Win32.Recognizer.RECO_LATTICE_COLUMN *)(pRecoLattice->pLatticeColumns); ulong *pulBestResultColumns = (ulong *)(pRecoLattice->pulBestResultColumns); for (uint i = 0; i < bestResultColumnCount; i++) { ulong column = pulBestResultColumns[i]; MS.Win32.Recognizer.RECO_LATTICE_COLUMN recoColumn = pLatticeColumns[column]; Debug.Assert(0 < recoColumn.cLatticeElements, "Invalid results!"); for (int j = 0; j < recoColumn.cLatticeElements; j++) { MS.Win32.Recognizer.RECO_LATTICE_ELEMENT recoElement = ((MS.Win32.Recognizer.RECO_LATTICE_ELEMENT *)(recoColumn.pLatticeElements))[j]; Debug.Assert((RECO_TYPE)(recoElement.type) == RECO_TYPE.RECO_TYPE_WCHAR, "The Application gesture has to be WCHAR type"); if ((RECO_TYPE)(recoElement.type) == RECO_TYPE.RECO_TYPE_WCHAR) { // Retrieve the confidence lever RecognitionConfidence confidenceLevel = RecognitionConfidence.Poor; MS.Win32.Recognizer.RECO_LATTICE_PROPERTIES recoProperties = recoElement.epProp; uint propertyCount = recoProperties.cProperties; MS.Win32.Recognizer.RECO_LATTICE_PROPERTY **apProps = (MS.Win32.Recognizer.RECO_LATTICE_PROPERTY * *)recoProperties.apProps; for (int k = 0; k < propertyCount; k++) { MS.Win32.Recognizer.RECO_LATTICE_PROPERTY *pProps = apProps[k]; if (pProps->guidProperty == GUID_CONFIDENCELEVEL) { Debug.Assert(pProps->cbPropertyValue == sizeof(uint) / sizeof(byte)); RecognitionConfidence level = (RecognitionConfidence)(((uint *)pProps->pPropertyValue))[0]; if (level >= RecognitionConfidence.Strong && level <= RecognitionConfidence.Poor) { confidenceLevel = level; } break; } } ApplicationGesture gesture = (ApplicationGesture)((char)(recoElement.pData)); Debug.Assert(ApplicationGestureHelper.IsDefined(gesture)); if (ApplicationGestureHelper.IsDefined(gesture)) { // Get the gesture result resultList.Add(new GestureRecognitionResult(confidenceLevel, gesture)); } } } } recResults = (GestureRecognitionResult[])(resultList.ToArray()); } } } return(recResults); }
internal static ApplicationGesture[] GetApplicationGestureArrayAndVerify(IEnumerable <ApplicationGesture> applicationGestures) { if (applicationGestures == null) { // Null is not allowed as the argument value throw new ArgumentNullException("applicationGestures"); } uint count = 0; //we need to make a disconnected copy ICollection <ApplicationGesture> collection = applicationGestures as ICollection <ApplicationGesture>; if (collection != null) { count = (uint)collection.Count; } else { foreach (ApplicationGesture gesture in applicationGestures) { count++; } } // Cannot be empty if (count == 0) { // An empty array is not allowed. throw new ArgumentException(SR.Get(SRID.ApplicationGestureArrayLengthIsZero), "applicationGestures"); } bool foundAllGestures = false; List <ApplicationGesture> gestures = new List <ApplicationGesture>(); foreach (ApplicationGesture gesture in applicationGestures) { if (!ApplicationGestureHelper.IsDefined(gesture)) { throw new ArgumentException(SR.Get(SRID.ApplicationGestureIsInvalid), "applicationGestures"); } //check for allgestures if (gesture == ApplicationGesture.AllGestures) { foundAllGestures = true; } //check for dupes if (gestures.Contains(gesture)) { throw new ArgumentException(SR.Get(SRID.DuplicateApplicationGestureFound), "applicationGestures"); } gestures.Add(gesture); } // AllGesture cannot be specified with other gestures if (foundAllGestures && gestures.Count != 1) { // no dupes allowed throw new ArgumentException(SR.Get(SRID.AllGesturesMustExistAlone), "applicationGestures"); } return(gestures.ToArray()); }