none
Create executable in memory (vb.net c#) RRS feed

  • Question

  • Hi friends, I need to create an executable in memory and then allow it to be downloaded through a web service. I am going to embed a session token for this application, which is why I create it dynamically.  My application will be in azure web service.

    I've tried creating the file in a directory and if it generates the .exe file, but I need to do it in memory because I don't want to handle directories in Azure Services.

    However, it does not perform any download. Anyone know what is missing or what I can do.

    Sorry for my English, it's not my mother tongue. 

    Public Function DownloadFile() As System.IO.Stream Implements IServicioRemotoAutenticacionWebApi.DownloadFile
            Dim codeProvider As New VBCodeProvider()
            'Dim Output As String = "C:\Program Files\MyApp.exe"
            Dim results As CompilerResults
    
            Dim parameters As New CompilerParameters()
    
    
            'Make sure we generate an EXE, not a DLL
            parameters.GenerateExecutable = True
            parameters.GenerateInMemory = True
            parameters.CompilerOptions = ("/target:winexe") & " " & "/win32icon:" & """" & "SSICONO.ico" & """"
    
            results = codeProvider.CompileAssemblyFromSource(parameters, " 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")
    
            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
            Return results.CompiledAssembly.GetFile("Installer.exe")
        End Function


    • Edited by Lusamine Monday, October 14, 2019 3:17 PM
    • Moved by Alberto PoblacionMVP Monday, October 14, 2019 3:18 PM Moved from the C# forum to the VB forum because it is entirely written in the latter language
    Monday, October 14, 2019 3:08 PM

Answers

  • I think you have to use Emit to write it to a memory stream which you write to the output to the web service. untested

            Dim syntaxTree = VisualBasicSyntaxTree.ParseText(" 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")
            Dim lst = New List(Of VisualBasicSyntaxTree)
            lst.Add(syntaxTree)
            Dim complier = VisualBasicCompilation.Create("myexe.exe", lst)
            Dim ms As New MemoryStream()
            Dim emitResults = complier.Emit(ms)

    • Marked as answer by Lusamine Tuesday, October 15, 2019 10:06 PM
    Monday, October 14, 2019 7:03 PM

All replies

  • I think you have to use Emit to write it to a memory stream which you write to the output to the web service. untested

            Dim syntaxTree = VisualBasicSyntaxTree.ParseText(" 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")
            Dim lst = New List(Of VisualBasicSyntaxTree)
            lst.Add(syntaxTree)
            Dim complier = VisualBasicCompilation.Create("myexe.exe", lst)
            Dim ms As New MemoryStream()
            Dim emitResults = complier.Emit(ms)

    • Marked as answer by Lusamine Tuesday, October 15, 2019 10:06 PM
    Monday, October 14, 2019 7:03 PM
  • Hi,

    If you want to download files from a remote server through a web server, take the txt file as an example, you can try the following code.

    Public Shared Sub DownLoad(ByVal Url As String, ByVal FileName As String)
            Dim Value As Boolean = False
            Dim response As WebResponse = Nothing
            Dim stream As Stream = Nothing
    
            Try
                Dim request As HttpWebRequest = CType(WebRequest.Create(Url), HttpWebRequest)
                response = request.GetResponse()
                stream = response.GetResponseStream()
    
                If Not response.ContentType.ToLower().StartsWith("text/") Then
                    Value = SaveBinaryFile(response, FileName)
                End If
    
            Catch err As Exception
                Dim aa As String = err.ToString()
            End Try
        End Sub
    
        Private Shared Function SaveBinaryFile(ByVal response As WebResponse, ByVal FileName As String) As Boolean
            Dim Value As Boolean = True
            Dim buffer As Byte() = New Byte(1023) {}
    
            Try
                If File.Exists(FileName) Then File.Delete(FileName)
                Dim outStream As Stream = System.IO.File.Create(FileName)
                Dim inStream As Stream = response.GetResponseStream()
                Dim l As Integer
    
                Do
                    l = inStream.Read(buffer, 0, buffer.Length)
                    If l > 0 Then outStream.Write(buffer, 0, l)
                Loop While l > 0
    
                outStream.Close()
                inStream.Close()
            Catch
                Value = False
            End Try
    
            Return Value
        End Function

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 15, 2019 9:38 AM
    Moderator
  • Thank you very much, it has helped me a lot, the only negative is that I have to add several libraries to my project.

    Tuesday, October 15, 2019 10:07 PM