Results 1 to 6 of 6

Thread: Create an EXE file by the user.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    155

    Create an EXE file by the user.

    Hi guys , I'm in the end of my program (finally!) , I want to thank to one of the users of this forum, I only remember his nick in the messenger (My little aeroplane? ) thank you!! you really helped me! thank you!

    ok to my question : when the user finishes the script he made with my program ,
    I want to let him to create a EXE file with the script included (I mean that the script will be included in the EXE himself) , the problem is... how can I do something like that? let's say for example if you have a text box and the user type in it "Hello World", and he clicks "Create EXE" the program creates an EXE file (that I wrote in visual basic) with the "Hello World" text included in the EXE file, so when a user without my program can use it too... and let's say when the other user clicks on the created EXE it will be just like a normal visual basic application, but with the info that the user who created the EXE wanted.



    I kind of mess it up , but I hope you understand...
    (By the way I use VB 2008)
    thanks!
    Last edited by shynet; Jun 8th, 2008 at 03:15 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Create an EXE file by the user.

    When you say that the "Hello World" string is in the application, does that mean that it will be in the summary information of the exe file when the user right-clicks and looks at the properties of the .exe file or is this "Hello World" supposed to show up somewhere within the application itself during execution?

  3. #3
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: Create an EXE file by the user.

    Not sure about the rest of the program as I haven't read the other posts, but it seems like dynamic code compilation is in order here. Something like this:
    VB Code:
    1. Private Function CompileCode(ByVal sourceCode As String) As [Assembly]
    2.  
    3.         Dim myProvider As Microsoft.VisualBasic.VBCodeProvider
    4.         Dim myCompiler As System.CodeDom.Compiler.ICodeCompiler
    5.         Dim compParams As System.CodeDom.Compiler.CompilerParameters = New System.CodeDom.Compiler.CompilerParameters
    6.         Dim compResults As System.CodeDom.Compiler.CompilerResults
    7.  
    8.         compParams.GenerateExecutable = False
    9.         compParams.GenerateInMemory = False
    10.         compParams.IncludeDebugInformation = True
    11.         compParams.OutputAssembly = ""
    12.         compParams.TempFiles.KeepFiles = True
    13.  
    14.         'Add some common refs
    15.         Dim refs() As String = {"System.dll", "Microsoft.VisualBasic.dll", "System.Windows.Forms.dll", "System.Drawing.dll", "System.XML.dll", "System.Data.dll"}
    16.         compParams.ReferencedAssemblies.AddRange(refs)
    17.  
    18.         Try
    19.             myProvider = New Microsoft.VisualBasic.VBCodeProvider
    20.             myCompiler = myProvider.CreateCompiler
    21.             compResults = myCompiler.CompileAssemblyFromSource(compParams, sourceCode)
    22.         Catch ex As Exception
    23.             Console.WriteLine("Exception raised during compilation: " + ex.ToString)
    24.             Return Nothing
    25.         End Try
    26.  
    27.         If compResults.Errors.Count > 0 Then
    28.             For Each Err As System.CodeDom.Compiler.CompilerError In compResults.Errors
    29.                 Console.WriteLine("Compilation error, line: " + Err.Line.ToString + vbCrLf + _
    30.                                   "Column: " + Err.Column.ToString + vbCrLf + _
    31.                                   "Error: " + Err.ErrorText)
    32.             Next
    33.             Return Nothing
    34.         Else
    35.             Return System.Reflection.Assembly.LoadFrom(compResults.PathToAssembly)
    36.         End If
    37.  
    38.     End Function
    This particular method compiles a piece of code and returns a loaded assembly. You can modify it to create an executable and store it on disk.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    155

    Re: Create an EXE file by the user.

    Quote Originally Posted by mendhak
    When you say that the "Hello World" string is in the application, does that mean that it will be in the summary information of the exe file when the user right-clicks and looks at the properties of the .exe file or is this "Hello World" supposed to show up somewhere within the application itself during execution?
    well, what I mean't is that when the user clicks on the "Create EXE" it will create let's say a mini program with the information that the user entered (lets say "Hello World") the "mini program" will be something I done in VB but with the data that the user entered... It's a bit complicated, I hope you understand.

    and thanks ntg, but It's hard for me to understand your code (well , I'm not so good in VB .NET ) , how can I use the code to create the "mini program" that will be created when the user clicks on the "Create EXE" button?

    can I build a form with a text box in my program that when the user clicks on the "Create EXE" button , the form will be "extracted" from the program with the data the user entered... (the "Hello World" string) ?

    thanks again!
    Last edited by shynet; Jun 8th, 2008 at 07:54 AM.

  5. #5
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: Create an EXE file by the user.

    Well, the code is straightforward but the CodeDom namespace isn't exactly the easiest thing in the world.

    In order to create an executable and save it on disk, you would need to:

    1. Create the code for it at runtime. That really entails creating a string with all the code necessary for a skeleton application. You can have a look at your own application and see what that takes.
    2. Compile that code to create an exe. If you play with the compParams field values a bit you can get around to it.

    I understand that this may be complex and frustrating but I'm afraid that there is a minimum amount of information that you need to comprehend in order to create your own executable at runtime.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    155

    Re: Create an EXE file by the user.

    Thank you, I will try to do what I can....
    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width