How To Create a Builder in Visual Basic .NET

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...
  1. Dim oPath As String = String.Empty
  2. Using fo As New OpenFileDialog
  3. fo.ShowDialog()
  4. If (Not fo.FileName = Nothing) Then oPath = fo.FileName
  5. End Using
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'...
  1. Dim oBytes As Byte() = File.ReadAllBytes(oPath)
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...
  1. Dim inputText As String = TextBox1.Text
  2. Dim inputBytes As Byte() = Encoding.Default.GetBytes(CheckBox1.Checked) 'True or False, in Bytes'
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...
  1. Dim sPath As String = String.Empty
  2. Using fs As New SaveFileDialog
  3. fs.ShowDialog()
  4. If (Not fs.FileName = Nothing) Then sPath = fs.FileName
  5. End Using
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...
  1. File.WriteAllBytes(sPath, Encoding.Default.GetBytes(inputText & "@sss@" & Encoding.Default.GetString(inputText) & "@sss@" & Encoding.Default.GetString(oBytes))
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!

Comments

Add new comment