0

I have an app with a button "cmdGenerate". What I want is, when I click on that button It needs to create a Console Application with a specific code inside it.

For example that Console Application that needs to be generated has this function :

    Dim WIDTH as integer = someValuePlacedWhenGeneratingFile
    Dim HEIGHT as integer = someValuePlacedWhenGeneratingFile

    Function calculateArea(x as Integer, y as Integer) as Integer 
         return x*y
    End Function

someValuePlacedWhenGeneratingFile is what I want to change when generating file. Is that possible ?

The Generated exe file should be just a console application with a function like the one above and WIDTH and HEIGHT set to a values which I would set in my Main Generator Application.

2
  • 2
    Possible duplicate of Dynamically create a compiled .NET exe from source code? Commented Jan 9, 2017 at 22:46
  • 1
    Use the duplicate mentioned by @GSerg; just replace "CSharp" with "VisualBasic" (for one answer) or "csc" with "vbc" for the other. Commented Jan 9, 2017 at 22:49

1 Answer 1

0

In case anyone needs this here is a full example of how I did it.

Create a form with a textbox(named "txtCode") and a button(name "cmdGenerate").

-How this works: You enter the code which will the generated .exe contain and then just press cmbButton. After that, the file .exe is generated and when launched it executes commands just like any other application.

This is the method for file generation:

Public Sub generateFile()

    Dim codeProvider As New VBCodeProvider()
    Dim icc As ICodeCompiler = codeProvider.CreateCompiler
    Dim Output As String = "C:\Program Files\MyApp.exe"


    Dim parameters As New CompilerParameters()
    Dim results As CompilerResults

    'Make sure we generate an EXE, not a DLL
    parameters.GenerateExecutable = True
    parameters.OutputAssembly = Output
    parameters.CompilerOptions = ("/target:winexe" & " " & "/win32icon:" & """") & "C:\Program Files\MyIcons\Icon.ico" & """"

    results = icc.CompileAssemblyFromSource(parameters, txtCode.Text)

    If results.Errors.Count > 0 Then
        'There were compiler errors   
        Dim CompErr As CompilerError
        For Each CompErr In results.Errors
            MessageBox.Show(
                "Line number " & CompErr.Line &
                ", Error Number: " & CompErr.ErrorNumber &
                ", '" & CompErr.ErrorText & ";" &
                Environment.NewLine & Environment.NewLine)
        Next
    Else

        'Successfull Compile
        MessageBox.Show("Your file is successfully generated!", "Success")
    End If

End Sub

And then, just call that method after you've finished writting your code inside txtCode textbox.

The code example that goes inside txtCode is:

Imports System
Imports System.Diagnostics

Module Module1

    Sub Main()

        Dim CmdStr As String = "CMD ARGUMENTS----"
        Dim startInfo As New ProcessStartInfo("CMD.EXE")
        startInfo.WindowStyle = ProcessWindowStyle.Minimized
        startInfo.WindowStyle = ProcessWindowStyle.Hidden
        startInfo.CreateNoWindow = True
        startInfo.UseShellExecute = False
        startInfo.Arguments = CmdStr
        Process.Start(startInfo)

    End Sub

End Module
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.