This article provides spell checking functionality for Windows Store Apps, which is missing out-of-box for C#-VB/XAML, HTML/JavaScript apps. This was implemented in response to MSDN forum questions where developers are often looking for spell check solutions in C# or JavaScripts apps. Writing a wrapper over the spell checker factory is easy. However, it could be a little tricky for C# developers. Also, in some way, it completes this Code Sample Request. http://gallery.technet.microsoft.com/Spell-Checking-WinRT-cb59a4d5
It also shows how to ...
1. Use WinRT Component
2. Create WinRT Component Wrapper over Win32 libraries
3. Use C++/CX class as a wrapper to expose Win32 apis
↑ Return to Top
It implements the component wrapper over Win32 SpellChecker library. A wrapper that it can be easily used by caller of Windows Store App irrespective of Application Language and without worrying about C++ and COM intricacies.
Spell checking client sample (C++)
Also it needs to have spell checker provider installed. See below link for more details
Spell checking provider sample
The functionality to check spellings is exposed by ISpellCheckerFactory and ISpellChecker interface.
ISpellCheckerFactory : public IUnknown { public: virtual /* [propget] */ HRESULT STDMETHODCALLTYPE get_SupportedLanguages( /* [retval][out] */ __RPC__deref_out_opt IEnumString **value) = 0; virtual HRESULT STDMETHODCALLTYPE IsSupported( /* [in] */ __RPC__in LPCWSTR languageTag, /* [retval][out] */ __RPC__out BOOL *value) = 0; virtual HRESULT STDMETHODCALLTYPE CreateSpellChecker( /* [in] */ __RPC__in LPCWSTR languageTag, /* [retval][out] */ __RPC__deref_out_opt ISpellChecker **value) = 0; };
To start using the functionality, we initialize the COM infrastructure and create a instance of ISpellCheckerFactory as shown below inside Win RT Component.
HRESULT hr = CoCreateInstance(__uuidof(SpellCheckerFactory), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(spellCheckerFactory)); if (FAILED(hr)) { //"Unable to create Spell Checker Factory" }
ISpellChecker* spellChecker = nullptr; hr = spellCheckerFactory->CreateSpellChecker(languageTag, &spellChecker); if (SUCCEEDED(hr)) { // Spell checker interface created }
IEnumSpellingError* enumSpellingError = nullptr; HRESULT hr = ReadText(buffer, ARRAYSIZE(text), text); // See project for more details of this function if (SUCCEEDED(hr)) { hr = spellChecker->Check(text, &enumSpellingError); }
1. Copy SpellCheckerWrapperComponent project to your solution
2. Add SpellCheckerWrapperComponent as Project Reference to the Project you would like to use the Spell Checker functions.
3. Use SpellChecker class inside namespace SpellCheckerWinRTComponent present in test project SpellChecker or simplly call the functions inside the WinRT component.
To get all available supported languages.
SpellCheckerWrapperComponent.SpellChecker spellChecker = new SpellCheckerWrapperComponent.SpellChecker(); IList<string> lstAllAvailablleLangs = spellChecker.GetSupportedLanguages();
Dim spellChecker As SpellCheckerWrapperComponent.SpellChecker = New SpellCheckerWrapperComponent.SpellChecker() Dim lstAvailablleLangs As IList(Of String) = spellChecker.GetSupportedLanguages()
SpellCheckerWrapperComponent.SpellChecker spellChecker = new SpellCheckerWrapperComponent.SpellChecker(); SpellCheckerWrapperComponent.SpellingCheckResults spellingCheckResults = spellChecker.CheckWordSpelling(word, languageTag/*"en-US"*/);
Dim spellChecker As SpellCheckerWrapperComponent.SpellChecker = new SpellCheckerWrapperComponent.SpellChecker(); Dim language As String = "en-US" Dim word As String = "testin" Dim spellingCheckResults As SpellCheckerWrapperComponent.SpellingCheckResults = spellChecker.CheckWordSpelling(word, language) Dim isSpellingCorrect As String = spellingCheckResults.IsCorrect Dim lstSuggestions As IList(Of String) = spellingCheckResults.Suggestions Dim replacementIfAny As String = spellingCheckResults.ReplacementIfAny
http://gallery.technet.microsoft.com/Spell-Checking-WinRT-cb59a4d5
Ed Price - MSFT edited Revision 13. Comment: Tags, white space
Ed Price - MSFT edited Revision 11. Comment: Grammar
Ed Price - MSFT edited Revision 10. Comment: Grammar
Sachin S edited Revision 8. Comment: Remove redundant top links
Sachin S edited Revision 5. Comment: Reason behind this implementation
Sachin S edited Revision 4. Comment: Added image
Sachin S edited Revision 3. Comment: Finishing initial draft
Sachin S edited Revision 1. Comment: Fixing initial draft
Congratulations on winning the May TechNet Guru contest! blogs.technet.com/.../technet-guru-awards-may-2013.aspx
Thanks Ed!
I highlighted your article on MSDN blogs here: blogs.msdn.com/.../c-guru-spell-checking-winrt-component-c-vb-html5-javascript.aspx
That's nice. Thanks Ed!
Congratulations on being featured on the home page of TechNet Wiki: social.technet.microsoft.com/.../default.aspx
We featured your article on the Wiki Ninjas blog: blogs.technet.com/.../may-c-guru-spell-checking-winrt-component.aspx
Sachin, we'd like to interview you for the Wiki Ninjas blog! Please email me. It's edprice at Microsoft. Thanks!