Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Imports System.Reflection
- Imports System.CodeDom
- Imports System.CodeDom.Compiler
- Imports Microsoft.VisualBasic
- 'MainForm.inputTXT.Text = textbox1.text
- 'MainForm.outputTXT.Text = textbox2.text
- 'MainForm.titleTXT.Text = as title
- Module coderMod
- Public Sub CoderX()
- ' read code from textbox
- Dim Code As String = MainForm.inputTXT.Text
- ' clear output textbox
- MainForm.outputTXT.Clear()
- ' create fully functional assembly string
- Code = ("Imports System" & vbCrLf &
- "Imports System.Windows.Forms" & vbCrLf &
- "Imports Microsoft.Visualbasic" & vbCrLf &
- "Public Class TempClass" & vbCrLf &
- "Public Sub MyCode(ByVal Textbox2 As TextBox)" & vbCrLf &
- Code & vbCrLf &
- "End Sub" & vbCrLf &
- "End Class")
- ' create the compiler
- Dim vbProv = New VBCodeProvider()
- ' create parameters to pass to the compiler
- Dim vbParams = New CompilerParameters()
- ' add referenced assemblies.
- vbParams.ReferencedAssemblies.Add("System.dll")
- vbParams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
- vbParams.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
- ' generate an assembly in memory
- vbParams.GenerateExecutable = False
- vbParams.GenerateInMemory = True
- ' give it a name
- 'vbParams.OutputAssembly = "MyCode"
- vbParams.OutputAssembly = MainForm.titleTXT.Text
- ' compile the code and get the compiler results
- Dim compResults = vbProv.CompileAssemblyFromSource(vbParams, Code)
- ' check for compile errors
- If compResults.Errors.HasErrors Then
- Dim ErrorMsg As String = compResults.Errors.Count.ToString & " Errors:"
- For x As Integer = 0 To compResults.Errors.Count - 1
- ErrorMsg = ErrorMsg & vbCrLf & "Line: " & compResults.Errors(x).Line.ToString & " - " + compResults.Errors(x).ErrorText
- Next
- MainForm.outputTXT.Text = ErrorMsg & vbCrLf & vbCrLf + Code
- Else
- ' create instance of the temporary compiled class
- Dim obj As Object = compResults.CompiledAssembly.CreateInstance("TempClass")
- ' use textbox 2 for output
- Dim args() As Object = {MainForm.outputTXT}
- Try
- ' execute the code
- Dim result As Object = obj.GetType().InvokeMember("MyCode", BindingFlags.InvokeMethod, Nothing, obj, args)
- Catch Oops As Exception
- ' oops
- MessageBox.Show(Oops.Message)
- End Try
- End If
- End Sub
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement