Skip to content

Instantly share code, notes, and snippets.

@kant2002
Last active March 25, 2023 18:39
Show Gist options
  • Save kant2002/8d3d18de68557d8d9ce6fc6e6fe70094 to your computer and use it in GitHub Desktop.
Save kant2002/8d3d18de68557d8d9ce6fc6e6fe70094 to your computer and use it in GitHub Desktop.
WinForms and NativeAOT
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.InteropServices;
internal static partial class Interop
{
internal static partial class Ole32
{
[DllImport(Libraries.Oleaut32, PreserveSig = false)]
private unsafe static extern void OleCreatePictureIndirect(PICTDESC* pictdesc, ref Guid refiid, BOOL fOwn, [MarshalAs(UnmanagedType.Interface)]out object pResult);
/// <param name="fOwn">
/// <see cref="BOOL.TRUE"/> if the picture object is to destroy its picture when the object is destroyed.
/// (The picture handle in the <paramref name="pictdesc"/>.)
/// </param>
public unsafe static object OleCreatePictureIndirect(ref PICTDESC pictdesc, ref Guid refiid, BOOL fOwn)
{
pictdesc.cbSizeofstruct = (uint)sizeof(PICTDESC);
fixed (PICTDESC* p = &pictdesc)
{
object result;
OleCreatePictureIndirect(p, ref refiid, fOwn, out result);
return result;
}
}
public unsafe static object OleCreatePictureIndirect(ref Guid refiid)
{
object result;
OleCreatePictureIndirect(null, ref refiid, BOOL.TRUE, out result);
return result;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="dotnet-experimental" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-experimental/nuget/v3/index.json" />
</packageSources>
</configuration>
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
// This line is what doing magic.
ComWrappers.RegisterForMarshalling(WinFormsComInterop.WinFormsComWrappers.Instance);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
<Project Sdk="Microsoft.NET.Sdk">
....
<ItemGroup>
<PackageReference Include="Microsoft.DotNet.ILCompiler" Version="6.0.0-*" />
<PackageReference Include="WinFormsComInterop" Version="0.1.0" />
</ItemGroup>
<ItemGroup>
<Reference Include="c:\winforms\artifacts\bin\System.Windows.Forms\Release\net6.0\System.Windows.Forms.dll" />
<Reference Include="c:\winforms\artifacts\bin\System.Windows.Forms\Release\net6.0\System.Windows.Forms.Primitives.dll" />
</ItemGroup>
....
</Project>
<Project Sdk="Microsoft.NET.Sdk">
....
<ItemGroup>
<PackageReference Include="Microsoft.DotNet.ILCompiler" Version="6.0.0-*" />
<PackageReference Include="WinFormsComInterop" Version="0.1.0" />
</ItemGroup>
....
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment