Executable conversion issue with VB 2008

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • womo1975
    New Member
    • May 2012
    • 2

    Executable conversion issue with VB 2008

    My issue is when I read from an existing executable and convert it to string and back to executable windows says it is not a recognized 32 bit or 64 bit application? The code below is real code I have rewritten many times but to no avail. If I leave out the part where I convert the bytes to string and just copy the binary and create a new exe with the binary the program works. But the whole concept of string converting back to valid binary has me stumped? This is Visual Basic 2008 code:

    Code:
    Imports System.IO
    Imports System.Text
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim tempPath = System.IO.Path.GetTempPath
            Dim inFile() As Byte
            Dim outFileB() As Byte
            Dim inFileString As String
            
    
            'read exe into byte array
            inFile = File.ReadAllBytes(Application.StartupPath & "\run.exe")
    
            'convert byte array to string and read into string variable
            inFileString = Convert.ToString(inFile)
    
            'convert string variable back to byte array
            outFileB = System.Text.Encoding.ASCII.GetBytes(inFileString)
    
            'write byte array to newly created exe 
            File.WriteAllBytes(tempPath & "\happyfeet.exe", outFileB)
    
            'execute new executable in user temp path
            System.Diagnostics.Process.Start(tempPath & "\happyfeet.exe")
    
            Me.Close()
    
        End Sub
  • womo1975
    New Member
    • May 2012
    • 2

    #2
    I have figured out the issue. You have to use default encoding for both exe to string and back:
    Code:
     inFileString = Encoding.Default.GetString(inFile)  AND
    
    outFileB = Encoding.Default.GetBytes(inFileString)
    Well this post is closed, but thanks for looking! Maybe this will teach somebody something.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Thanks for posting your solution. It's likely to be helpful if anyone else comes looking with the same sort of question.

      One point, though. The experts in VB2008 are more likely to be found over in our VB.Net forum. This is the forum for the old (and I do mean old) pre-dotnet versions of VB. There's overlap of course, but you'll likely get a better response if you hit your target audience. :-)

      Comment

      • PsychoCoder
        Recognized Expert Contributor
        • Jul 2010
        • 465

        #4
        VB 8 is VB.NET so it's in the wrong forum. Fear not I'll move it to the VB.NET forum for you :)

        Comment

        Working...