Skip to content

Commit

Permalink
Add bunch of comwrappers for Text services
Browse files Browse the repository at this point in the history
  • Loading branch information
kant2002 committed Jul 5, 2022
1 parent acd7bd3 commit e1c803e
Show file tree
Hide file tree
Showing 21 changed files with 1,071 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ internal UnsafeNativeMethods.ITfDocumentMgr TransitoryDocumentManager
compartment.GetValue(out obj);
doc = obj as UnsafeNativeMethods.ITfDocumentMgr;

Marshal.ReleaseComObject(compartment);
((IDisposable)compartment).Dispose();
return doc;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public override void Complete()
Marshal.ReleaseComObject(composition);
}

Marshal.ReleaseComObject(context);
}
((IDisposable)context).Dispose();
}

//------------------------------------------------------
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ internal void AdviseNotifySink(UnsafeNativeMethods.ITfCompartmentEventSink sink)
Guid guid = UnsafeNativeMethods.IID_ITfCompartmentEventSink;

source.AdviseSink(ref guid, sink, out _cookie);
Marshal.ReleaseComObject(compartment);
((IDisposable)compartment).Dispose();
Marshal.ReleaseComObject(source);
}

Expand All @@ -123,7 +123,7 @@ internal void UnadviseNotifySink()
source.UnadviseSink(_cookie);
_cookie = UnsafeNativeMethods.TF_INVALID_COOKIE;

Marshal.ReleaseComObject(compartment);
((IDisposable)compartment).Dispose();
Marshal.ReleaseComObject(source);
}

Expand Down Expand Up @@ -204,7 +204,7 @@ internal object Value

compartment.GetValue(out obj);

Marshal.ReleaseComObject(compartment);
((IDisposable)compartment).Dispose();

return obj;
}
Expand All @@ -216,7 +216,7 @@ internal object Value
return;

compartment.SetValue(0 /* clientid */, ref value);
Marshal.ReleaseComObject(compartment);
((IDisposable)compartment).Dispose();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ internal void Uninitialize(bool appDomainShutdown)
if (_defaultTextStore.DocumentManager != null)
{
_defaultTextStore.DocumentManager.Pop(UnsafeNativeMethods.PopFlags.TF_POPF_ALL);
Marshal.ReleaseComObject(_defaultTextStore.DocumentManager);
_defaultTextStore.DocumentManager = null;
}

Expand Down Expand Up @@ -226,7 +225,7 @@ internal void RegisterTextStore(DefaultTextStore defaultTextStore)
doc.Push(context);

// Release any native resources we're done with.
Marshal.ReleaseComObject(context);
((IDisposable)context).Dispose();

// Same DocumentManager and EditCookie in _defaultTextStore.
_defaultTextStore.DocumentManager = doc;
Expand Down Expand Up @@ -375,14 +374,10 @@ private void StartTransitoryExtension()
// Advise TransitoryExtension Sink and store the cookie.
guid = UnsafeNativeMethods.IID_ITfTransitoryExtensionSink;
source = _defaultTextStore.DocumentManager as UnsafeNativeMethods.ITfSource;
if (source != null)
{
// DocumentManager only supports ITfSource on Longhorn, XP does not support it
source.AdviseSink(ref guid, _defaultTextStore, out transitoryExtensionSinkCookie);
_defaultTextStore.TransitoryExtensionSinkCookie = transitoryExtensionSinkCookie;
}
source.AdviseSink(ref guid, _defaultTextStore, out transitoryExtensionSinkCookie);
_defaultTextStore.TransitoryExtensionSinkCookie = transitoryExtensionSinkCookie;

Marshal.ReleaseComObject(comp);
((IDisposable)comp).Dispose();
}

// Stop TransitoryExtesion
Expand Down Expand Up @@ -417,7 +412,7 @@ private void StopTransitoryExtension()
{
var = (int)0;
comp.SetValue(0, ref var);
Marshal.ReleaseComObject(comp);
((IDisposable)comp).Dispose();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ internal static void CompleteCurrentComposition(UnsafeNativeMethods.ITfDocumentM
Marshal.ReleaseComObject(composition);
}

Marshal.ReleaseComObject(context);
((IDisposable)context).Dispose();
}

internal static UnsafeNativeMethods.ITfCompositionView GetCurrentCompositionView(UnsafeNativeMethods.ITfDocumentMgr documentMgr)
Expand All @@ -179,7 +179,7 @@ internal static UnsafeNativeMethods.ITfCompositionView GetCurrentCompositionView

UnsafeNativeMethods.ITfCompositionView view = GetComposition(context);

Marshal.ReleaseComObject(context);
((IDisposable)context).Dispose();

return view;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,10 @@ private void EnumerateSuggestions()
// Convert the VARIANT to string, and add it to our list.
// There's some special magic here. The VARIANT is VT_UI2/ByRef.
// But under the hood it's really a raw WCHAR *.
suggestions.Add(Marshal.PtrToStringUni(variant.data1.Value));
suggestions.Add(Marshal.PtrToStringUni(variant.data1));
}

