Execute system command within VB Console App
In C++ there is this function that comes with called system. This function allows you to run cmd commands within your console application (same window) and then return control back to the app within the same instance of cmd.exe. But this function is not available in VB.NET, the closest is ‘shell’ which doesn’t seem to work well because the ‘shell’ function starts a new executable in a different window. So this is my solution: By looking at the disassembly of a c++ program that used the ‘system’ function I have determined that this function is a call to the ‘system’ export from msvcrt.dll with the command as a string argument. Now in VB.NET you can call exports from dll’s, so I did just that:
Module Program Declare Function sys Lib "msvcrt.dll" Alias "system" (ByVal command As String) As Integer Sub Main() sys("echo hello world!") sys("pause") sys("exit") End Sub End Module
The second statement in that code created a new function within the app called ‘sys’ which is treated as “system” when calling the function that is an export function of msvcrt.dll with ‘command’ As String as an argument. Now it is possible to run cmd within your vb.net console application. One drawback is that this is not the same as a batch file, after every command the cmd call exits and brings back control to the program within the same window. One solution is to write all the commands to a batch file then execute that batch file within one command on the cmd call. It’s up to you with what you’re going to do with this, I just wanted to share this. So that’s it.