PersistedAssemblyBuilder to generate executable
Hello! I'm trying to generate IL executable from C# via PersistedAssemblyBuilder.
I'm running into issues where the generated IL throws an exception:
I'm not sure if I did something wrong as I followed the docs https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-reflection-emit-persistedassemblybuilder or even if I tried to copy and paste the code directly (
SetEntryPoint()
example with output path modification, shown in screenshot), it still gives me error when I run the exe. I definitely have .NET 9 installed (as I need .NET 9 to actually run this exe generator in the first place).
Can someone help me figure out what went wrong? And how can I fix it so that it runs fine?9 Replies
If you try clicking the exe in explorer to run it it might give more details in the error message if it's really a dll load issue
the issue is that if you output an IL image as a .exe windows sees it launches it with .NET Framework, you need to output as a .dll and use dotnet to run it
hmm... I'm fine with having to generate another .dll file, but in this case how to build the .exe alongside to use dotnet to run it? Also how to generate that dll? Is it just using the earlier example on that MS Learn page that generates dll instead of that example?
you need to output as a .dll and use dotnet to run itAlso, I'm not 100% what you mean by this, are you suggesting that if I output a dll, there is a way to run it with a
dotnet
command or some sort? If so, can I have the documentation for that so I can use that to test the persisted assembly first, although in the end I'd like an exe that a user can double clicks.
Alright I think I got the first version working. Thank you for this!Here's what I have now. I had to read into the .NET source code in github to determine a bit of how they generate dll.
(The program is basically equivalent to this)
However, I'd really love to have
Output.exe
instead of Output.bat
. Does somebody have some insights of how can I make that?
hmm... yeah this probably sounds way too complicated for what I'd like 😅 I think I'll go with .bat for now. Maybe one day I can make an exe. (i'm designing this to be portable and be able to be used with people without full development setup a bit so...)
I think it will work but probably not something that I wantThere is no such thing as .exe's generated or used by the Core runtime anymore. When you build a project in MSBuild, and you get an exe, what that actually is is a copy of apphost.exe, which is prebuilt and distributed as a binary within the SDK itself.
It is simply copied into your output, and the .runtimeconfig.json file is plopped next to it.
So you can get and do that yourself.
apphost.exe itself is just a small launcher that fires up the CLR, and it itself is written in C.
There's another associated process for single file executables, and something much more complicated for NAOT, etc.
In this case, can I copy the .exe from any other non-AOT or self contained project that I have, rename it to
<outputName>.exe
, then it will work as expected?
i'm trying to create a compiler though and the function takes the name outputName
to make $"{outputName}.dll"
and $"{outputName}.exe"
so like I can't just create a new project every time unfortunately (user can provide like literally any name)Part of building a compiler is building the tooling around it.
Generally in .NET world that means a MSBuild SDK.