943,987 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1347
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 24th, 2009
0

Reading/Writing Bytes

Expand Post »
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 -
C# Syntax (Toggle Plain Text)
  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.

C# Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Jul 24th, 2009
0

Re: Reading/Writing Bytes

Well, the file shown in the image says it uses UTF-8 encoding; try using the UTF8Encoding class instead of the ASCIIEncoding class.
Reputation Points: 69
Solved Threads: 48
Posting Whiz in Training
nmaillet is offline Offline
203 posts
since Aug 2008
Jul 24th, 2009
0

Re: Reading/Writing Bytes

Click to Expand / Collapse  Quote originally posted by nmaillet ...
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.
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Jul 24th, 2009
0

Re: Reading/Writing Bytes

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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 24th, 2009
0

Re: Reading/Writing Bytes

Click to Expand / Collapse  Quote originally posted by sknake ...
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.
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Jul 25th, 2009
0

Re: Reading/Writing Bytes

No one has any clue?
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Jul 25th, 2009
0

Re: Reading/Writing Bytes

Yes I have a clue. I smell some Mexican, Pig whatever flu around here
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Jul 25th, 2009
-1

Re: Reading/Writing Bytes

Click to Expand / Collapse  Quote originally posted by ddanbe ...
Yes I have a clue. I smell some Mexican, Pig whatever flu around here
Post reported, please don't spam my topics.
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Jul 25th, 2009
0

Re: Reading/Writing Bytes

Click to Expand / Collapse  Quote originally posted by FTProtocol ...
Post reported, please don't spam my topics.
Get ready danny... the hammer of daniweb is being brought upon you!
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 25th, 2009
-1

Re: Reading/Writing Bytes

Click to Expand / Collapse  Quote originally posted by sknake ...
Get ready danny... the hammer of daniweb is being brought upon you!
Same for you, stop spamming my topics.
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: loading multiple pictureBox in panel
Next Thread in C# Forum Timeline: Descending Order





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC