Skip to content

Commit

Permalink
Add support for drop target in .NET 8
Browse files Browse the repository at this point in the history
  • Loading branch information
kant2002 committed Jun 15, 2024
1 parent 73aaefb commit 38dafef
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<UseNet8 Condition="'$(UseNet8)' == ''">False</UseNet8>
<UseNet8 Condition="'$(UseNet8)' == ''">True</UseNet8>
<GenerateWPF Condition="'$(GenerateWPF)' == ''">False</GenerateWPF>
<DefineConstants Condition="'$(GenerateWPF)' == 'True'">$(DefineConstants);USE_WPF</DefineConstants>
<VersionPrefix>0.4.3</VersionPrefix>
Expand Down
7 changes: 5 additions & 2 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
<DotnetInstallLocation>C:\Program Files\dotnet</DotnetInstallLocation>
</PropertyGroup>
<ItemGroup>
<Reference Update="Accessibility" Condition="$(TargetFramework) == 'net8.0'">
<HintPath>$(DotnetInstallLocation)\shared\Microsoft.WindowsDesktop.App\8.0.6\Accessibility.dll</HintPath>
</Reference>
<Reference Update="Accessibility" Condition="$(TargetFramework) == 'net7.0'">
<HintPath>$(DotnetInstallLocation)\shared\Microsoft.WindowsDesktop.App\7.0.10\Accessibility.dll</HintPath>
<HintPath>$(DotnetInstallLocation)\shared\Microsoft.WindowsDesktop.App\7.0.20\Accessibility.dll</HintPath>
</Reference>
<Reference Update="Accessibility" Condition="$(TargetFramework) == 'net6.0'">
<HintPath>$(DotnetInstallLocation)\shared\Microsoft.WindowsDesktop.App\6.0.21\Accessibility.dll</HintPath>
<HintPath>$(DotnetInstallLocation)\shared\Microsoft.WindowsDesktop.App\6.0.27\Accessibility.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions WinFormsComInterop/IDropTargetWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace WinFormsComInterop;

#if !NET8_0_OR_GREATER
[RuntimeCallableWrapper(typeof(primitives::Interop.Ole32.IDropTarget))]
#else
#endif
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
partial class IDropTargetWrapper
{
Expand Down
2 changes: 1 addition & 1 deletion WinFormsComInterop/WinFormsComInterop.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
Expand Down
40 changes: 37 additions & 3 deletions WinFormsComInterop/WinFormsComWrappers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ namespace WinFormsComInterop
[ComCallableWrapper(typeof(primitives::Interop.Ole32.IServiceProvider))]
[ComCallableWrapper(typeof(primitives::Interop.UiaCore.IRawElementProviderSimple))]
//[ComCallableWrapper(typeof(primitives::Interop.UiaCore.IAccessibleEx))]
#if !NET8_0_OR_GREATER
[ComCallableWrapper(typeof(primitives::Interop.Ole32.IDropTarget))]
[ComCallableWrapper(typeof(primitives::Interop.Mshtml.IDocHostUIHandler))]
[ComCallableWrapper(typeof(primitives::Interop.Ole32.IStorage))]
[ComCallableWrapper(typeof(primitives::Interop.Richedit.IRichEditOleCallback))]
#endif
[ComCallableWrapper(typeof(primitives::Interop.Ole32.IOleControlSite))]
[ComCallableWrapper(typeof(primitives::Interop.Ole32.IOleInPlaceSite))]
[ComCallableWrapper(typeof(primitives::Interop.Ole32.IOleContainer))]
[ComCallableWrapper(typeof(primitives::Interop.Ole32.IOleClientSite))]
[ComCallableWrapper(typeof(primitives::Interop.Ole32.IOleInPlaceFrame))]
[ComCallableWrapper(typeof(primitives::Interop.Mshtml.IDocHostUIHandler))]
[ComCallableWrapper(typeof(primitives::Interop.SHDocVw.DWebBrowserEvents2))]
[ComCallableWrapper(typeof(primitives::Interop.Ole32.ISimpleFrameSite))]
[ComCallableWrapper(typeof(primitives::Interop.Ole32.IPropertyNotifySink))]
Expand All @@ -43,9 +45,11 @@ public unsafe partial class WinFormsComWrappers : ComWrappers
{
static ComWrappers.ComInterfaceEntry* accessibleObjectEntry;
static ComWrappers.ComInterfaceEntry* primitivesStreamEntry;
#if !NET8_0_OR_GREATER
static ComWrappers.ComInterfaceEntry* primitivesDropTargetEntry;
static ComWrappers.ComInterfaceEntry* storageEntry;
#endif
static ComWrappers.ComInterfaceEntry* richEditOleCallbackEntry;
static ComWrappers.ComInterfaceEntry* primitivesDropTargetEntry;
static ComWrappers.ComInterfaceEntry* formsWebBrowserSiteEntry;
static ComWrappers.ComInterfaceEntry* formsWebBrowserContainerEntry;
static ComWrappers.ComInterfaceEntry* formsWebBrowserEventEntry;
Expand Down Expand Up @@ -107,8 +111,10 @@ static WinFormsComWrappers()
enumVariantEntry = CreateEnumVariantEntry();
#endif

#if !NET8_0_OR_GREATER
primitivesDropTargetEntry = CreatePrimitivesDropTargetEntry();
storageEntry = CreatePrimitivesIStorageEntry();
#endif
richEditOleCallbackEntry = CreatePrimitivesIRichEditOleCallbackEntry();
formsWebBrowserSiteEntry = CreateWebBrowserSiteEntry();
formsWebBrowserContainerEntry = CreateWebBrowserContainerEntry();
Expand Down Expand Up @@ -160,6 +166,7 @@ static WinFormsComWrappers()
}
#endif

#if !NET8_0_OR_GREATER
private static ComInterfaceEntry* CreatePrimitivesDropTargetEntry()
{
CreatePrimitivesIDropTargetProxyVtbl(out var vtbl);
Expand Down Expand Up @@ -192,6 +199,22 @@ static WinFormsComWrappers()
wrapperEntry->Vtable = vtbl;
return wrapperEntry;
}
#else

private static ComInterfaceEntry* CreatePrimitivesIRichEditOleCallbackEntry()
{
var vtblRaw = (System.IntPtr*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(global::WinFormsComInterop.WinFormsComWrappers), sizeof(System.IntPtr) * 13);
GetIUnknownImpl(out vtblRaw[0], out vtblRaw[1], out vtblRaw[2]);
primitives::Windows.Win32.UI.Controls.RichEdit.IRichEditOleCallback.PopulateVTable((primitives::Windows.Win32.UI.Controls.RichEdit.IRichEditOleCallback.Vtbl*)vtblRaw);
var vtbl = (System.IntPtr)vtblRaw;

var comInterfaceEntryMemory = RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(WinFormsComWrappers), sizeof(ComInterfaceEntry) * 1);
var wrapperEntry = (ComInterfaceEntry*)comInterfaceEntryMemory.ToPointer();
wrapperEntry->IID = IID_IRichEditOleCallback;
wrapperEntry->Vtable = vtbl;
return wrapperEntry;
}
#endif
private static ComInterfaceEntry* CreateAccessibleObjectEntry()
{
CreatePrimitivesIRawElementProviderSimpleProxyVtbl(out var rawElementProviderSimpleVtbl);
Expand All @@ -208,7 +231,9 @@ static WinFormsComWrappers()
private static ComInterfaceEntry* CreateWebBrowserSiteEntry()
{
CreatePrimitivesIOleControlSiteProxyVtbl(out var oleControlSiteVtbl);
#if !NET8_0_OR_GREATER
CreatePrimitivesIDocHostUIHandlerProxyVtbl(out var docHostUIHandlerVtbl);
#endif
CreatePrimitivesIOleInPlaceSiteProxyVtbl(out var oleInPlaceSiteVtbl);
CreatePrimitivesIOleClientSiteProxyVtbl(out var oleClientSiteVtbl);
CreatePrimitivesISimpleFrameSiteProxyVtbl(out var simpleFrameSiteVtbl);
Expand All @@ -218,8 +243,10 @@ static WinFormsComWrappers()
var wrapperEntry = (ComInterfaceEntry*)comInterfaceEntryMemory.ToPointer();
wrapperEntry[0].IID = IID_IOleControlSite;
wrapperEntry[0].Vtable = oleControlSiteVtbl;
#if !NET8_0_OR_GREATER
wrapperEntry[1].IID = IID_IDocHostUIHandler;
wrapperEntry[1].Vtable = docHostUIHandlerVtbl;
#endif
wrapperEntry[2].IID = IID_IOleInPlaceSite;
wrapperEntry[2].Vtable = oleInPlaceSiteVtbl;
wrapperEntry[3].IID = IID_IOleClientSite;
Expand Down Expand Up @@ -305,7 +332,7 @@ static WinFormsComWrappers()
return primitivesStreamEntry;
}
#endif

#if !NET8_0_OR_GREATER
if (obj is primitives::Interop.Ole32.IDropTarget)
{
count = 1;
Expand All @@ -323,6 +350,13 @@ static WinFormsComWrappers()
count = 1;
return richEditOleCallbackEntry;
}
#else
if (obj is primitives::Windows.Win32.UI.Controls.RichEdit.IRichEditOleCallback.Interface)
{
count = 1;
return richEditOleCallbackEntry;
}
#endif

#if USE_WPF
if (obj is winbase::MS.Win32.UnsafeNativeMethods.IOleDropTarget)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

#if !NET8_0_OR_GREATER
public partial class Interop
{
public partial class Mshtml
Expand Down Expand Up @@ -92,3 +93,4 @@ HRESULT FilterDataObject(
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Drawing;
using System.Runtime.InteropServices;

#if !NET8_0_OR_GREATER
public partial class Interop
{
public static partial class Ole32
Expand Down Expand Up @@ -42,3 +43,4 @@ HRESULT Drop(
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

#if !NET8_0_OR_GREATER
public partial class Interop
{
public static partial class Ole32
Expand Down Expand Up @@ -83,3 +84,4 @@ void SetElementTimes(
}
}
}
#endif
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.
// See the LICENSE file in the project root for more information.

#if !NET8_0_OR_GREATER
using System.Runtime.InteropServices;

public partial class Interop
Expand All @@ -16,3 +17,11 @@ public struct CHARRANGE
}
}
}
#else
internal struct CHARRANGE
{
internal int cpMin;

internal int cpMax;
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


#if !NET8_0_OR_GREATER
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
Expand Down Expand Up @@ -50,3 +52,37 @@ HRESULT GetDragDropEffect(
}
}
}
#else
// Windows.Win32.UI.Controls.RichEdit.IRichEditOleCallback
using System;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

