kant2002 / WinFormsComInterop

ComWrappers required to run NativeAOT and WinForms
MIT License
227 stars 32 forks source link

How to publish AOT? #43

Open Symbai opened 2 years ago

Symbai commented 2 years ago

If I publish the sample app, I still get a managed .NET dll file with an executable host file and all the .NET runtime dll files. But when I publish a console project with AOT I get a single unmanaged executable. This is what I've expected for Winforms as well.

I used this command dotnet publish -r win-x64 and then went to the publish folder.

image

kant2002 commented 2 years ago

Question. Are you have .NET 7 SDK installed? I have 7.0.100-rc.2.22452.3 (and previously version was working fine) and everything was working. Are you have either <PublishAot>true</PublishAot> or <PackageReference Include="Microsoft.DotNet.ILCompiler" Version="7.0.0-*" /> (not needed in .NET 7) in your project file?

Symbai commented 2 years ago

My SDK is 6.0.400 and I'm building the winforms sample project, <PublishAot>true</PublishAot> was missing, <PackageReference Include="Microsoft.DotNet.ILCompiler" Version="7.0.0-*" /> not.

I've now added <PublishAot>true</PublishAot> and published again but still no AOT.

kant2002 commented 2 years ago

I think this is your answer. https://github.com/dotnet/runtime/issues/61231#issuecomment-1235385283 or maybe slightly earlier for context. For now .NET 6 SDK a bit broken. You can either use specific version of ILCompiler or wait for NET 6 SDK to land fix, or install NET 7 preview

Symbai commented 2 years ago

How can I use a specific version of the ILCompiler?

kant2002 commented 2 years ago

Try

<PackageReference Include="Microsoft.DotNet.ILCompiler" Version="7.0.0-preview.6.22324.4" />
Symbai commented 2 years ago

With this I get: error : The ResolvedILCompilerPack ItemGroup is required for target ImportRuntimeIlcPackageTarget

kant2002 commented 1 year ago

NativeAOT does not support .NET 6, so I probably would limit my attempts here too. Let me know if you still need this.

Symbai commented 1 year ago

Yeah it works fine with .NET 7. But it is normal that the output files are extremely huge in release mode? And that there are still many other files in the output directory?

For a console project a get a rather small app with only one executable.

kant2002 commented 1 year ago

native dll which is in the output dir you most likely can ignore. They are coming from WPF and that's SDK issue. Probbaly nobody bother with this yet. Minimal size of output EXE is limited to the fact that WinForms is using reflection a lot, Control is very fat class. Not sure what can be done to this, but this is mostly related to WinForms code.

Symbai commented 1 year ago

Yeah I was talking about all of these:

clrjit.dll
D3DCompiler_47_cor3.dll
mscordbi.dll
vcruntime140_cor3.dll

and so on. There are 21 additional files with a total size of 21mb.

kant2002 commented 1 year ago

Not quite clear why do you have 21 additional files. If they are from your external libs, probably you need them. For me, tipical WinForms App + NativeAOT uses only

Other files, I never seen them. I assume we did not count PDB files, they are not required obviously.

Symbai commented 1 year ago

I dont count PDB file. It is a simple "hello world" Winforms app with no other external lib / dependency than WinFormsComInterop. Deleting all code reduces it to 17 additiona files.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>
    <PlatformTarget>x64</PlatformTarget>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <PublishAot>true</PublishAot>
    <_SuppressWinFormsTrimError>true</_SuppressWinFormsTrimError>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="WinFormsComInterop" Version="0.3.2" />
  </ItemGroup>

</Project>

image

Project file: WinFormsNativeAOTTest.zip

//edit: I just moved only the executable into a new folder and it runs. So maybe all of the additional files can be deleted. I however havent tried running the executable on a system without any SDK / runtime / VC++ redis installed.

kant2002 commented 1 year ago

What command do you use for publish? dotnet publish. Also I belive PlatformTarget is redundant.

kant2002 commented 1 year ago

Can you run just dotnet publish without additional switched, and open bin\Debug\net7.0-windows\win-x64\publish\ folder?

Symbai commented 1 year ago

Doesn't matter, I published through VS but using dotnet publish without any additional parameter generates the same output:

\bin\Debug\net7.0-windows\win-x64\publish: image

//edit: and yes I've cleaned the output folder before publishing.

.NET SDK:
  Version: 7.0.100
  Commit: E12B7AF219

