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 -

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Polyamory
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        static string byteString(byte[] myBytes)
        {
            ASCIIEncoding enc = new ASCIIEncoding();
            return enc.GetString(myBytes);
        }

        private void buildButton_Click(object sender, EventArgs e)
        {
            try
            {
                string sPath = "";

                SaveFileDialog saveFile = new SaveFileDialog();

                saveFile.FileName = "built.exe";
                saveFile.Filter = "Executable Files (*.exe)|*.exe";

                if (saveFile.ShowDialog() == DialogResult.OK)
                    sPath = saveFile.FileName;
                else
                    return;

                File.Copy(Application.StartupPath + "/Stub.exe", sPath, true);
                byte[] myFile = File.ReadAllBytes("C:\\notepad.exe");

                File.SetAttributes(sPath, FileAttributes.Normal);

                string mySplit = "<@@ENCEXE@@>";
                string myEndSplit = "</@@ENCEXE@@/>";

                string sUrl = mySplit + byteString(myFile) + myEndSplit;

                FileStream myStream = new FileStream(sPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
                BinaryWriter myWriter = new BinaryWriter(myStream);

                myStream.Position = myStream.Length + 1;

                myWriter.Write(sUrl);

                myStream.Close();
                myStream.Dispose();

                myWriter.Close();

                MessageBox.Show("Built.");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
    }
}

Stub to house the bytes.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Windows.Forms;

namespace Polyamorous
{
    class Program
    {
        static string GetBetween(string myFile, string strStart, string strEnd)
        {
            int iPos, iEnd, lenStart = strStart.Length, startPos = 0;

            iPos = myFile.IndexOf(strStart, startPos);
            iEnd = myFile.IndexOf(strEnd, iPos + lenStart);

            if(iPos != -1 && iEnd != -1)
            {
                return myFile.Substring(iPos + lenStart, iEnd - (iPos + lenStart));
            }

            return "error";
        }

        static byte[] stringByte(string myString)
        {
            ASCIIEncoding encoding = new ASCIIEncoding();
            return encoding.GetBytes(myString);
        }

        static string byteString(byte[] myBytes)
        {
            ASCIIEncoding enc = new ASCIIEncoding();
            return enc.GetString(myBytes);
        }

        static void Main()
        {
            try
            {
                byte[] myFile = File.ReadAllBytes(Application.ExecutablePath);

                string sData = GetBetween(byteString(myFile), "<@@ENCEXE@@>", "</@@ENCEXE@@/>");

                File.WriteAllBytes("wtf.exe", stringByte(sData));
            }
            catch
            {
                Environment.Exit(0);
            }
        }
    }
}

Hope you guys have some insight into my problem :)

Well, the file shown in the image says it uses UTF-8 encoding; try using the UTF8Encoding class instead of the ASCIIEncoding class.

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.

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.

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.

No one has any clue?

Yes I have a clue. I smell some Mexican, Pig whatever flu around here:( :'(

Yes I have a clue. I smell some Mexican, Pig whatever flu around here:( :'(

Post reported, please don't spam my topics.

commented: yes danny is a RELENTLESS spammer. One of the worst on daniweb -1

Post reported, please don't spam my topics.

Get ready danny... the hammer of daniweb is being brought upon you!

Get ready danny... the hammer of daniweb is being brought upon you!

Same for you, stop spamming my topics.

commented: You suck. +0

Same for you, stop spamming my topics.

I already gave you another approach for going about this topic and you ignored it. You may have tried it but did not post feedback. FYI this is considered "bumping" a thread at worst but hardly spamming. Drop the attitude and you may find people more willing to help. Just a thought.

Yes this is likely an encoding issue. Don't bring it back to a managed byte array without specifying the source encoding or better yet just stream one file to the other.

I already gave you another approach for going about this topic and you ignored it. You may have tried it but did not post feedback. FYI this is considered "bumping" a thread at worst but hardly spamming. Drop the attitude and you may find people more willing to help. Just a thought.

Yes this is likely an encoding issue. Don't bring it back to a managed byte array without specifying the source encoding or better yet just stream one file to the other.

your sarcasm and your friends spam is what annoyed me, but yes i have tried every encoding known to man and i still cannot write a fully working file.

I do not understand why.

Maybe that is the problem, it is binary data after all. Perhaps you should just leave it. Quit changing binary data to an ASCII encoded string and leave it as a byte array.

Maybe that is the problem, it is binary data after all. Perhaps you should just leave it. Quit changing binary data to an ASCII encoded string and leave it as a byte array.

I tried something like this.

Original
Split1
byteArray
Split2

But it never worked.

Upload your project

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.