2014-11-21 180 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錯誤,你的代碼可能應該工作。