0

I try to build an application using a builder and a stub but i fail

My builder code :

File.Copy(AppDomain.CurrentDomain.BaseDirectory + @"\Camstub.exe", filepath);
            string split = "|";
            string info = split + CName.Text + split + Link.Text + split;

            FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
            BinaryWriter bw = new BinaryWriter(fs);
            fs.Position = fs.Length + 1;
            bw.Write(info);
            bw.Close();
            MessageBox.Show(info);

My stub code :

public MainWindow()
    {
        InitializeComponent();

        StreamReader sr = new StreamReader(System.Windows.Forms.Application.ExecutablePath);
        BinaryReader br = new BinaryReader(sr.BaseStream);
        byte[] fileData = br.ReadBytes(Convert.ToInt32(sr.BaseStream.Length));
        br.Close();
        sr.Close();

        ASCIIEncoding Enc = new ASCIIEncoding();
        string split = "|";
        string Message = Enc.GetString(fileData);
        MessageBox.Show(Message);

The messagebox in the builder show me:

http://embed.gyazo.com/d8f55ce5ea608b3861e64862242b0012.png

The application is sucessfully build but the messagebox when i execute it show me:

enter image description here

So, I expect the same messagebox in both.

Any idea ?

Thanks in advance ;)

  • Your stub reads the first xxx bytes (xxx being equal to the file length) from the executable and puts them into *info* (note, that a Windows executable or DLL file always starts with the magic bytes "MZ"). In your builder, the variable *info* is composed from different data (*CName* and *Link*), so no surprise that you see differences between the stub and the builder messageboxes. There is no reason to expect the two messageboxes to show the same data... –  Jun 20 '15 at 16:36
  • Are you perhaps intending to read the Cname/Link data you appended at the end of the file? If so, is this Cname/Link data of constant length, i.e. a constant number of bytes; or can its length be variable? –  Jun 20 '15 at 16:41
  • Ty, hum so i have to read in the end of the file but the lenght of the CName/Link can be variable. – Nicolas Vieira Jun 20 '15 at 16:45

1 Answers1

0

Based on the information given in the comments, you want to read the |CName|Link| data from the end of the file. The |CName|Link| data can be of variable length.

To deal with variable length data you somehow need to indicate the byte length of the data. In the given scenario here, a simple and suitable solution would be to store the byte length as a 2-byte or 4-byte number at the end of the file after the |CName|Link| data. A 2-byte number (i.e., ushort or UInt16) allows to specify a length of up to 65536 and should likely be sufficient for your purpose. (If the data can be more than 64KB, then use 4 bytes to store the length.)

Thus, the builder should do the following:

  1. Compose |CName|Link| string data and store it in the info variable.
  2. Convert the string in info into a byte array with a chosen text encoding. (I would suggest using the UTF-8 text encoding.)
  3. Append this byte array to the executable file.
  4. Append the length of the byte array as a 2-byte number to the end of the executable file.


The stub should do something like this:

  1. Determine the file size of the executable file.
  2. Open the executable file for reading.
  3. Set the current file position to fileSize-2, so the length of the |CName|Link| can be read.
  4. Read the 2-byte number and store it into an ushort (or int) variable dataLength.
  5. Set the current file position to fileSize-2-dataLength.
  6. Read the bytes of the |CName|Link| data. The number of bytes to read is specified by dataLength.
  7. Process the |CName|Link| data bytes. For example, convert them into a string, using the same(!) text encoding that has been used by the builder.
  • Thanks for you help, i'm pretty sure your solution is good but i have found an other one, I add a string like "-START-" before "|CName|Link|" And i read all the stream and i keep information following "-START-" – Nicolas Vieira Jun 20 '15 at 17:22
  • Your stub program needs to read the whole file to find the "-start-" marker. A very inefficient way of doing things... With my approach only 2 + dataLength bytes are being read. But then again, i do not know your application scenario. Perhaps performance is irrelevant, and your approach is just fine for your purposes... :-) –  Jun 20 '15 at 17:25
  • But consider this: What if the executable file already contains "-START-" somewhere in its file data? Then your approach fails, whereas mine would still operate properly... Hint: This will happen, because the "-START-" string literal would be a part of the compiled C# code and therefore already be part of the compiled executable file. :p –  Jun 20 '15 at 17:29