Advertisement
calfred2808

coderMod

Dec 27th, 2016
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.73 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Reflection
  3. Imports System.CodeDom
  4. Imports System.CodeDom.Compiler
  5. Imports Microsoft.VisualBasic
  6.  
  7.  
  8. 'MainForm.inputTXT.Text = textbox1.text
  9. 'MainForm.outputTXT.Text = textbox2.text
  10. 'MainForm.titleTXT.Text = as title
  11.  
  12. Module coderMod
  13.  
  14.  
  15.  
  16.     Public Sub CoderX()
  17.         ' read code from textbox
  18.         Dim Code As String = MainForm.inputTXT.Text
  19.         ' clear output textbox
  20.         MainForm.outputTXT.Clear()
  21.  
  22.         ' create fully functional assembly string
  23.  
  24.         Code = ("Imports System" & vbCrLf &
  25.                 "Imports System.Windows.Forms" & vbCrLf &
  26.                 "Imports Microsoft.Visualbasic" & vbCrLf &
  27.                 "Public Class TempClass" & vbCrLf &
  28.                 "Public Sub MyCode(ByVal Textbox2 As TextBox)" & vbCrLf &
  29.                 Code & vbCrLf &
  30.                 "End Sub" & vbCrLf &
  31.                 "End Class")
  32.  
  33.         ' create the compiler
  34.         Dim vbProv = New VBCodeProvider()
  35.         ' create parameters to pass to the compiler
  36.         Dim vbParams = New CompilerParameters()
  37.         ' add referenced assemblies.  
  38.         vbParams.ReferencedAssemblies.Add("System.dll")
  39.         vbParams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
  40.         vbParams.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
  41.         ' generate an assembly in memory
  42.         vbParams.GenerateExecutable = False
  43.         vbParams.GenerateInMemory = True
  44.         ' give it a name
  45.         'vbParams.OutputAssembly = "MyCode"
  46.         vbParams.OutputAssembly = MainForm.titleTXT.Text
  47.         ' compile the code and get the compiler results
  48.         Dim compResults = vbProv.CompileAssemblyFromSource(vbParams, Code)
  49.         ' check for compile errors  
  50.         If compResults.Errors.HasErrors Then
  51.             Dim ErrorMsg As String = compResults.Errors.Count.ToString & " Errors:"
  52.             For x As Integer = 0 To compResults.Errors.Count - 1
  53.                 ErrorMsg = ErrorMsg & vbCrLf & "Line: " & compResults.Errors(x).Line.ToString & " - " + compResults.Errors(x).ErrorText
  54.             Next
  55.             MainForm.outputTXT.Text = ErrorMsg & vbCrLf & vbCrLf + Code
  56.         Else
  57.             ' create instance of the temporary compiled class
  58.             Dim obj As Object = compResults.CompiledAssembly.CreateInstance("TempClass")
  59.             ' use textbox 2 for output
  60.             Dim args() As Object = {MainForm.outputTXT}
  61.             Try
  62.                 ' execute the code  
  63.                 Dim result As Object = obj.GetType().InvokeMember("MyCode", BindingFlags.InvokeMethod, Nothing, obj, args)
  64.             Catch Oops As Exception
  65.                 ' oops
  66.                 MessageBox.Show(Oops.Message)
  67.             End Try
  68.         End If
  69.     End Sub
  70.  
  71. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement