How To Create a Builder in Visual Basic .NET
Submitted by Yorkiebar on Saturday, July 26, 2014 - 09:24.
Introduction:
Welcome to my tutorial on how to create a builder application in Visual Basic (VB.NET).
What is a Builder?
A builder application is an application which allows a user to select certain settings before generating a sub application (named a 'stub') which is uniquely created to the users settings entered within the builder.
Theory:
So for the theory of a builder application, we want to perform the following steps in order;
Gain original source code of the original exe application.
Get user settings through a CLI/GUI.
Write the new application through file streaming output.
GUI:
For our GUI I will be using two example controls;
Textbox, textbox1
Checkbox, checkbox1
Original .exe:
So from the steps above, we first want to get the original .exe source code, to do this, we are first going to allow the user to select a location through a new OpenFileDialog pop up box...
This gets saved in to a new string variable named 'oPath', standing for 'originalPath'.
Next we read the .exe's bytes in to a single dimensional byte array, named 'oBytes'...
Settings:
Now that we have the original source code, we next want to get the user settings for new applicaiton to be written, these should be in either byte arrays, or strings - we can convert freely later if needed.
I am just going to create one string and one byte array containing the information from the users input via the textbox, and checkbox controls...
File Creation:
Before we can create our new file, we want to get a savePath from the user, we'll do this through a SaveFileDialog box...
To create our new file, we simply write all the information we have now collected, as bytes through the File classes 'WriteAllBytes' function. To make this easy for ourselves, we are going to write the first setting, followed by a string splitter/delimiter - we'll use "@sss@", followed by the next settings, etc, up until we are only left with our original file's bytes...
We have to convert the byte arrays to strings, since we are converting the entire thing to a byte array - getBytes.
Why Use Splitters?
We've used splitters in order to separate our settings because when we read the bytes information in my next tutorial, we are going to use these string splitters in order to group our information so we know exactly which strings are which settings.
Finished!
- Dim oPath As String = String.Empty
- Using fo As New OpenFileDialog
- fo.ShowDialog()
- If (Not fo.FileName = Nothing) Then oPath = fo.FileName
- End Using
- Dim oBytes As Byte() = File.ReadAllBytes(oPath)
- Dim inputText As String = TextBox1.Text
- Dim inputBytes As Byte() = Encoding.Default.GetBytes(CheckBox1.Checked) 'True or False, in Bytes'
- Dim sPath As String = String.Empty
- Using fs As New SaveFileDialog
- fs.ShowDialog()
- If (Not fs.FileName = Nothing) Then sPath = fs.FileName
- End Using
- File.WriteAllBytes(sPath, Encoding.Default.GetBytes(inputText & "@sss@" & Encoding.Default.GetString(inputText) & "@sss@" & Encoding.Default.GetString(oBytes))
Comments
Add new comment
- Add new comment
- 218 views