How To Build a Stub in Visual Basic .NET
Submitted by Yorkiebar on Wednesday, August 6, 2014 - 12:59.
Introduction:
This tutorial is on how to create a stub in Visual Basic .NET.
Builder:
This is a follow on tutorial from my previous tutorial on how to create a builder in Visual Basic .NET which allows a user to input custom settings to their liking in to an application (the builder) which will then put those settings via the controls (textbox and checkbox in that example) in to the generated stub. This stub - this tutorial - will then take those settings and generate & run a tepmorary .exe application using the settings given from the builder.
Imports:
Before we can begin, we must first import the System.IO namespace which means the application will be able to read and write to/from files on our computers filesystem...
We also import System.Windows.Forms in order for us to be able to access the Application's own properties on runtime.
System.Encoding to convert between Bytes and Strings.
Form1 Load:
The first thing we want to do in our form load event is make the application get it's own directory, we will store this in a new string variable named 'exePath'...
Next, we want to use the System.IO namespace's class 'File' > function 'ReadAllBytes'. This function takes one parameter, which is the file path to the file we want to read the bytes of. In this case, we are wanting to get the current application's (stub's) bytes, split them by the file splitter (has to be the exact same one used in the builder applicaation), and then use each split as a setting (I'll explain this in a second).
Now that we have the bytes, we want to convert them to a string (so that we can use our file splitter string afterwards)...
-We can declare our file splitter string as a variable if we wanted to. This will make it easier for future development to understand exactly what the string means and what it does. This MUST be the exact same (including capitals) as the file splitter in the builder application.-
Extracting Settings:
Now that we have the information we need, we can extract the settings. Since we input the settings in a specific order from our builder (we put the textbox text followed by the checkbox state as a string (true or false)), they will be in the same order once they are extracted.
You should know by now that we put the settings in to the built .exe (stub) using the file splitter after/before each setting, followed by the stub's code. So, we can now use out splitter string to split our stub's string code (myString), like so...
We can now write down the exact settings using the exact indexing as the one used while putting the settings in to the stub via the builder...
Stub Code:
That's all we need for the settings part. Now we can simply write what the stub would normally do, and take in to account the settings. So for now, we are simply going to write the textbox string setting to our own custom textbox, and the same with the checkbox. So first add the two controls to our stub form...
And then set the information, like so...
Finished!
- Imports System.IO
- Imports System.Windows.Forms
- Imports System.Encoding
- Dim exePath As String = Application.ExecutablePath
- Dim bytes As Byte() = File.ReadAllBytes(exePath)
- Dim myString As String = Encoding.Default.GetString(bytes)
- Dim splitter As String = "@@spliter@@"
- Dim mySplits As String() = myString.Split(spitter)
- Dim textboxStr As String = mySplits(0)
- Dim checkboxStr As String = mySplits(1)
- Textbox, textbox1
- Checkbox, checkbox1
- TextBox1.Text = textboxStr
- If (checkboxStr = "True") Then
- CheckBox1.Checked = True
- Else : CheckBox2.Checked = False
- End If
Add new comment
- 294 views