Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make AxHost work without classic COM interop. #10927

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using Windows.Win32.System.Com;

namespace Windows.Win32;
Expand Down Expand Up @@ -98,15 +99,29 @@ internal static bool SupportsInterface<T>(object? @object) where T : unmanaged,
}
else
{
// Fall back to COM interop if possible. Note that this will use the globally registered ComWrappers
// if that exists (so it won't always fall into legacy COM interop).
try
if (BuiltInComSupported)
{
ccw = (IUnknown*)Marshal.GetIUnknownForObject(@object);
// Fall back to COM interop if possible. Note that this will use the globally registered ComWrappers
// if that exists (so it won't always fall into legacy COM interop).
try
{
ccw = (IUnknown*)Marshal.GetIUnknownForObject(@object);
}
catch (Exception ex)
{
Debug.WriteLine($"Did not find IUnknown for {@object.GetType().Name}. {ex.Message}");
}
}
catch (Exception ex)
else
{
Debug.WriteLine($"Did not find IUnknown for {@object.GetType().Name}. {ex.Message}");
try
{
ccw = (IUnknown*)ComInterfaceMarshaller<object>.ConvertToUnmanaged(@object);
}
catch (Exception ex)
{
Debug.WriteLine($"Did not find IUnknown for {@object.GetType().Name}. {ex.Message}");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ConnectionPointCookie(object source, object sink, Type eventInterface)

internal ConnectionPointCookie(object? source, object sink, Type eventInterface, bool throwException)
{
if (source is not IConnectionPointContainer.Interface cpc)
if (!ComHelpers.SupportsInterface<IConnectionPointContainer>(source))
{
if (throwException)
{
Expand All @@ -45,8 +45,10 @@ internal ConnectionPointCookie(object? source, object sink, Type eventInterface,
IConnectionPoint* connectionPoint = null;
try
{
using var cpc = ComHelpers.GetComScope<IConnectionPointContainer>(source);

Guid riid = eventInterface.GUID;
HRESULT hr = cpc.FindConnectionPoint(&riid, &connectionPoint);
HRESULT hr = cpc.Value->FindConnectionPoint(&riid, &connectionPoint);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.ComponentModel.Design;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using Windows.Win32.System.Com;
using Windows.Win32.System.Ole;
using Windows.Win32.System.Variant;
Expand Down Expand Up @@ -129,7 +130,15 @@ protected override unsafe HRESULT Invoke(
object? ambient = _host.GetAmbientProperty(dispId);
if (ambient is not null)
{
Marshal.GetNativeVariantForObject(ambient, (nint)result);
if (ComHelpers.BuiltInComSupported)
{
Marshal.GetNativeVariantForObject(ambient, (nint)result);
}
else
{
*(ComVariant*)result = ComVariantMarshaller.ConvertToUnmanaged(ambient);
}

return HRESULT.S_OK;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using Microsoft.VisualStudio.Shell;
using Windows.Win32.System.Com;
using Windows.Win32.System.Com.StructuredStorage;
Expand Down Expand Up @@ -3464,7 +3465,15 @@ private void ReleaseAxControl()
{
if (_instance is not null)
{
Marshal.ReleaseComObject(_instance);
if (_instance is ComObject)
{
// TODO: how to release a ComWrappers ComObject?
}
else
{
Marshal.ReleaseComObject(_instance);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This throws if ComHelpers.BuiltInComSupported is false but I believe it was a no-op for ComObject anyways, at least used to be. I'm not clear if it is possible to do manual disposal of a ComObject based RCW so maybe this just needs to be a no-op for this case.

}

_instance = null;
DisposeHelper.NullAndDispose(ref _iOleInPlaceActiveObjectExternal);
}
Expand Down
Loading