none
run exe from form RRS feed

  • Question

  • Why not open the program even though there is no error

    Imports System.Reflection
    Imports System.Runtime.CompilerServices
    Imports System.IO
    
    Public Class Form1
    
        Private Function ReadFileAsByte(ByVal Filepath As String) As Byte()
            If IO.File.Exists(Filepath) = False Then
                MsgBox("File not exists")
                Return Nothing
            End If
            Dim fstream As New FileStream(Filepath, FileMode.Open, FileAccess.Read)
            Dim binReader As New BinaryReader(fstream)
            Dim fbinary() As Byte = Nothing
            Try
                fbinary = binReader.ReadBytes(Convert.ToInt32(fstream.Length))
                fstream.Close()
                binReader.Close()
                Return fbinary
            Catch ex As Exception
                MsgBox(ex.Message.ToString)
            Finally
                If Not fstream Is Nothing Then
                    fstream.Close()
                End If
                If Not binReader Is Nothing Then
                    binReader.Close()
                End If
    
            End Try
            Return fbinary
        End Function
        Private Sub RunExecutableFromMemory(ByVal bytes As Byte())
            Try
                Dim asmb As System.Reflection.Assembly = System.Reflection.Assembly.Load(bytes)
                Dim entryPt As MethodInfo = asmb.EntryPoint
                If Not entryPt Is Nothing Then
                    Dim objectValue As Object = RuntimeHelpers.GetObjectValue(asmb.CreateInstance(entryPt.Name))
                    entryPt.Invoke(RuntimeHelpers.GetObjectValue(objectValue), Nothing)
                End If
            Catch ex As TargetParameterCountException
            Catch ex As Exception
    
            End Try
    
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim bt() As Byte
            bt = ReadFileAsByte("C:\Windows\system32\calc.exe")
    
            If Not bt Is Nothing Then
                RunExecutableFromMemory(bt)
            Else
                MsgBox("Error")
            End If
        End Sub
    End Class
    

    Saturday, September 9, 2017 7:21 PM

Answers

  •  According to  This CodeProject Link, that code is meant for running .Net Console applications or Windows Form applications.  In the case of a Windows Form application, you have to have access to the code so you can modify the application to use a "Sub Main".   It will not work for just any old exe file.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Saturday, September 9, 2017 11:19 PM
    • Marked as answer by programmervb.net Sunday, September 10, 2017 12:51 PM
    Saturday, September 9, 2017 10:35 PM

All replies

  • Probably there goes something wrong and normally is shown what. But you have hidden that with probably this code. 

    Therefore remove that and see why it does nothing. 


    Success
    Cor

    Saturday, September 9, 2017 7:57 PM
  • Probably there goes something wrong and normally is shown what. But you have hidden that with probably this code. 

    Therefore remove that and see why it does nothing. 


    Success
    Cor

    Please professor
      Fixing this code is really wrong

     Could not load file or assembly '918528 bytes loaded from WindowsApplication10, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

    Saturday, September 9, 2017 8:01 PM
  •  According to  This CodeProject Link, that code is meant for running .Net Console applications or Windows Form applications.  In the case of a Windows Form application, you have to have access to the code so you can modify the application to use a "Sub Main".   It will not work for just any old exe file.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Saturday, September 9, 2017 11:19 PM
    • Marked as answer by programmervb.net Sunday, September 10, 2017 12:51 PM
    Saturday, September 9, 2017 10:35 PM
  •  That code is only meant for running .Net Console applications or Windows Form applications.  In the case of a Windows Form application, you have to have access to the code so you can modify the application to use a "Sub Main".   It will not work for just any old exe file.


    If you say it can`t be done then i`ll try it

    I want to open the executable file and take the connection from the tcp

    like this

    https://www.youtube.com/watch?v=GMycMPijRqM

    Saturday, September 9, 2017 10:44 PM