variant.Clear();
}
finally
{
Expand Down Expand Up @@ -1039,10 +1041,7 @@ private static int EnumVariantNext(UnsafeNativeMethods.IEnumVariant variantEnume

unsafe
{
fixed (void* pVariant = &variant.vt)
{
result = variantEnumerator.Next(1, (IntPtr)pVariant, fetched);
}
result = variantEnumerator.Next(1, (IntPtr)(short*)&variant.vt, fetched);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ private object OnUnregisterTextStore(object arg)
source.UnadviseSink(textstore.EditSinkCookie);
textstore.EditSinkCookie = UnsafeNativeMethods.TF_INVALID_COOKIE;
}
Marshal.ReleaseComObject(context);

((IDisposable)context).Dispose();
}

textstore.DocumentManager.Pop(UnsafeNativeMethods.PopFlags.TF_POPF_ALL);
Expand Down Expand Up @@ -362,7 +363,7 @@ private void _RegisterTextStore(TextStore textstore)
}

// Release any native resources we're done with.
Marshal.ReleaseComObject(context);
((IDisposable)context).Dispose();

textstore.DocumentManager = doc;
textstore.ThreadFocusCookie = threadFocusCookie;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1883,7 +1883,7 @@ private bool GetFnReconv(ITextPointer textStart, ITextPointer textEnd, out Unsaf
}

Marshal.ReleaseComObject(range);
Marshal.ReleaseComObject(context);
((IDisposable)context).Dispose();

return fReconvertable;
}
Expand Down Expand Up @@ -2813,22 +2813,18 @@ private void PrepareAttributes(InputScope inputScope, double fontSize, FontFamil
attrval.overlappedId = (int)_supportingattributes[i].Style;
attrval.val = new NativeMethods.VARIANT();

// This VARIANT is returned to the caller, which supposed to call VariantClear().
// GC does not have to clear it.
attrval.val.SuppressFinalize();

switch (_supportingattributes[i].Style)
{
case AttributeStyle.InputScope:
object obj = new InputScopeAttribute(inputScope);
attrval.val.vt = (short)NativeMethods.tagVT.VT_UNKNOWN;
attrval.val.data1.Value = Marshal.GetIUnknownForObject(obj);
attrval.val.data1 = Marshal.GetIUnknownForObject(obj);
break;

case AttributeStyle.Font_Style_Height:
// We always evaluate the font size and returns a value.
attrval.val.vt = (short)NativeMethods.tagVT.VT_I4;
attrval.val.data1.Value = (IntPtr)(int)fontSize;
attrval.val.data1 = (IntPtr)(int)fontSize;
break;

case AttributeStyle.Font_FaceName:
Expand All @@ -2837,24 +2833,24 @@ private void PrepareAttributes(InputScope inputScope, double fontSize, FontFamil
if (familyName != null)
{
attrval.val.vt = (short)NativeMethods.tagVT.VT_BSTR;
attrval.val.data1.Value = Marshal.StringToBSTR(familyName);
attrval.val.data1 = Marshal.StringToBSTR(familyName);
}
}
break;

case AttributeStyle.Font_SizePts:
attrval.val.vt = (short)NativeMethods.tagVT.VT_I4;
attrval.val.data1.Value = (IntPtr)(int)(fontSize / 96.0 * 72.0);
attrval.val.data1 = (IntPtr)(int)(fontSize / 96.0 * 72.0);
break;

case AttributeStyle.Text_ReadOnly:
attrval.val.vt = (short)NativeMethods.tagVT.VT_BOOL;
attrval.val.data1.Value = IsReadOnly ? (IntPtr)1 : (IntPtr)0;
attrval.val.data1 = IsReadOnly ? (IntPtr)1 : (IntPtr)0;
break;

case AttributeStyle.Text_Orientation:
attrval.val.vt = (short)NativeMethods.tagVT.VT_I4;
attrval.val.data1.Value = (IntPtr)0;
attrval.val.data1 = (IntPtr)0;

// Get the transformation that is relative from source.
PresentationSource source = null;
Expand Down Expand Up @@ -2889,7 +2885,7 @@ private void PrepareAttributes(InputScope inputScope, double fontSize, FontFamil
else
angle = 360 - angleCos;

attrval.val.data1.Value = (IntPtr)((int)angle * 10);
attrval.val.data1 = (IntPtr)((int)angle * 10);
}
}
}
Expand All @@ -2901,7 +2897,7 @@ private void PrepareAttributes(InputScope inputScope, double fontSize, FontFamil
// the vertical writing is not supported yet
//
attrval.val.vt = (short)NativeMethods.tagVT.VT_BOOL;
attrval.val.data1.Value = (IntPtr)0;
attrval.val.data1 = (IntPtr)0;
break;
}

