Advertisement
albusmw

Untitled

Dec 15th, 2020
1,289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.08 KB | None | 0 0
  1.     Private Sub btnBuildFile_Click(sender As Object, e As EventArgs) Handles btnBuildFile.Click
  2.  
  3.         Dim Code As New List(Of String)
  4.  
  5.         Code.Add("Imports Microsoft.VisualBasic")
  6.         Code.Add("Imports System")
  7.         Code.Add("Imports System.Text")
  8.         Code.Add("Imports System.Drawing")
  9.         Code.Add("Imports System.Diagnostics")
  10.         Code.Add("Imports System.Collections.Generic")
  11.  
  12.         Code.Add("Public Class MainClass")
  13.         Code.Add("Public Sub ExecuteCode")
  14.         Code.AddRange(Split(tbCode.Text, System.Environment.NewLine))
  15.         Code.Add("End Sub")
  16.         Code.Add("End Class")
  17.  
  18.         RunXMLSequenceGen(Code.ToArray)
  19.  
  20.         Dim Output As New List(Of String)
  21.         Output.Add("<sequence>")
  22.         For Exp As Integer = 5 To 240 Step 5
  23.             Output.Add("<exp ReadOutMode=""Photographic"" CaptureCount=""1"" ExposureTime=""" & Exp.ToString.Trim & """ Gain=""50""/>")
  24.         Next Exp
  25.         Output.Add("</sequence>")
  26.  
  27.         Dim EXEPath As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
  28.         Dim FilePath As String = ("C:\TEMP\QHYSequence.qhycapture.xml")
  29.         System.IO.File.WriteAllLines(FilePath, Output.ToArray)
  30.  
  31.     End Sub
  32.  
  33.     Private Sub RunXMLSequenceGen(ByVal strCode As String())
  34.  
  35.         ' Creates object of the compiler
  36.         Dim objCodeCompiler As New VBCodeProvider
  37.  
  38.         'References/Parameters.
  39.         Dim objCompilerParameters As New System.CodeDom.Compiler.CompilerParameters()
  40.         objCompilerParameters.ReferencedAssemblies.Add("System.dll")
  41.         objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll")
  42.         objCompilerParameters.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
  43.         objCompilerParameters.GenerateInMemory = True
  44.  
  45.         'Compiler Results
  46.         Dim objCompileResults As System.CodeDom.Compiler.CompilerResults = objCodeCompiler.CompileAssemblyFromSource(objCompilerParameters, Join(strCode, System.Environment.NewLine))
  47.  
  48.         'If an Error occurs
  49.         If objCompileResults.Errors.HasErrors Then
  50.             MsgBox("Compiling error: Line > " & objCompileResults.Errors(0).Line.ToString & ", " & objCompileResults.Errors(0).ErrorText)
  51.             Exit Sub
  52.         End If
  53.  
  54.         'Creates assembly
  55.         Dim objAssembly As System.Reflection.Assembly = objCompileResults.CompiledAssembly
  56.  
  57.         Dim objTheClass As Object = objAssembly.CreateInstance("MainClass")
  58.         If objTheClass Is Nothing Then
  59.             MsgBox("Can't load class...")
  60.             Exit Sub
  61.         End If
  62.  
  63.         'Try to excute
  64.         Try
  65.             objTheClass.GetType.InvokeMember("ExecuteCode", System.Reflection.BindingFlags.InvokeMethod, Nothing, objTheClass, Nothing)
  66.         Catch ex As Exception
  67.             If IsNothing(ex.InnerException) = True Then
  68.                 MsgBox("Execution error:" & ex.Message)
  69.             Else
  70.                 MsgBox("Execution error:" & ex.Message & System.Environment.NewLine & " >> " & ex.InnerException.Message)
  71.             End If
  72.         End Try
  73.  
  74.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement