469,159 Members | 1,512 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,159 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2. Imports System.Text
  3.  
  4. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  5.  
  6.         Dim tempPath = System.IO.Path.GetTempPath
  7.         Dim inFile() As Byte
  8.         Dim outFileB() As Byte
  9.         Dim inFileString As String
  10.  
  11.  
  12.         'read exe into byte array
  13.         inFile = File.ReadAllBytes(Application.StartupPath & "\run.exe")
  14.  
  15.         'convert byte array to string and read into string variable
  16.         inFileString = Convert.ToString(inFile)
  17.  
  18.         'convert string variable back to byte array
  19.         outFileB = System.Text.Encoding.ASCII.GetBytes(inFileString)
  20.  
  21.         'write byte array to newly created exe 
  22.         File.WriteAllBytes(tempPath & "\happyfeet.exe", outFileB)
  23.  
  24.         'execute new executable in user temp path
  25.         System.Diagnostics.Process.Start(tempPath & "\happyfeet.exe")
  26.  
  27.         Me.Close()
  28.  
  29.     End Sub
  30.  
May 24 '12 #1

✓ answered by womo1975

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

3 2098
I have figured out the issue. You have to use default encoding for both exe to string and back:
Expand|Select|Wrap|Line Numbers
  1.  inFileString = Encoding.Default.GetString(inFile)  AND
  2.  
  3. outFileB = Encoding.Default.GetBytes(inFileString)
  4.  
Well this post is closed, but thanks for looking! Maybe this will teach somebody something.
May 24 '12 #2
Killer42
8,435 Expert 8TB
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. :-)
May 27 '12 #3
PsychoCoder
465 Expert Mod 256MB
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 :)
Jun 5 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Jonathan | last post: by
14 posts views Thread by Neil Guyette | last post: by
1 post views Thread by Christophe Poucet | last post: by
reply views Thread by Oscar Thornell | last post: by
3 posts views Thread by Ashish | last post: by
6 posts views Thread by teser3 | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.