none
Generate new executable from VB code NOT independent code! RRS feed

  • Question

  • Hello,

    My previous question was "How to generate an executable using VB code" and answered correctly.Now the problem is that in order to make what I want, the two codes of these applications must be connected. For example: The button text of the newly created executable must be the text of a textbox in the main executable. To make it easier: I have an exe with a textbox and a button. When someone clicks this button a new executable file is being created somewhere on the disk. When the user opens this new executable it contains a simple form with a button. The button text must be the same with the textbox text in the first form. I have the following code from the previous question,so does anybody knows how to modify it in order to do the above?

    Thanks to Crazypennie

    Imports System.CodeDom.Compiler
    
    Public Class Form1
        'This string is the code to be compiled by the application...
        'Here I hardcoded the code to be compiled, ... You, you can get it from wherever you want
        Private Code As String = CStr(
               <Code_Example>
    
                Imports System
                Imports System.Windows.Forms
    
                Class Form1 : Inherits Form
                      Private WithEvents Button1 As New Button
    
                      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                             Button1.Text = "ClickMe"
                             Application.EnableVisualStyles()
                             Button1.parent=Me
                      End Sub
    
                      private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
                             Messagebox.Show("Button1 Was Clicked")
                      End Sub
                 End Class
    
                </Code_Example>)
    
        '==================================================
        '=========== Now the code to do the compilation
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            '===Create a provider
            Dim Provider As CodeDomProvider = CodeDomProvider.CreateProvider("VisualBasic")
    
            '===Create the compiler parameters
            Dim cp As CompilerParameters = New CompilerParameters()
    
            '===Specify to create an executable (Here False will create a DLL file)
            cp.GenerateExecutable = True
    
            '===Always set to false (It can get very complicated to create it in memory)
            cp.GenerateInMemory = False
    
            '===Set target to winexe to create an executable
            '===m:Form1 specify to start the application in Form1. To start in Sub main,
            '     Specify the name of the module where the Sub Main is located
            cp.CompilerOptions = " /target:winexe /m:Form1"
    
            '===Specify the main Form
            cp.MainClass = "Form1"
    
            '===The DLL name you want to put as reference (To create a form application you
            '    will at least need a reference to System.dll and System.Windows.Forms.dll)
            cp.ReferencedAssemblies.AddRange({"System.dll", "System.Windows.Forms.dll"}.ToArray)
    
            '===Specify the path and name where to save the EXE file compiled
            cp.OutputAssembly = "C:\tst\SourceFileEXE.exe"
    
            '===Specify if you want the compiler to abort the compilation for the warnings
            cp.TreatWarningsAsErrors = False
    
            '===Compile and get the result of the compilation
            Dim Result As CompilerResults = Provider.CompileAssemblyFromSource(cp, Code)
    
            '===If the result has some error then get the error messages and show them in a message box
            If Result.Errors.HasErrors Then
                Dim ErrorString As String = "Compiler error:"
                For Each Err As CompilerError In Result.Errors
                    ErrorString &= Err.ToString & vbCrLf
                Next
                MsgBox(ErrorString, MsgBoxStyle.OkOnly, "Error while compiling")
            Else
                '===== If there is no error start the new process
                Process.Start("C:\tst\SourceFileEXE.exe")
            End If
    
        End Sub
    End Class

    Tuesday, July 17, 2012 10:41 AM

Answers

  • You can set the text of the button from code, as in the example.  You need to replace the text in the string with the text from the text box.  There are many ways to do this, but the simplest might be to use the String Replace method to replace the existing text with the new string

    code = code.Replace("ClickMe", TextBox1.Text)

    Depending on how you construct your code you might use a different procedure to insert the text at the appropriate point. 

    Tuesday, July 17, 2012 11:49 AM

All replies

  • You can set the text of the button from code, as in the example.  You need to replace the text in the string with the text from the text box.  There are many ways to do this, but the simplest might be to use the String Replace method to replace the existing text with the new string

    code = code.Replace("ClickMe", TextBox1.Text)

    Depending on how you construct your code you might use a different procedure to insert the text at the appropriate point. 

    Tuesday, July 17, 2012 11:49 AM
  • Isn't there any easier way, for example to compile just a specific form (I'll make that form as I want and then compile it). I think this would be the best....
    Tuesday, July 17, 2012 1:30 PM
  • I don't know what you mean by 'easier' - the replace is just one line of code that neerds to be added to the example you have already been provided with.

    If for some reason that change doesn't suit what you are actually trying to do then you will need to post the code that you are using.

    Tuesday, July 17, 2012 9:42 PM
  • Hi theblacksnake,

    Are you trying to write code within a running VB.Net program and then create an executable from it?

    You may also want to read this thread.>>

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/686e5445-90fa-4c9f-9a0b-ad056622df39/

    However that deals with compiling code from a text file of the entire VB.Net program as one file.

    '

    There is a compiler option to combine more than one file when trying to compile and generate a separate executable file.




    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

    Tuesday, July 17, 2012 10:05 PM
  • The code I use is that in  my first post. As you can see the code which is going to be compiled starts at <Code_Example> and ends at </Code_Example>. In that area I cannot add modules(errors come when in real project they don't), so I am asking you If there is another way to specify the code to be compiled

    Thursday, July 19, 2012 10:15 AM
  • Any changes you make in the <Code_Example> and ends at </Code_Example> section wil be part of the new application.  You need to change that to suit the program that you are trying to create.  But if you just want to adjust one portion of that text for a particular variable you can do that by changing the text afterit is created.  That's why I suggested using replace.  Any time after the string is created and before it is used at 

            '===Compile and get the result of the compilation
           
    Dim Result As CompilerResults = Provider.CompileAssemblyFromSource(cp, Code)

    do the replace with the variable from the textbox text. 

    Thursday, July 19, 2012 10:31 AM