Results 1 to 6 of 6

Thread: [RESOLVED] [2008] GUI To Create New Executable

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    159

    Resolved [RESOLVED] [2008] GUI To Create New Executable

    Hey all,

    I know it's possible to do but let me first explain what I'm talking about here.
    I have a form that will take input from the user and then depending on their
    choices, I want the form to be able to generate a unique .exe file that it will
    output.

    So for example, let's say I asked the user what is your favorite homepage and the user input http://www.vbforums.com and clicked "generate exe", an executable would be created that when launched would change their homepage to http://www.vbforums.com

    This is just an example and not what the application is about but what I need to find out is how to generate a new exe from the choices? It must be advanced stuff since I'm not familiar with it but know it can be done.

    Anyone know where to find answers or code to look at?

    chris

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] GUI To Create New Executable

    search the forum for the CodeDomProvider class

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    159

    Re: [2008] GUI To Create New Executable

    Thanks .paul. I will check that out!

    Cheers.

    Chris

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    159

    Re: [2008] GUI To Create New Executable

    I didn't find anything in this forum but was able to locate this C# code. How can I use this in VB.net?

    vb Code:
    1. public static bool CompileExecutable(String sourceName)
    2.         {
    3.             //Source file that you are compliling
    4.             FileInfo sourceFile = new FileInfo(sourceName);
    5.  
    6.             //Create a C# code provider
    7.             CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
    8.  
    9.             //Create a bool variable for to to use after the complie proccess to see if there are any erros
    10.             bool compileOk = false;
    11.  
    12.              //Make a name for the exe
    13.              String exeName = String.Format(@"{0}\{1}.exe",
    14.              System.Environment.CurrentDirectory, sourceFile.Name.Replace(".", "_"));
    15.  
    16.              //Creates a variable, cp, to set the complier parameters
    17.              CompilerParameters cp = new CompilerParameters();
    18.  
    19.              //You can generate a dll or a exe file, in this case we'll make an exe so we set this to true
    20.              cp.GenerateExecutable = true;
    21.  
    22.              //Set the name
    23.              cp.OutputAssembly = exeName;
    24.  
    25.              //Save the exe as a physical file
    26.              cp.GenerateInMemory = false;
    27.  
    28.              //Set the compliere to not treat warranings as erros
    29.              cp.TreatWarningsAsErrors = false;
    30.  
    31.              //Make it compile
    32.              CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceName);
    33.  
    34.              //if there are more then 0 erros...
    35.              if (cr.Errors.Count > 0)
    36.              {
    37.                  //A message box shows the erros that occured
    38.                  MessageBox.Show("Errors building {0} into {1}" +
    39.                      sourceName + cr.PathToAssembly);
    40.  
    41.                  //for each error that occured in the code make a separete message box
    42.                  foreach (CompilerError ce in cr.Errors)
    43.                  {
    44.                      MessageBox.Show("  {0}" + ce.ToString());
    45.                  }
    46.              }
    47.  
    48.              //if there are no erros...
    49.              else
    50.              {
    51.                  //a message box shows compliere results and a success message
    52.                  MessageBox.Show("Source {0} built into {1} successfully." +
    53.                      sourceName + cr.PathToAssembly);
    54.              }
    55.  
    56.              //if there are erros...
    57.              if (cr.Errors.Count > 0)
    58.              {
    59.                  //the bool variable that we made in the beggining is set to flase so the functions returns a false
    60.                  compileOk = false;
    61.              }
    62.  
    63.              //if there are no erros...
    64.              else
    65.              {
    66.                  //we are returning a true (success)
    67.                  compileOk = true;
    68.              }
    69.  
    70.              //return the result
    71.              return compileOk;
    72.         }
    73.  
    74.         //this is the code for the button, but don't have to use a button to do it you can make it on load or w/e
    75.         private void button1_Click_1(object sender, EventArgs e)
    76.         {
    77.             //create a stream writer and make a .cs file
    78.             TextWriter writer = new StreamWriter(Application.StartupPath + "\\myExe.cs");
    79.  
    80.             //we'll use the WriteLine() function to write all of our code
    81.             writer.WriteLine("using System;");
    82.             writer.WriteLine("using System.Collections.Generic;");
    83.             writer.WriteLine("using System.Text;");
    84.             writer.WriteLine("namespace TestAppi");
    85.             writer.WriteLine("{");
    86.             writer.WriteLine("class Program");
    87.             writer.WriteLine("{");
    88.             writer.WriteLine("static void Main(string[] args)");
    89.             writer.WriteLine("{");
    90.             writer.WriteLine("Console.WriteLine(\"Hello World\");");
    91.             writer.WriteLine("Console.ReadLine();");
    92.             writer.WriteLine("} } }");
    93.  
    94.             //close and save the file
    95.             writer.Close();
    96.            
    97.             //create a string to the file
    98.             string file = Application.StartupPath + "\\myExe.cs";
    99.  
    100.             //compile it
    101.             CompileExecutable(file);
    102.         }

  5. #5
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    Re: [2008] GUI To Create New Executable

    You can go line by line and convert it as long as you dont hit unsafe code. Use http://www.developerfusion.com/tools.../vb-to-csharp/ to convert line by line or blocks of code.

    It might be even worthwhile to create a c# dll with that function, and just referecne that dll in your code. Import it, create a new class instance, and call that method.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    159

    Re: [2008] GUI To Create New Executable

    That's a good idea! Gees...thanks masfenix

    Worked like a charm!

    Chris
    Last edited by cmmorris1; Oct 14th, 2008 at 10:12 PM.

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