Click here to Skip to main content
Click here to Skip to main content

Transferring Data to the New EXE

, 13 Aug 2009
Rate this:
Please Sign up or sign in to vote.
Have you ever wanted to change variables without rebuilding?

Introduction

Changing one or more variables in your application without even rebuilding might sometimes be a lifesaver for developers and users. Let's assume that you coded an application which uploads files to a server but you want to enable your users to write their server info on their own. You probably can do this by adding a registry key or an application setting. But what if the user wants this application to be portable, to be used on other PCs or not to save anything to the PC. With this code, I am going to explain it in this article and you will be able to do all these. 

In order to use this code, you only need to include the "TransferData.cs" to your project and create an instance of it. After creating an instance of the class, it is actually straight forward thanks to IntelliSense.

Anyway, I included a demonstration Editor (which makes changes on the stub) and the output stub. Output stub is basically your application. 

Background 

I needed a solution like this to use in my project and I couldn't find a satisfactory solution, so I coded it myself and I thought someone might need this code someday. So, here it is.

Using the Code

How It Works?

  • Editor reads the output stub from the resource and appends the separator and then the data you entered. PE files aren't affected when you append data at the end of the file, therefore your program will work without a problem. If you open the output file and the stub file on hex editor and compare them, you will see something like this. If you want, you can encrypt the string before you write it to the file.

  • At runtime, your output reads the data from end of file and processes it. This way, you can change the variables in your application without the need of rebuilding it or make your users be able to customize your application. There are infinite fields of use.

Editor Side (Writing the Data) 

The editor writes the variables (of course as one string) to the end of the file so that the application will be able to read them at runtime. As most of you know, you can append data to the end of an PE file and the PE file will work without a problem. In order to write data to the output, you should first create an instance of the class with:

TransferData.TransferData td = new TransferData.TransferData();

and then write the data with:

td.Write(Resource.Output, "output-new.exe", strToWrite, strSeperator); 

Resource.Output is your output stub which you included into your project, the rest is self explanatory. 

Application Side (Reading the Data)

Your application reads the variables from itself at runtime. In order to get the transferred data, you need to create an instance of the class with:

TransferData.TransferData td = new TransferData.TransferData();

and then read the data with:

td.Read(Application.ExecutablePath, strSeperator);

Read method returns the data as string so that you can work on it easily. If the application is not able to read the data correctly, it doesn't throws an exception. It just returns an "ERROR OCCURED" string. An important point is that the separator string (strSeperator) should be the same as the separator string you used in the editor.

Points of Interest

Here are some things that you should be careful about when using this code:

  • Only use ASCII characters in your data.
  • Your separator and data should not exceed 200 characters. You can actually change this limit by changing the buffer length on Read() method. Its length should be LIMIT+20.
  • Your data should never contain the separator in itself.

History

  • 14th August, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

SimpleData

Turkey Turkey
No Biography provided

Comments and Discussions

 
GeneralGood share! Pinmemberalrsds19-Sep-09 20:52 
GeneralA few things need to be discussed... Pinmembersupercat914-Aug-09 5:13 
GeneralGreat!May be we can use it do other thing Pinmemberguaike14-Aug-09 3:44 
GeneralRe: Great!May be we can use it do other thing PinmemberSimpleData14-Aug-09 3:45 
GeneralMy vote of 1 PinmemberMichael E. Jones13-Aug-09 23:23 
GeneralRe: My vote of 1 PinmemberJefis13-Aug-09 23:38 
GeneralRe: My vote of 1 PinmemberSimpleData13-Aug-09 23:41 
GeneralRe: My vote of 1 PinmemberMichael E. Jones14-Aug-09 0:16 
GeneralRe: My vote of 1 PinmemberSimpleData14-Aug-09 0:18 
GeneralRe: My vote of 1 PinmemberMichael E. Jones14-Aug-09 1:04 
GeneralRe: My vote of 1 PinmemberJohnno7417-Aug-09 16:10 
GeneralRe: My vote of 1 PinmemberTom Spink18-Aug-09 10:34 
AnswerMy vote + PinmemberMember 28788514-Sep-09 20:39 
GeneralRe: My vote + PinmemberTom Spink4-Sep-09 23:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 14 Aug 2009
Article Copyright 2009 by SimpleData
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid