Visual Studio for Application 內幕之二
當然,我們不會在每次都Compile,run,這樣並不是我們的本意,利用IVsaSite介面的GetCompiledState,我們可以在編譯後的pe檔案和調試資訊檔裝入
Public Sub GetCompiledState(ByRef pe() As Byte, ByRef debugInfo() As Byte) Implements Microsoft.Vsa.IVsaSite.GetCompiledState
End Sub
這個過程的實現有兩個參數,第一是pe檔案的二進位表示,其二是debug資訊,說白了第一個檔案是dll的二進位值,第二個檔案是pdb檔案的二進位值
如何得到編譯後的pe檔案和調試資訊檔
這個過程主要使用VsaEngine的SaveComiledState方法來實現,在正確Compile後
If m_VsaEngine.Compile() Then
m_VsaEngine.Run()
End If
Dim pe() As Byte
Dim pdb() As Byte
m_VsaEngine.SaveCompiledState(pe, pdb)
接下去,就是寫二進位檔案的問題了
Dim fs As New FileStream("c:\test.dll", FileMode.Create)
Dim bs As New BinaryWriter(fs)
bs.Write(pe)
fs.Close()
bs.Close()
fs = New FileStream("c:\test.pdb", FileMode.Create)
bs = New BinaryWriter(fs)
bs.Write(pdb)
fs.Close()
bs.Close()
接下來,我們切換到實現IVsaSite的類,在這個例子中是MyVsaSite,我們實現GetCompiledState方法
Public Sub GetCompiledState(ByRef pe() As Byte, ByRef debugInfo() As Byte) Implements Microsoft.Vsa.IVsaSite.GetCompiledState
Dim fs As FileStream = New FileStream("c:\test.dll", FileMode.Open)
Dim bs As BinaryReader = New BinaryReader(fs)
pe = bs.ReadBytes(fs.Length)
fs.Close()
bs.Close()
fs = New FileStream("c:\test.pdb", FileMode.Open)
bs = New BinaryReader(fs)
debugInfo = bs.ReadBytes(fs.Length)
End Sub
當調用VsaLoader的Run方法時,VsaLoader會調用其Site對象的GetCompiledState方法,擷取資料
這是調用的例子
m_VsaEngine = New Microsoft.Vsa.VsaLoader
m_VsaEngine.RootNamespace = "test"
m_VsaEngine.RootMoniker = "test://project1"
m_VsaEngine.Site = New MyVsaSite
m_VsaEngine.Run()
Dim args() As Object = New Object() {"jjx"}
Invoke("TestClass.Hello", args)