kant2002 / WinFormsComInterop

ComWrappers required to run NativeAOT and WinForms
MIT License
213 stars 30 forks source link

Generated Wrapper calls Marshal.GetNativeVariantForObject which is not supported #82

Open zznty opened 2 months ago

zznty commented 2 months ago

I was working with WebView2 COM wrappers and encountered an issue where calling CoreWebView2.AddHostObjectToScript which produces the following output

System.NotSupportedException: Built-in COM has been disabled via a feature switch. See https://aka.ms/dotnet-illink/com for more information.
   at System.Runtime.InteropServices.Marshal.GetNativeVariantForObject(Object obj, IntPtr pDstNativeVariant)
   at WinFormsComInterop.WebView2.ICoreWebView2Wrapper.webview2::Microsoft.Web.WebView2.Core.Raw.ICoreWebView2.AddHostObjectToScript(String name, Object& object)
   at Microsoft.Web.WebView2.Core.CoreWebView2.AddHostObjectToScript(String name, Object rawObject)

I assume COM wrappers source generator unintentionally emits a call into this method instead of using ComWrappers-specific code.

kant2002 commented 2 months ago

I’m not sure that A) you are using correct ComWrappers instance and B) I have support only for Net 7. Have to finish interface implementation for Net 8 which is tiresome

can you tell me what TFM are you targeting? And how do you enable ComWrappers?

zznty commented 2 months ago

Looks like I was targeting net 8 as it has somewhat working built-in ComWrappers source generator. Probably would have to wait for you to add support of net 8.

zznty commented 2 months ago

An update for this issue. I had to write a source generator for IDispatch COM interface implementation and to get around that exception from Marshal.GetNativeVariantForObject, call ICoreWebView2 directly.