Expand Down
43 changes: 21 additions & 22 deletions src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/NativeMethodsCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4361,7 +4361,7 @@ public sealed class CA_STRUCT
}
#endif
[StructLayout(LayoutKind.Sequential)]
public sealed class VARIANT {
public struct VARIANT {
[MarshalAs(UnmanagedType.I2)]
public short vt;
[MarshalAs(UnmanagedType.I2)]
Expand All @@ -4371,9 +4371,9 @@ public sealed class VARIANT {
[MarshalAs(UnmanagedType.I2)]
public short reserved3;

public SecurityCriticalDataForSet<IntPtr> data1;
public IntPtr data1;

public SecurityCriticalDataForSet<IntPtr> data2;
public IntPtr data2;


public bool Byref{
Expand All @@ -4383,21 +4383,21 @@ public bool Byref{
}

public void Clear() {
if ((this.vt == (int)tagVT.VT_UNKNOWN || this.vt == (int)tagVT.VT_DISPATCH) && this.data1.Value != IntPtr.Zero) {
Marshal.Release(this.data1.Value);
if ((this.vt == (int)tagVT.VT_UNKNOWN || this.vt == (int)tagVT.VT_DISPATCH) && this.data1 != IntPtr.Zero) {
Marshal.Release(this.data1);
}

if (this.vt == (int)tagVT.VT_BSTR && this.data1.Value != IntPtr.Zero) {
SysFreeString(this.data1.Value);
if (this.vt == (int)tagVT.VT_BSTR && this.data1 != IntPtr.Zero) {
SysFreeString(this.data1);
}

this.data1.Value = this.data2.Value = IntPtr.Zero;
this.data1 = this.data2 = IntPtr.Zero;
this.vt = (int)tagVT.VT_EMPTY;
}

~VARIANT() {
Clear();
}
//~VARIANT() {
// Clear();
//}

public void SuppressFinalize()
{
Expand All @@ -4407,7 +4407,7 @@ public void SuppressFinalize()
GC.SuppressFinalize(this);
}

#if never
#if !UIAUTOMATIONTYPES
public static VARIANT FromObject(Object var) {
VARIANT v = new VARIANT();

Expand Down Expand Up @@ -4496,15 +4496,14 @@ public static VARIANT FromObject(Object var) {
return v;
}
#endif

[DllImport(ExternDll.Oleaut32,CharSet=CharSet.Auto)]
private static extern IntPtr SysAllocString([In, MarshalAs(UnmanagedType.LPWStr)]string s);

[DllImport(ExternDll.Oleaut32,CharSet=CharSet.Auto)]
private static extern void SysFreeString(IntPtr pbstr);
public void SetLong(long lVal) {
data1.Value = (IntPtr)(lVal & 0xFFFFFFFF);
data2.Value = (IntPtr)((lVal >> 32) & 0xFFFFFFFF);
data1 = (IntPtr)(lVal & 0xFFFFFFFF);
data2 = (IntPtr)((lVal >> 32) & 0xFFFFFFFF);
}

public IntPtr ToCoTaskMemPtr() {
Expand All @@ -4513,13 +4512,13 @@ public IntPtr ToCoTaskMemPtr() {
Marshal.WriteInt16(mem, 2, reserved1);
Marshal.WriteInt16(mem, 4, reserved2);
Marshal.WriteInt16(mem, 6, reserved3);
Marshal.WriteInt32(mem, 8, (int) data1.Value);
Marshal.WriteInt32(mem, 12, (int) data2.Value);
Marshal.WriteInt32(mem, 8, (int) data1);
Marshal.WriteInt32(mem, 12, (int) data2);
return mem;
}

public object ToObject() {
IntPtr val = data1.Value;
IntPtr val = data1;
long longVal;

int vtType = (int)(this.vt & (short)tagVT.VT_TYPEMASK);
Expand Down Expand Up @@ -4575,7 +4574,7 @@ public object ToObject() {
longVal = Marshal.ReadInt64(val);
}
else {
longVal = ((uint)data1.Value & 0xffffffff) | ((uint)data2.Value << 32);
longVal = ((uint)data1 & 0xffffffff) | ((uint)data2 << 32);
}

if (vt == (int)tagVT.VT_I8) {
Expand All @@ -4599,7 +4598,7 @@ public object ToObject() {

case (int)tagVT.VT_CY:
// internally currency is 8-byte int scaled by 10,000
longVal = ((uint)data1.Value & 0xffffffff) | ((uint)data2.Value << 32);
longVal = ((uint)data1 & 0xffffffff) | ((uint)data2 << 32);
return new Decimal(longVal);
case (int)tagVT.VT_DATE:
throw new FormatException(/*SR.GetString(SR.CannotConvertDoubleToDate)*/);
Expand All @@ -4621,7 +4620,7 @@ public object ToObject() {
return val;

case (int)tagVT.VT_DECIMAL:
longVal = ((uint)data1.Value & 0xffffffff) | ((uint)data2.Value << 32);
longVal = ((uint)data1 & 0xffffffff) | ((uint)data2 << 32);
return new Decimal(longVal);

case (int)tagVT.VT_BOOL:
Expand All @@ -4636,7 +4635,7 @@ public object ToObject() {
return guid;

case (int)tagVT.VT_FILETIME:
longVal = ((uint)data1.Value & 0xffffffff) | ((uint)data2.Value << 32);
longVal = ((uint)data1 & 0xffffffff) | ((uint)data2 << 32);
return new DateTime(longVal);

case (int)tagVT.VT_ARRAY:
Expand Down
Loading

0 comments on commit e1c803e

Please sign in to comment.