2014-11-21 182 views
0

所以我是C#中的一名相当中级的程序员,最近我刚刚痴迷于文件大小和创建可用的最小文件。为此,我意识到使用MSIL进行更简单的程序可以减少很多尺寸。IL - 我做错了什么?

所以,我试图将以下

  • 创建一个简单的下载。易于管理。下载我的文件并将它们保存在temp中并运行它们。管理得到它的工作和1,5kb大小,非常有用!

现在,在这样做后,我想......为什么写文件时,我可以简单地使用反射和运行他们必须使用磁盘?

这就是问题的走过来的,我无法找到解决方案。

这段代码有什么问题?

.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[]) 
}} 

我得到编译的错误是“引用未声明的extern程序集系统”。和“引用未声明的extern程序集mscorlib”。我是不是在宣布他们?它仍然编译它们,但运行它只是崩溃。

+3

出于兴趣,* *为什么你迷恋文件的大小?你实际上是在一个非常有限的环境中运作,还是仅仅为了兴趣? – 2014-11-21 10:51:52

+2

“这就是问题出现的地方,我无法找到解决方案” - 什么问题?您向我们展示了一些代码,但没有说明问题是什么。你应该描述你实际面临的问题。 (目前还不完全清楚问题在于您是如何使用反射,还是使用IL。) – 2014-11-21 10:52:29

+0

我无法在工作场所使用USB记忆棒,因此无法完成此项工作。 – 2014-11-21 11:13:15

回答

3

The Error I get compiling is "Reference to undeclared extern Assembly System." and "Reference to undeclared extern assembly mscorlib".

那些没有错误,但警告,警告我得到的全部信息是:

warning : Reference to undeclared extern assembly 'mscorlib'. Attempting autodetect

为了摆脱的警告,你需要声明的组件:

.assembly extern mscorlib 
{ 
    .ver 4:0:0:0 
    .publickeytoken = (B7 7A 5C 56 19 34 E0 89) 
} 
.assembly extern System 
{ 
    .ver 4:0:0:0 
    .publickeytoken = (B7 7A 5C 56 19 34 E0 89) 
} 

但这不是你的实际问题。如果我运行编译后的程序集,它确实崩溃了。并使用Visual Studio刚刚在实时调试器,你会发现例外是:

System.InvalidProgramException: Common Language Runtime detected an invalid program.

这很可能意味着你的信息素养是错误的,你应该在其上运行PEVerify。如果你这样做,你得到这个错误:

Error: [AA1.B::a][offset 0x00000023] fall through end of the method without returning

这意味着你需要在你的方法的末尾添加ret。如果解决这个问题,您可以:

Error: [AA1.B::a][offset 0x00000028] Stack must be empty on return from a void function.

这意味着你需要pop这是一个从MethodBase::Invoke返回object

如果解决这个问题,你没有得到任何更多PEVerify错误,你的代码可能应该工作。