-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shout out to @mikebattista who wrote this. I just refined it a little.
- Loading branch information
Showing
9 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
test/Microsoft.Windows.CsWin32.Tests/Microsoft.Windows.CsWin32.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CoCreateInstance | ||
CoTaskMemFree | ||
SpellCheckerFactory | ||
ISpellCheckerFactory | ||
ISpellChecker | ||
CLSCTX | ||
S_FALSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.Windows.Sdk | ||
{ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal partial class PInvoke | ||
{ | ||
/// <inheritdoc cref="CoCreateInstance(System.Guid*, IUnknown*, uint, System.Guid*, void**)"/> | ||
/// <seealso href="https://github.com/microsoft/CsWin32/issues/103" /> | ||
internal static unsafe HRESULT CoCreateInstance<T>(in System.Guid rclsid, IUnknown* pUnkOuter, uint dwClsContext, in System.Guid riid, out T* ppv) | ||
where T : unmanaged | ||
{ | ||
HRESULT hr = CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid, out void* o); | ||
ppv = (T*)o; | ||
return hr; | ||
} | ||
|
||
/// <inheritdoc cref="Marshal.ThrowExceptionForHR(int, IntPtr)" /> | ||
/// <returns>The value from <paramref name="errorCode"/> if it does not reflect an error.</returns> | ||
/// <seealso cref="Marshal.ThrowExceptionForHR(int, IntPtr)"/> | ||
/// <seealso href="https://github.com/microsoft/CsWin32/issues/119" /> | ||
internal static HRESULT ThrowOnFailure(this HRESULT errorCode, IntPtr errorInfo = default) | ||
{ | ||
Marshal.ThrowExceptionForHR(errorCode, errorInfo); | ||
return errorCode; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Windows.Sdk; | ||
using static Microsoft.Windows.Sdk.Constants; | ||
using static Microsoft.Windows.Sdk.PInvoke; | ||
|
||
unsafe | ||
{ | ||
CoCreateInstance( | ||
typeof(SpellCheckerFactory).GUID, | ||
null, | ||
(uint)CLSCTX.CLSCTX_INPROC_SERVER, // https://github.com/microsoft/win32metadata/issues/185 | ||
typeof(ISpellCheckerFactory).GUID, | ||
out ISpellCheckerFactory* spellCheckerFactory).ThrowOnFailure(); | ||
|
||
spellCheckerFactory->IsSupported( | ||
"en-US", | ||
out bool supported).ThrowOnFailure(); | ||
|
||
if (!supported) | ||
{ | ||
return; | ||
} | ||
|
||
spellCheckerFactory->CreateSpellChecker( | ||
"en-US", | ||
out ISpellChecker* spellChecker).ThrowOnFailure(); | ||
|
||
var text = @"""Cann I I haev some?"""; | ||
|
||
Console.WriteLine(@"Check {0}", text); | ||
|
||
spellChecker->Check( | ||
text, | ||
out IEnumSpellingError* errors).ThrowOnFailure(); | ||
|
||
while (true) | ||
{ | ||
if (errors->Next(out ISpellingError* error).ThrowOnFailure() == S_FALSE) | ||
{ | ||
break; | ||
} | ||
|
||
error->get_StartIndex(out uint startIndex).ThrowOnFailure(); | ||
error->get_Length(out uint length).ThrowOnFailure(); | ||
|
||
var word = text.Substring((int)startIndex, (int)length); | ||
|
||
error->get_CorrectiveAction(out CORRECTIVE_ACTION action).ThrowOnFailure(); | ||
|
||
switch (action) | ||
{ | ||
case CORRECTIVE_ACTION.CORRECTIVE_ACTION_DELETE: | ||
Console.WriteLine(@"Delete ""{0}""", word); | ||
break; | ||
case CORRECTIVE_ACTION.CORRECTIVE_ACTION_REPLACE: | ||
// KNOWN ISSUE: ushort will be changed to string (https://github.com/microsoft/CsWin32/issues/121) | ||
error->get_Replacement(out ushort* replacement).ThrowOnFailure(); | ||
Console.WriteLine(@"Replace ""{0}"" with ""{1}""", word, new string((char*)replacement)); | ||
CoTaskMemFree(replacement); | ||
break; | ||
case CORRECTIVE_ACTION.CORRECTIVE_ACTION_GET_SUGGESTIONS: | ||
var l = new List<string>(); | ||
spellChecker->Suggest(word, out IEnumString* suggestions).ThrowOnFailure(); | ||
while (true) | ||
{ | ||
// KNOWN ISSUE: ushort will be changed to string (https://github.com/microsoft/CsWin32/issues/121) | ||
ushort* suggestion; | ||
if (suggestions->Next(1, &suggestion, null).ThrowOnFailure() != 0) | ||
{ | ||
break; | ||
} | ||
|
||
l.Add(new string((char*)suggestion)); | ||
CoTaskMemFree(suggestion); | ||
} | ||
|
||
suggestions->Release(); | ||
Console.WriteLine(@"Suggest replacing ""{0}"" with:", word); | ||
foreach (var s in l) | ||
{ | ||
Console.WriteLine("\t{0}", s); | ||
} | ||
|
||
break; | ||
default: | ||
break; | ||
} | ||
|
||
error->Release(); | ||
} | ||
|
||
errors->Release(); | ||
spellChecker->Release(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="..\..\src\Microsoft.Windows.CsWin32\build\Microsoft.Windows.CsWin32.props" /> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Microsoft.Windows.CsWin32\Microsoft.Windows.CsWin32.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> | ||
<Analyzer Include="$(RepoRootPath)\bin\Microsoft.Windows.CsWin32\$(Configuration)\netstandard2.0\System.Text.Json.dll" /> | ||
<Analyzer Include="$(RepoRootPath)\bin\Microsoft.Windows.CsWin32\$(Configuration)\netstandard2.0\Microsoft.Bcl.AsyncInterfaces.dll" /> | ||
<Analyzer Include="$(RepoRootPath)\bin\Microsoft.Windows.CsWin32\$(Configuration)\netstandard2.0\System.Text.Encodings.Web.dll" /> | ||
<Analyzer Include="$(RepoRootPath)\bin\Microsoft.Windows.CsWin32\$(Configuration)\netstandard2.0\YamlDotNet.dll" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Windows.SDK.Win32Metadata" Version="$(MetadataVersion)" GeneratePathProperty="true" PrivateAssets="none" /> | ||
<PackageReference Include="System.Memory" Version="4.5.4" /> | ||
</ItemGroup> | ||
|
||
</Project> |