#include "stdafx.h" #include <atlbase.h> #include <atlstr.h> #include <Spellcheck.h> class CCoInitialize { public: CCoInitialize() { CoInitializeEx(NULL, COINIT_MULTITHREADED); } ~CCoInitialize() { CoUninitialize(); } };
LPCWSTR kActionStrings[] = { L"CORRECTIVE_ACTION_NONE", L"CORRECTIVE_ACTION_GET_SUGGESTIONS", L"CORRECTIVE_ACTION_REPLACE", L"CORRECTIVE_ACTION_DELETE" };
int _tmain(int argc, _TCHAR* argv[]) { CCoInitialize com_init; CComPtr spell_checker_factory; HRESULT hr = CoCreateInstance(__uuidof(SpellCheckerFactory), NULL, CLSCTX_INPROC_SERVER, __uuidof(spell_checker_factory), reinterpret_cast(&spell;_checker_factory)); if (FAILED(hr)) { return 1; }
LPCWSTR lang_tag = L"en-US"; BOOL suppored = FALSE; spell_checker_factory->IsSupported(lang_tag, &suppored;); if (!suppored) { return 1; }
CComPtr spell_checker; hr = spell_checker_factory->CreateSpellChecker(lang_tag, &spell;_checker); if (FAILED(hr)) { return 1; }
WCHAR my_text[] = L"Helloo world, I am am new heere, hvae fun"; wprintf(L"%s\n\n", my_text); CComPtr spell_errors; hr = spell_checker->Check(my_text, &spell;_errors); if (FAILED(hr)) { return 1; }
CComPtr spell_error; while (spell_errors->Next(&spell;_error) == S_OK) { ULONG index, length; if (SUCCEEDED(spell_error->get_StartIndex(&index;)) && SUCCEEDED(spell_error->get_Length(&length;))) { CStringW tmp_str(my_text + index, length); wprintf(L"%-10s ", tmp_str.GetString());
CORRECTIVE_ACTION action; if (SUCCEEDED(spell_error->get_CorrectiveAction(&action;))) { wprintf(L"%-40s ", kActionStrings[action]); }
if (action == CORRECTIVE_ACTION_DELETE) { wprintf(L"delete %s\n", tmp_str.GetString()); } else if (action == CORRECTIVE_ACTION_GET_SUGGESTIONS) { CComPtr spell_suggestions; hr = spell_checker->Suggest(tmp_str.GetString(), &spell;_suggestions); if (FAILED(hr)) { break;; }
WCHAR *suggestion_str; while (spell_suggestions->Next(1, &suggestion;_str, NULL) == S_OK) { wprintf(L"%s ", suggestion_str); CoTaskMemFree(suggestion_str); } wprintf(L"\n"); } else if (action == CORRECTIVE_ACTION_REPLACE) { WCHAR *replace_str; hr = spell_error->get_Replacement(&replace;_str); wprintf(L"%s\n", replace_str); CoTaskMemFree(replace_str); } }
spell_error.Release(); }
return 0; }
|