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!
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?
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:
Private Function CompileCode(ByVal sourceCode As String) As [Assembly]
Dim myProvider As Microsoft.VisualBasic.VBCodeProvider
Dim myCompiler As System.CodeDom.Compiler.ICodeCompiler
Dim compParams As System.CodeDom.Compiler.CompilerParameters = New System.CodeDom.Compiler.CompilerParameters
Dim compResults As System.CodeDom.Compiler.CompilerResults
compParams.GenerateExecutable = False
compParams.GenerateInMemory = False
compParams.IncludeDebugInformation = True
compParams.OutputAssembly = ""
compParams.TempFiles.KeepFiles = True
'Add some common refs
Dim refs() As String = {"System.dll", "Microsoft.VisualBasic.dll", "System.Windows.Forms.dll", "System.Drawing.dll", "System.XML.dll", "System.Data.dll"}
compParams.ReferencedAssemblies.AddRange(refs)
Try
myProvider = New Microsoft.VisualBasic.VBCodeProvider
myCompiler = myProvider.CreateCompiler
compResults = myCompiler.CompileAssemblyFromSource(compParams, sourceCode)
Catch ex As Exception
Console.WriteLine("Exception raised during compilation: " + ex.ToString)
Return Nothing
End Try
If compResults.Errors.Count > 0 Then
For Each Err As System.CodeDom.Compiler.CompilerError In compResults.Errors
Console.WriteLine("Compilation error, line: " + Err.Line.ToString + vbCrLf + _
"Column: " + Err.Column.ToString + vbCrLf + _
"Error: " + Err.ErrorText)
Next
Return Nothing
Else
Return System.Reflection.Assembly.LoadFrom(compResults.PathToAssembly)
End If
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.
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!
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.
Re: Create an EXE file by the user.
Thank you, I will try to do what I can....
Thanks!