By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

Executable conversion issue with VB 2008

P: 2
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.

Share this Question
Share on Google+
3 Replies


P: 2
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

Expert 5K+
P: 8,434
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
Expert Mod 100+
P: 465
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.