Reading/Writing Bytes

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 99
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Reading/Writing Bytes

 
0
  #1
Jul 24th, 2009
Image of my problem

http://i31.tinypic.com/2v3ivyq.png

Left = Actual notepad.exe opened in notepad
Right = what i get after i read/write.

Goal - Be able to store notepad.exe between to splits and then when the application that is houses the bytes is run, it reads those bytes and saves them.

I have the reading/writing sussed but it looks to me like the encoding is the problem.

Builder -
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace Polyamory
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. static string byteString(byte[] myBytes)
  21. {
  22. ASCIIEncoding enc = new ASCIIEncoding();
  23. return enc.GetString(myBytes);
  24. }
  25.  
  26. private void buildButton_Click(object sender, EventArgs e)
  27. {
  28. try
  29. {
  30. string sPath = "";
  31.  
  32. SaveFileDialog saveFile = new SaveFileDialog();
  33.  
  34. saveFile.FileName = "built.exe";
  35. saveFile.Filter = "Executable Files (*.exe)|*.exe";
  36.  
  37. if (saveFile.ShowDialog() == DialogResult.OK)
  38. sPath = saveFile.FileName;
  39. else
  40. return;
  41.  
  42. File.Copy(Application.StartupPath + "/Stub.exe", sPath, true);
  43. byte[] myFile = File.ReadAllBytes("C:\\notepad.exe");
  44.  
  45. File.SetAttributes(sPath, FileAttributes.Normal);
  46.  
  47. string mySplit = "<@@ENCEXE@@>";
  48. string myEndSplit = "</@@ENCEXE@@/>";
  49.  
  50. string sUrl = mySplit + byteString(myFile) + myEndSplit;
  51.  
  52. FileStream myStream = new FileStream(sPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
  53. BinaryWriter myWriter = new BinaryWriter(myStream);
  54.  
  55. myStream.Position = myStream.Length + 1;
  56.  
  57. myWriter.Write(sUrl);
  58.  
  59. myStream.Close();
  60. myStream.Dispose();
  61.  
  62. myWriter.Close();
  63.  
  64. MessageBox.Show("Built.");
  65. }
  66. catch (Exception err)
  67. {
  68. MessageBox.Show(err.Message);
  69. }
  70. }
  71. }
  72. }

Stub to house the bytes.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using System.IO;
  6. using System.Net;
  7. using System.Diagnostics;
  8. using System.Windows.Forms;
  9.  
  10. namespace Polyamorous
  11. {
  12. class Program
  13. {
  14. static string GetBetween(string myFile, string strStart, string strEnd)
  15. {
  16. int iPos, iEnd, lenStart = strStart.Length, startPos = 0;
  17.  
  18. iPos = myFile.IndexOf(strStart, startPos);
  19. iEnd = myFile.IndexOf(strEnd, iPos + lenStart);
  20.  
  21. if(iPos != -1 && iEnd != -1)
  22. {
  23. return myFile.Substring(iPos + lenStart, iEnd - (iPos + lenStart));
  24. }
  25.  
  26. return "error";
  27. }
  28.  
  29. static byte[] stringByte(string myString)
  30. {
  31. ASCIIEncoding encoding = new ASCIIEncoding();
  32. return encoding.GetBytes(myString);
  33. }
  34.  
  35. static string byteString(byte[] myBytes)
  36. {
  37. ASCIIEncoding enc = new ASCIIEncoding();
  38. return enc.GetString(myBytes);
  39. }
  40.  
  41. static void Main()
  42. {
  43. try
  44. {
  45. byte[] myFile = File.ReadAllBytes(Application.ExecutablePath);
  46.  
  47. string sData = GetBetween(byteString(myFile), "<@@ENCEXE@@>", "</@@ENCEXE@@/>");
  48.  
  49. File.WriteAllBytes("wtf.exe", stringByte(sData));
  50. }
  51. catch
  52. {
  53. Environment.Exit(0);
  54. }
  55. }
  56. }
  57. }

Hope you guys have some insight into my problem
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 83
Reputation: nmaillet is an unknown quantity at this point 
Solved Threads: 18
nmaillet nmaillet is offline Offline
Junior Poster in Training

Re: Reading/Writing Bytes

 
0
  #2
Jul 24th, 2009
Well, the file shown in the image says it uses UTF-8 encoding; try using the UTF8Encoding class instead of the ASCIIEncoding class.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 99
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Re: Reading/Writing Bytes

 
0
  #3
Jul 24th, 2009
Originally Posted by nmaillet View Post
Well, the file shown in the image says it uses UTF-8 encoding; try using the UTF8Encoding class instead of the ASCIIEncoding class.
Same result.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,200
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Reading/Writing Bytes

 
0
  #4
Jul 24th, 2009
Do you have to do this at runtime? You could embed notepad.exe in your project. This seems like a bad idea. Also I don't believe you can just append data wherever you want in an executable so are you sure this concept works? I know you can declare a static length struct() in a C/C++ application and read/write to that struct freely as long as you stay <= the structs size but I don't know that this is possible with .NET assemblies.

Another approach instead of bringing the data back in a byte[] array you could hook the StreamReader up directly the StreamWrite and write the data but this would assume you already had an empty slot for the data to do.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 99
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Re: Reading/Writing Bytes

 
0
  #5
Jul 24th, 2009
Originally Posted by sknake View Post
Do you have to do this at runtime? You could embed notepad.exe in your project. This seems like a bad idea. Also I don't believe you can just append data wherever you want in an executable so are you sure this concept works? I know you can declare a static length struct() in a C/C++ application and read/write to that struct freely as long as you stay <= the structs size but I don't know that this is possible with .NET assemblies.

Another approach instead of bringing the data back in a byte[] array you could hook the StreamReader up directly the StreamWrite and write the data but this would assume you already had an empty slot for the data to do.
Notepad must be embedded into the secondary exe. I can write strings to the EOF data and have the secondary exe read them into a messagebox so i dont see why i can't do it with bytes.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 99
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Re: Reading/Writing Bytes

 
0
  #6
Jul 25th, 2009
No one has any clue?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Reading/Writing Bytes

 
0
  #7
Jul 25th, 2009
Yes I have a clue. I smell some Mexican, Pig whatever flu around here
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 99
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Re: Reading/Writing Bytes

 
-1
  #8
Jul 25th, 2009
Originally Posted by ddanbe View Post
Yes I have a clue. I smell some Mexican, Pig whatever flu around here
Post reported, please don't spam my topics.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,200
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Reading/Writing Bytes

 
0
  #9
Jul 25th, 2009
Originally Posted by FTProtocol View Post
Post reported, please don't spam my topics.
Get ready danny... the hammer of daniweb is being brought upon you!
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 99
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Re: Reading/Writing Bytes

 
0
  #10
Jul 25th, 2009
Originally Posted by sknake View Post
Get ready danny... the hammer of daniweb is being brought upon you!
Same for you, stop spamming my topics.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC