Results 1 to 8 of 8

Thread: [RESOLVED] [2008] Compile exe from user input problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    159

    Resolved [RESOLVED] [2008] Compile exe from user input problem

    Hey guys,

    I have a big problem that I know I can not work out by myself so I have tried to simplify what I need so I can figure out the bigger picture by myself so here goes.

    I borrowed kleinma's open default browser code from the codebank and I want to take the user input url from textbox1 and when button1 is clicked it will create an executable that will launch the domain input into the textbox.

    That's the very simple version of it, if someone can help me figure out how to make it work I would REALLY appreciate it! Here is the code I have for this simple form. It has one textbox for user input and one button to generate the exe after user inputs the url into the textbox.

    I noticed some code problems in the button1 that I found online, and I'm not sure how to make this work correctly but with kleinma's code you call it like this:

    BrowserExec.LaunchNewBrowser(url here)

    So I think this code goes into the button1 right? and then is this the correct code to be generating an exe from it? Can anyone give me my missing piece?

    vb Code:
    1. Imports Microsoft.Win32
    2. Imports System
    3. Imports System.CodeDom.Compiler
    4. Imports System.ComponentModel
    5. Imports System.Drawing
    6. Imports System.IO
    7. Imports System.Reflection
    8. Imports System.Windows.Forms
    9.  
    10. Public Class Form1
    11.  
    12.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    13.  
    14.     End Sub
    15.  
    16.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    17.  
    18.     End Sub
    19.  
    20.     Public Shared Function Button1_Click((ByVal sourceName As String) As Boolean
    21.  
    22.         Dim sourceFile As FileInfo = New FileInfo(sourceName)
    23.         Dim provider As CodeDomProvider = Nothing
    24.         Dim compileOk As Boolean = False
    25.  
    26.         ' Select the code provider based on the input file extension.
    27.         If sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) = ".CS" Then
    28.  
    29.             provider = New Microsoft.CSharp.CSharpCodeProvider()
    30.  
    31.         ElseIf sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) = ".VB" Then
    32.  
    33.             provider = New Microsoft.VisualBasic.VBCodeProvider()
    34.  
    35.         Else
    36.             Console.WriteLine("Source file must have a .cs or .vb extension")
    37.         End If
    38.  
    39.         If Not provider Is Nothing Then
    40.  
    41.             ' Format the executable file name.
    42.             ' Build the output assembly path using the current directory
    43.             ' and <source>_cs.exe or <source>_vb.exe.
    44.  
    45.             Dim exeName As String = String.Format("{0}\{1}.exe", _
    46.                 System.Environment.CurrentDirectory, _
    47.                 sourceFile.Name.Replace(".", "_"))
    48.  
    49.             Dim cp As CompilerParameters = New CompilerParameters()
    50.  
    51.             ' Generate an executable instead of
    52.             ' a class library.
    53.             cp.GenerateExecutable = True
    54.  
    55.             ' Specify the assembly file name to generate.
    56.             cp.OutputAssembly = exeName
    57.  
    58.             ' Save the assembly as a physical file.
    59.             cp.GenerateInMemory = False
    60.  
    61.             ' Set whether to treat all warnings as errors.
    62.             cp.TreatWarningsAsErrors = False
    63.  
    64.             ' Invoke compilation of the source file.
    65.             Dim cr As CompilerResults = provider.CompileAssemblyFromFile(cp, _
    66.                 sourceName)
    67.  
    68.             If cr.Errors.Count > 0 Then
    69.                 ' Display compilation errors.
    70.                 Console.WriteLine("Errors building {0} into {1}", _
    71.                     sourceName, cr.PathToAssembly)
    72.  
    73.                 Dim ce As CompilerError
    74.                 For Each ce In cr.Errors
    75.                     Console.WriteLine("  {0}", ce.ToString())
    76.                     Console.WriteLine()
    77.                 Next ce
    78.             Else
    79.                 ' Display a successful compilation message.
    80.                 Console.WriteLine("Source {0} built into {1} successfully.", _
    81.                     sourceName, cr.PathToAssembly)
    82.             End If
    83.  
    84.             ' Return the results of the compilation.
    85.             If cr.Errors.Count > 0 Then
    86.                 compileOk = False
    87.             Else
    88.                 compileOk = True
    89.             End If
    90.         End If
    91.         Return compileOk
    92.  
    93.  
    94.     End Function
    95.  
    96. End Class


    Thanks for looking!

    Chris

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    159

    Re: [2008] Compile exe from user input problem

    I'm not sure I even am using the right code to generate the exe. Can anyone point me in the right direction for how to do something like this?
    It sounds pretty straight forward easy but I'm having a really difficult time with this.
    Chris

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

    Re: [2008] Compile exe from user input problem

    try this: i've included the zipped project so you can see it working.

    vb.net Code:
    1. Imports Microsoft.VisualBasic
    2. Imports System.CodeDom.Compiler
    3.  
    4. Public Class Form1
    5.  
    6.     Private Sub cmdCompile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCompile.Click
    7.  
    8.         txtCode.Text = txtCode.Text.Replace(String.Format("{0}{1}{1}{2}", "(", Chr(34), ")"), String.Format("{0}{3}{1}{3}{2}", "(", TextBox1.Text, ")", Chr(34)))
    9.  
    10.         ' Create the compiler.
    11.        
    12.         Dim provider As CodeDomProvider = New Microsoft.VisualBasic.VBCodeProvider()
    13.         ' Define some parameters.
    14.         ' In this case, we choose to save the assembly file to a file.
    15.         Dim Param As New CompilerParameters()
    16.         Param.GenerateExecutable = True
    17.         Param.OutputAssembly = "TestApp.exe"
    18.         Param.IncludeDebugInformation = False
    19.         Param.CompilerOptions = "/target:winexe"
    20.  
    21.         ' Add some common assembly references (based on the currently running application).
    22.         Dim Asm As System.Reflection.Assembly
    23.         For Each Asm In AppDomain.CurrentDomain.GetAssemblies()
    24.             Param.ReferencedAssemblies.Add(Asm.Location)
    25.         Next
    26.  
    27.         ' Compile the code.
    28.         Dim Results As CompilerResults = provider.CompileAssemblyFromSource(Param, txtCode.Text)
    29.  
    30.         ' Check for errors.
    31.         If Results.Errors.Count > 0 Then
    32.             Dim Err As CompilerError
    33.             Dim ErrorString As String = String.Empty
    34.             For Each Err In Results.Errors
    35.                 ErrorString &= Err.ToString()
    36.             Next
    37.             MessageBox.Show(ErrorString)
    38.         Else
    39.             ' Launch the new application.
    40.             Process.Start("TestApp.exe")
    41.         End If
    42.  
    43.     End Sub
    44.  
    45. End Class
    Attached Files Attached Files
    Last edited by .paul.; Nov 2nd, 2008 at 01:40 PM.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    159

    Re: [2008] Compile exe from user input problem

    Man, that is perfect! Thank you VERY much!

    Chris

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

    Re: [RESOLVED] [2008] Compile exe from user input problem

    i've made an amendment to the code + the zip file in post #3
    it'll run windowless now.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    159

    Re: [RESOLVED] [2008] Compile exe from user input problem

    Hey .paul. I really appreciate that a lot man, it's running great :-) Looks like it even picks out the default browser on its own :-)

    Thanks again!

    Chris

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

    Re: [RESOLVED] [2008] Compile exe from user input problem

    process.start(url) - loads url in your default browser

  8. #8
    Lively Member
    Join Date
    Aug 2008
    Posts
    75

    Re: [RESOLVED] [2008] Compile exe from user input problem

    Quote Originally Posted by .paul.
    process.start(url) - loads url in your default browser
    vista has problems with this though... atleast my 64x ultimate... everytime i use process.start(url) i get an error saying it cannot find firefox (my default browser) and then the page will load anyway... ive had to use this code to fix that... thank you very much paul
    How To Make A Youtube Downloader
    I use Visual Studio 2008 Pro...

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