Running time environment:
  OS name: Windows
  OS version: 10.0.22621
  OS Platform: Windows
  RID: Win10-X64
  Base Path: C: \ Program Files \ Dotnet \ SDK \ 7.0.100 \

Host:
   Version: 7.0.0
   Architecture: X64
   Commit: D099F075E4

.NET SDKS installed:
   6.0.100-preview.5.21302.13 [C: \ Program Files \ Dotnet \ SDK]
   6.0.112 [C: \ Program Files \ Dotnet \ SDK]
   7.0.100 [C: \ Program Files \ Dotnet \ SDK]
kant2002 commented 1 year ago

do you have <PublishAot>true</PublishAot> inside your csproj?

Symbai commented 1 year ago

I've attached the project file above, yes I have.

kant2002 commented 1 year ago
  1. I unpack application to C:\Users\User\Downloads\WinFormsNativeAOTTest image
  2. Run dotnet publish image
  3. Now I have following files image

If you have something else, you some issues in your setup. Maybe you have some Directory.Build.props above which annulls PublishAot or something else. Files which you are show indicates that you publish as regular self-contained app, not as NativeAOT application

NCLnclNCL commented 1 year ago

hey bro. i can build winform native oat but i click button and it notifi errors See the end of this message for details on invoking just-in-time (JIT) debugging instead of this dialog box.

** Exception Text ** System.NullReferenceException: Object reference not set to an instance of an object. at getcpu.Form1.checkactive() + 0x19d at System.Windows.Forms.Button.OnClick(EventArgs) + 0xcf at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs) + 0xb1 at System.Windows.Forms.Control.WmMouseUp(Message&, MouseButtons, Int32) + 0x268 at System.Windows.Forms.Control.WndProc(Message&) + 0x670 at System.Windows.Forms.ButtonBase.WndProc(Message&) + 0x161 at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Interop.User32.WM msg, IntPtr wparam, IntPtr lparam) + 0x70

** Loaded Assemblies ** System.Private.Xml Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Diagnostics.TraceSource Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Security.Principal.Windows Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Runtime.Numerics Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Drawing.Primitives Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Net.WebHeaderCollection Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Drawing.Common Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Design Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Text.Encoding.CodePages Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Runtime.InteropServices Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.IO.Compression.Brotli Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Diagnostics.FileVersionInfo Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Net.Primitives Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Windows.Forms.Primitives Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Net.Sockets Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Runtime.Serialization.Formatters Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Drawing.Design Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.ComponentModel Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Security.Cryptography Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Net.Security Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Collections.NonGeneric Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

WinFormsComInterop Assembly Version: 0.4.3.0 Win32 Version: n/a CodeBase:

System.Net.WebClient Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Collections.Specialized Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Private.Uri Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

Accessibility Assembly Version: 4.0.0.0 Win32 Version: n/a CodeBase:

System.Diagnostics.DiagnosticSource Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Reflection.Metadata Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

Microsoft.Win32.SystemEvents Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.ObjectModel Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Private.CoreLib Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Threading Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.ComponentModel.EventBasedAsync Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System Assembly Version: 4.0.0.0 Win32 Version: n/a CodeBase:

System.Collections Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.IO.Compression Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

Microsoft.Win32.Registry Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Collections.Concurrent Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Windows.Forms.Design Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Net.NetworkInformation Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Net.ServicePoint Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Net.Requests Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Security.Claims Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Security.Cryptography.Algorithms Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.ComponentModel.TypeConverter Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Linq Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

form Assembly Version: 1.0.0.0 Win32 Version: n/a CodeBase:

System.Threading.Channels Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Drawing Assembly Version: 4.0.0.0 Win32 Version: n/a CodeBase:

System.Windows.Forms Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.IO.MemoryMappedFiles Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Private.TypeLoader Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.ComponentModel.Primitives Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Net.Quic Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Collections.Immutable Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Management Assembly Version: 7.0.0.1 Win32 Version: n/a CodeBase:

System.Text.RegularExpressions Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Net.NameResolution Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Net.Http Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

System.Private.Reflection.Execution Assembly Version: 7.0.0.0 Win32 Version: n/a CodeBase:

** JIT Debugging **

kant2002 commented 1 year ago

@NCLnclNCL can you show what's in your Form1.checkactive function? Can you confirm that this is name of event handler for button click?

NCLnclNCL commented 1 year ago

@NCLnclNCL can you show what's in your Form1.checkactive function? Can you confirm that this is name of event handler for button click? hey bro, i know .net framework has .net native and it supports compiling for x86, why is it difficult to compile x86 for native aot at .net core