namespace Windows.Win32.UI.Controls.RichEdit
{

[Guid("00020D03-0000-0000-C000-000000000046")]
[SupportedOSPlatform("windows6.0.6000")]
[GeneratedCode("Microsoft.Windows.CsWin32", "0.3.36-beta+6a5076f50a")]
public struct IRichEditOleCallback //: IVTable<IRichEditOleCallback, IRichEditOleCallback.Vtbl>, IVTable, IComIID
{
public struct Vtbl
{
}

[ComImport]
[Guid("00020D03-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[SupportedOSPlatform("windows6.0.6000")]
public interface Interface
{
}

public unsafe static void PopulateVTable(Vtbl* vtable)
{
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<FileVersion>6.0.0.0</FileVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
Expand Down
2 changes: 1 addition & 1 deletion facades/System.Windows.Forms/System.Windows.Forms.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<FileVersion>6.0.0.0</FileVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.100",
"version": "8.0.200",
"allowPrerelease": true,
"rollForward": "major"
}
Expand Down
2 changes: 1 addition & 1 deletion samples/HostedWindowsForms/HostedWindowsForms.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
2 changes: 1 addition & 1 deletion samples/SampleVisualBasic/SampleVisualBasic.vbproj
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>
<StartupObject>Sub Main</StartupObject>
<UseWindowsForms>true</UseWindowsForms>
<MyType>WindowsForms</MyType>
Expand Down
4 changes: 2 additions & 2 deletions samples/SampleWindowsForms/SampleWindowsForms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<UseWindowsForms Condition="'$(WinFormsRepoRoot)' == ''">True</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<_SuppressWinFormsTrimError>true</_SuppressWinFormsTrimError>
Expand Down Expand Up @@ -75,7 +75,7 @@
</Target>

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

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion samples/TrimmingSample/TrimmingSample.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 Down

0 comments on commit 38dafef

Please sign in to comment.