Skip to content

Commit

Permalink
Fix BlazorHybrid for .NET 7
Browse files Browse the repository at this point in the history
  • Loading branch information
kant2002 committed Nov 9, 2024
1 parent 6b553b2 commit 4d91c8c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 19 deletions.
2 changes: 2 additions & 0 deletions WinFormsComInterop/WebView2/ICoreWebView2ControllerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ namespace WinFormsComInterop.WebView2;
[RuntimeCallableWrapper(typeof(webview2::Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller2))]
[RuntimeCallableWrapper(typeof(webview2::Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller3))]
[RuntimeCallableWrapper(typeof(webview2::Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller4))]
#if !NET7_0_OR_GREATER
[RuntimeCallableWrapper(typeof(webview2::Microsoft.Web.WebView2.Core.Raw.ICoreWebView2PrivatePartialController))]
#endif
internal partial class ICoreWebView2ControllerWrapper
{
public readonly IntPtr instance;
Expand Down
104 changes: 91 additions & 13 deletions WinFormsComInterop/WebView2/WebView2ComWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,79 +387,157 @@ static WebView2ComWrapper()

protected override object CreateObject(IntPtr externalComObject, CreateObjectFlags flags)
{
if (Marshal.QueryInterface(externalComObject, ref IID_ICoreWebView2Environment, out var coreWebView2EnvironmentPtr) >= 0)
if (Marshal.QueryInterface(externalComObject,
#if NET8_0_OR_GREATER
in
#else
ref
#endif
IID_ICoreWebView2Environment, out var coreWebView2EnvironmentPtr) >= 0)
{
Marshal.Release(coreWebView2EnvironmentPtr);
return new ICoreWebView2EnvironmentWrapper(externalComObject);
}

if (Marshal.QueryInterface(externalComObject, ref IID_ICoreWebView2Controller, out var coreWebView2ControllerPtr) >= 0)
if (Marshal.QueryInterface(externalComObject,
#if NET8_0_OR_GREATER
in
#else
ref
#endif
IID_ICoreWebView2Controller, out var coreWebView2ControllerPtr) >= 0)
{
Marshal.Release(coreWebView2ControllerPtr);
return new ICoreWebView2ControllerWrapper(externalComObject);
}

if (Marshal.QueryInterface(externalComObject, ref IID_ICoreWebView2, out var coreWebView2Ptr) >= 0)
if (Marshal.QueryInterface(externalComObject,
#if NET8_0_OR_GREATER
in
#else
ref
#endif
IID_ICoreWebView2, out var coreWebView2Ptr) >= 0)
{
Marshal.Release(coreWebView2Ptr);
return new ICoreWebView2Wrapper(externalComObject);
}

if (Marshal.QueryInterface(externalComObject, ref IID_ICoreWebView2Settings, out var coreWebView2SettingsPtr) >= 0)
if (Marshal.QueryInterface(externalComObject,
#if NET8_0_OR_GREATER
in
#else
ref
#endif
IID_ICoreWebView2Settings, out var coreWebView2SettingsPtr) >= 0)
{
Marshal.Release(coreWebView2SettingsPtr);
return new ICoreWebView2SettingsWrapper(externalComObject);
}

if (Marshal.QueryInterface(externalComObject, ref IID_ICoreWebView2NavigationStartingEventArgs, out var coreWebView2NavigationStartingEventArgsPtr) >= 0)
if (Marshal.QueryInterface(externalComObject,
#if NET8_0_OR_GREATER
in
#else
ref
#endif
IID_ICoreWebView2NavigationStartingEventArgs, out var coreWebView2NavigationStartingEventArgsPtr) >= 0)
{
Marshal.Release(coreWebView2NavigationStartingEventArgsPtr);
return new ICoreWebView2NavigationStartingEventArgsWrapper(externalComObject);
}

if (Marshal.QueryInterface(externalComObject, ref IID_ICoreWebView2NavigationCompletedEventArgs, out var coreWebView2NavigationCompletedEventArgsPtr) >= 0)
if (Marshal.QueryInterface(externalComObject,
#if NET8_0_OR_GREATER
in
#else
ref
#endif
IID_ICoreWebView2NavigationCompletedEventArgs, out var coreWebView2NavigationCompletedEventArgsPtr) >= 0)
{
Marshal.Release(coreWebView2NavigationCompletedEventArgsPtr);
return new ICoreWebView2NavigationCompletedEventArgsWrapper(externalComObject);
}

if (Marshal.QueryInterface(externalComObject, ref IID_ICoreWebView2WebResourceRequestedEventArgs, out var coreWebView2WebResourceRequestedEventArgsPtr) >= 0)
if (Marshal.QueryInterface(externalComObject,
#if NET8_0_OR_GREATER
in
#else
ref
#endif
IID_ICoreWebView2WebResourceRequestedEventArgs, out var coreWebView2WebResourceRequestedEventArgsPtr) >= 0)
{
Marshal.Release(coreWebView2WebResourceRequestedEventArgsPtr);
return new ICoreWebView2WebResourceRequestedEventArgsWrapper(externalComObject);
}

if (Marshal.QueryInterface(externalComObject, ref IID_ICoreWebView2WebResourceRequest, out var coreWebView2WebResourceRequestPtr) >= 0)
if (Marshal.QueryInterface(externalComObject,
#if NET8_0_OR_GREATER
in
#else
ref
#endif
IID_ICoreWebView2WebResourceRequest, out var coreWebView2WebResourceRequestPtr) >= 0)
{
Marshal.Release(coreWebView2WebResourceRequestPtr);
return new ICoreWebView2WebResourceRequestWrapper(externalComObject);
}

if (Marshal.QueryInterface(externalComObject, ref IID_ICoreWebView2WebResourceResponse, out var coreWebView2WebResourceResponsePtr) >= 0)
if (Marshal.QueryInterface(externalComObject,
#if NET8_0_OR_GREATER
in
#else
ref
#endif
IID_ICoreWebView2WebResourceResponse, out var coreWebView2WebResourceResponsePtr) >= 0)
{
Marshal.Release(coreWebView2WebResourceResponsePtr);
return new ICoreWebView2WebResourceResponseWrapper(externalComObject);
}

if (Marshal.QueryInterface(externalComObject, ref IID_ICoreWebView2SourceChangedEventArgs, out var coreWebView2SourceChangedEventArgsPtr) >= 0)
if (Marshal.QueryInterface(externalComObject,
#if NET8_0_OR_GREATER
in
#else
ref
#endif
IID_ICoreWebView2SourceChangedEventArgs, out var coreWebView2SourceChangedEventArgsPtr) >= 0)
{
Marshal.Release(coreWebView2SourceChangedEventArgsPtr);
return new ICoreWebView2SourceChangedEventArgsWrapper(externalComObject);
}

if (Marshal.QueryInterface(externalComObject, ref IID_ICoreWebView2ContentLoadingEventArgs, out var coreWebView2ContentLoadingEventArgsPtr) >= 0)
if (Marshal.QueryInterface(externalComObject,
#if NET8_0_OR_GREATER
in
#else
ref
#endif
IID_ICoreWebView2ContentLoadingEventArgs, out var coreWebView2ContentLoadingEventArgsPtr) >= 0)
{
Marshal.Release(coreWebView2ContentLoadingEventArgsPtr);
return new ICoreWebView2ContentLoadingEventArgsWrapper(externalComObject);
}

if (Marshal.QueryInterface(externalComObject, ref IID_ICoreWebView2AcceleratorKeyPressedEventArgs, out var coreWebView2AcceleratorKeyPressedEventArgsPtr) >= 0)
if (Marshal.QueryInterface(externalComObject,
#if NET8_0_OR_GREATER
in
#else
ref
#endif
IID_ICoreWebView2AcceleratorKeyPressedEventArgs, out var coreWebView2AcceleratorKeyPressedEventArgsPtr) >= 0)
{
Marshal.Release(coreWebView2AcceleratorKeyPressedEventArgsPtr);
return new ICoreWebView2AcceleratorKeyPressedEventArgsWrapper(externalComObject);
}

if (Marshal.QueryInterface(externalComObject, ref IID_ICoreWebView2WebMessageReceivedEventArgs, out var coreWebView2WebMessageReceivedEventArgsPtr) >= 0)
if (Marshal.QueryInterface(externalComObject,
#if NET8_0_OR_GREATER
in
#else
ref
#endif
IID_ICoreWebView2WebMessageReceivedEventArgs, out var coreWebView2WebMessageReceivedEventArgsPtr) >= 0)
{
Marshal.Release(coreWebView2WebMessageReceivedEventArgsPtr);
return new ICoreWebView2WebMessageReceivedEventArgsWrapper(externalComObject);
Expand Down
16 changes: 10 additions & 6 deletions samples/BlazorHybrid/BlazorHybrid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -12,7 +12,7 @@
<PublishTrimmed>true</PublishTrimmed>-->
<_SuppressWinFormsTrimError>true</_SuppressWinFormsTrimError>
<MetadataUpdaterSupport>false</MetadataUpdaterSupport>
</PropertyGroup>
</PropertyGroup>

<ItemGroup>
<!-- Required because of https://github.com/dotnet/runtime/issues/79930 -->
Expand All @@ -23,10 +23,14 @@
<TrimmerRootDescriptor Include="ILLink.Descriptors.xml" />
</ItemGroup>

<ItemGroup>
<ItemGroup Condition="$(TargetFramework) == 'net7.0-windows'">
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.WindowsForms" Version="7.0.52" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1462.37" />
<PackageReference Include="Microsoft.DotNet.ILCompiler" Version="8.0.0-*" Condition="'$(UseNet8)' == 'True'" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1462.37" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'net8.0-windows'">
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.WindowsForms" Version="8.0.93" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2592.51" />
</ItemGroup>

<ItemGroup>
Expand All @@ -35,7 +39,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\WinFormsComInterop\WinFormsComInterop.csproj" Condition="'$(UseNet8)' != 'True'" />
<ProjectReference Include="..\..\WinFormsComInterop\WinFormsComInterop.csproj" />
</ItemGroup>

</Project>

0 comments on commit 4d91c8c

Please sign in to comment.