Windows and Linux can work together. How to execute a Linux command from Visual Basic?

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:

WSL VB.NET Tutorial - Steemit Followers - 1

You then select and install WLS:

WSL VB.NET Tutorial - Steemit Followers - 2

It may ask for a restart, and once it’s done, you simply launch bash.exe:

WSL VB.NET Tutorial - Steemit Followers - 3

Which will present you with the terminal:

WSL VB.NET Tutorial - Steemit Followers - 4

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:

WSL VB.NET Tutorial - Steemit Followers - 5

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:

WSL VB.NET Tutorial - Steemit Followers - 6

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:

WSL VB.NET Tutorial - Steemit Followers - 7

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):

WSL VB.NET Tutorial - Steemit Followers - 8

Once we create the project, we will see an empty form:

WSL VB.NET Tutorial - Steemit Followers - 9

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):

WSL VB.NET Tutorial - Steemit Followers - 10

Let’s make it look like this:

WSL VB.NET Tutorial - Steemit Followers - 11

We’ll change the Button1 name to Get Follower Count!. For this, click the button, and change the Text property:

WSL VB.NET Tutorial - Steemit Followers - 12

We can also change the Form1 name to something better. We’ll do the steps like the button, but instead clicking on the Form:

WSL VB.NET Tutorial - Steemit Followers - 13

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:

WSL VB.NET Tutorial - Steemit Followers - 14

Here, we’ll select <New…>:

WSL VB.NET Tutorial - Steemit Followers - 15

Make sure it says x64 and press OK:

WSL VB.NET Tutorial - Steemit Followers - 16

Now, the Active Platform will change to x64. We can now Close the window:

WSL VB.NET Tutorial - Steemit Followers - 17

If you haven’t done so, save the project by clicking on the Floppy icon:

WSL VB.NET Tutorial - Steemit Followers - 18
WSL VB.NET Tutorial - Steemit Followers - 19

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:

WSL VB.NET Tutorial - Steemit Followers - 20

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:

WSL VB.NET Tutorial - Steemit Followers - 21

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:

WSL VB.NET Tutorial - Steemit Followers - 22

Now, we simply show a simple message with the follower number using MsgBox:

WSL VB.NET Tutorial - Steemit Followers - 22.5

So, our code should look like this:

WSL VB.NET Tutorial - Steemit Followers - 23

We’re done!

Now, we will press the Start button to test our software:

WSL VB.NET Tutorial - Steemit Followers - 24

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:

WSL VB.NET Tutorial - Steemit Followers - 25

The result will be:

WSL VB.NET Tutorial - Steemit Followers - 26

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:

WSL VB.NET Tutorial - Steemit Followers - 27

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!