Windows and Linux can work together. How to execute a Linux command from Visual Basic?
Hi everyone,
In this post, I’d like to show you how easy it is to connect your Windows software to linux commands and software.
As you may know, Windows now includes the Windows Subsystem for Linux (WSL), which allows you to run a bash terminal and execute linux utilites as well as install software from the repository. Now, this is not virtualized. Rather, it uses a compatibility layer which allows it to execute linux tools on Windows.
This is not installed by default but can be installed by going into Control Panel, click on Turn Windows features on or off:
You then select and install WLS:
It may ask for a restart, and once it’s done, you simply launch bash.exe:
Which will present you with the terminal:
Perfect
But now, let’s say you want to automate something, and you are an expert using Visual Basic.NET or C#, and there’s this tool or script that runs on Linux that you want to execute. Can it be done? Sure it can!
From the CMD (Command Prompt), you can also launch a linux tool by simply invoking bash -c the tool to use. For example, if I’d like to launch python3, I would just use bash -c python3:
When I hit Enter, I’ll go into Python running on a bash terminal but it’s redirected into CMD. This basically allows us to interact with Linux from the Windows Command Prompt:
Interesting, right?
Then, how do you launch a linux script, tool, software, etc, from a VB.NET app?
EASY! Basically, we will use ProcessStartInfo, launch bash.exe and pass the -c parameter followed by what we want to run.
Here, let’s make a mini tutorial based on my script to get my Steem Followers:
This is the Python script I made that interacts with the Piston lib to get my follower count:
To use it, I have to launch the script along with the username of the account I’d like to know the followers. That’s what I’ll show in this mini tutorial.
We will be using Microsoft’s Visual Studio 2017.
First: Let’s create a new project in Visual Studio. This project will be a Windows Forms App in the Visual Basic language. I’ve named it SteemGetFollowers (This project will be uploaded into GitHub. See the end of this post for links):
Once we create the project, we will see an empty form:
From the Toolbox, we will add a TextBox and a Button. The TextBox is for us to enter our Steem username, and the button will be to call the script and show our follower number in a MessageBox or MsgBox (Both do the same):
Let’s make it look like this:
We’ll change the Button1 name to Get Follower Count!. For this, click the button, and change the Text property:
We can also change the Form1 name to something better. We’ll do the steps like the button, but instead clicking on the Form:
Now, we have our form complete.
Next step?
We have to make sure our software compiles as x64. For this, we’ll select where it says Any CPU at the right of Debug and we’ll click on Configuration Manager:
Here, we’ll select <New…>:
Make sure it says x64 and press OK:
Now, the Active Platform will change to x64. We can now Close the window:
If you haven’t done so, save the project by clicking on the Floppy icon:
Once done, we’ll go to the project folder, which, if saved on the default path, should be in Documents\Visual Studio 2017\Projects\SteemGetFollowers\SteemGetFollowers\bin\x64\Debug. There, we will copy the script file, because when we build the software, it will find the script to launch in this folder:
We have configure our project now! Let’s write code!
Now, let’s write the code. Double click on the Button and we should be directed to a pre-made code:
Inside the Private Sub that it made, we will write a code to launch bash along with python and the script. For this, we will use ProcessStartInfo:
Now, we simply show a simple message with the follower number using MsgBox:
So, our code should look like this:
We’re done!
Now, we will press the Start button to test our software:
It should compile pretty fast, and once the software lauches, we will write our Steem Username in the TextBox and press the Get Follower Count! button:
The result will be:
And that’s it!
We have successfully written a software that interacts with the Linux bash terminal! This way, we can build awesome Windows software that interacts with our Python scripts inside Linux.
Note that when we compile the software, we will see the executables in the folder where we copied the Python script:
The Python script file should be in the same folder as the executable for it to work. Also, the software must be compiled as 64bit or it will not find the bash executable.
GitHub Links:
You can get this project and check it out for yourself by going to the following repo:
https://github.com/moisespr123/SteemGetFollowers
Here, you can find the Python script:
https://github.com/moisespr123/SteemGetFollowers/blob/master/SteemGetFollowers/getFollowersCount.py
Hope you enjoyed this tutorial!!
Shall you have any questions, feel free to leave me a comment!