So I'm a fairly intermediate programmer in C#, and recently I have just been obsessed with file sizes and creating the smallest files available. For that I realized that using MSIL for simpler programs can reduce the size by alot.
So I tried converting the following
- Create a simple downloader. Managed to do it easily. Downloads my Files and saving them in temp and runs them. Managed to get it working and 1,5kb size, very useful!
now, after doing that I thought... why write the files when I could simply use reflection and run them withing having to use the disk?
That's where the problem came to be and I can't find the solution.
What is wrong with this code?
.assembly a{}
.subsystem 0x0002
.class private AA1.B
extends [mscorlib]System.Object{
.method private static void a(){
.entrypoint
.maxstack 4
.locals init (
[0] string str,
[1] uint8[] buffer)
nop
newobj instance void [System]System.Net.WebClient::.ctor()
ldstr "http://www.mywebwiste.com/myDotNetFile.exe"
call instance uint8[] [System]System.Net.WebClient::DownloadData(string)
stloc.1
ldloc.1
call class [mscorlib]System.Reflection.Assembly [mscorlib]System.Reflection.Assembly::Load(uint8[])
callvirt instance class [mscorlib]System.Reflection.MethodInfo [mscorlib]System.Reflection.Assembly::get_EntryPoint()
ldnull
ldc.i4.0
newarr object
callvirt instance object [mscorlib]System.Reflection.MethodBase::Invoke(object, object[])
}}
The Error I get compiling is "Reference to undeclared extern Assembly System." and "Reference to undeclared extern assembly mscorlib". Am I not declaring them?? It still compiles them but on run it just crashes.
Those are not errors, but warnings, the full message of the warning I get is:
To get rid of the warnings, you need to declare the assemblies:
But this is not your actual problem. If I run the compiled assembly, it indeed crashes. And using Visual Studio Just-In-Time Debugger, you'll find that the exception is:
This most likely means that your IL is wrong and that you should run PEVerify on it. If you do that, you get this error:
This means you need to add
ret
at the end of your method. If you fix that, you get:This means that you need to
pop
theobject
that's returned fromMethodBase::Invoke
.If you fix that, you don't get any more PEVerify errors and your code should probably work.