DllMain 1.0.2

dotnet add package DllMain --version 1.0.2
                    
NuGet\Install-Package DllMain -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DllMain" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DllMain" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="DllMain" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DllMain --version 1.0.2
                    
#r "nuget: DllMain, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DllMain@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DllMain&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=DllMain&version=1.0.2
                    
Install as a Cake Tool

DllMain

This appropriately named package allows you to safely define a DllMain function for your NativeAOT library in C.

Getting started

Add the DllMain NuGet package to your project, then follow the instructions from readme.txt.

How this works

The NativeAOT toolchain declares multiple MSBuild targets to build/link the NativeAOT library. Among other things, it injects an object file that defines an empty DllMain function, in an effort to avoid user-defined DllMain .NET implementations, since the NativeAOT runtime doesn't support initialization under DllMain's loader lock.

This package declares another target that gets injected between SetupOSSpecificProps (which builds LinkerArg) and LinkNative (which calls the linker with those arguments). This target:

  • Builds the user-defined .c file,
  • Removes the toolchain-defined object file with an empty DllMain from linker arguments,
  • Adds compiled user-defined C code to linker arguments.
  • Forces the linker to include the otherwise unreferenced new DllMain function in the final library.

As long as the user doesn't call managed code from their DllMain function, the resulting library should not break any rules imposed by the OS or the NativeAOT runtime/toolchain.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 167 6/24/2025
1.0.1 163 6/23/2025
1.0.0 166 6/23/2025