Skip to content

Does it support compiling Windows Forms applications? #219

Description

@ichat001

I used the following PowerShell code to compile a simple Windows Forms application. The compilation was successful, but when running the generated .exe file, the following error occurred.

Thank you in advance for any suggestions and help.

sdk ver:

C:\Users\Administrator>dotnet --list-sdks
8.0.203 [C:\Program Files\dotnet\sdk]

error info:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Forms.Application.EnableVisualStyles() + 0xb
   at Program.Main() + 0x9
   at Program!<BaseAddress>+0x192685

powershell code:

cls

cd "C:\Users\Administrator\Desktop\MyWinApp"

# == This script automatically detects the Microsoft.WindowsDesktop.App version and constructs the path for System.Drawing.Common.dll to correctly build the bflat command-line arguments ==
# == Define the base path for the Microsoft WindowsDesktop App shared runtime ==
$sharedRuntimeBasePath = "C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App"

# == Get the latest version folder in this directory ==
$versions = Get-ChildItem -Path $sharedRuntimeBasePath -Directory | Sort-Object Name -Descending
if ($versions.Count -eq 0) {
"No Microsoft.WindowsDesktop.App runtime directory found. Please confirm that .NET Desktop Runtime is installed."
return
}
$latestVersion = $versions[0].Name

# == Construct the full path for System.Drawing.Common.dll ==
$systemDrawingCommonPath = Join-Path -Path $sharedRuntimeBasePath -ChildPath "$latestVersion\System.Drawing.Common.dll"
if (-not (Test-Path $systemDrawingCommonPath)) {
"Runtime DLL not found: System.Drawing.Common.dll at path: $systemDrawingCommonPath"
return
}

# == Predefined array of reference DLL paths for compilation, add or modify as needed ==
$refBasePath = "C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\$latestVersion\ref\net8.0"
$netCoreRefBasePath = "C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\$latestVersion\ref\net8.0"

$refDlls = @(
"$refBasePath\System.Windows.Forms.dll",
"$refBasePath\System.Windows.Forms.Primitives.dll",
"$refBasePath\System.Drawing.dll",
"$netCoreRefBasePath\System.Drawing.Primitives.dll",
"$netCoreRefBasePath\System.ComponentModel.Primitives.dll",
"$netCoreRefBasePath\System.ComponentModel.TypeConverter.dll",
"$netCoreRefBasePath\System.Runtime.InteropServices.dll",
"$netCoreRefBasePath\System.ObjectModel.dll",
"$netCoreRefBasePath\System.Runtime.dll",
"$netCoreRefBasePath\System.Threading.dll"
)

# == Verify that all reference DLLs exist ==
foreach ($dll in $refDlls) {
if (-not (Test-Path $dll)) {
"Missing reference DLL: $dll. Please confirm the file exists."
return
}
}

# == Construct the bflat reference argument string ==
$bflatRefs = @()
foreach ($dll in $refDlls) {
$bflatRefs += "-r:`"$dll`""
}
$bflatRefs += "-r:`"$systemDrawingCommonPath`""

# == Build the final bflat command ==
$bflatCmd = "bflat build Program.cs " + ($bflatRefs -join " ") + " --os:windows --arch:x64"

# == Output the final command ==
$bflatCmd

# == run the final command ==
iex $bflatCmd

Program.cs File:

using System;
using System.Windows.Forms;

class Program
{
	[STAThread]
	static void Main()
	{
		Application.EnableVisualStyles();   
		Application.SetCompatibleTextRenderingDefault(false);

		Form form = new Form
		{
			Text  = "Hello bflat",
			Width = 300,
			Height= 200
		};

		Application.Run(form);                             
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions