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!")
       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.

5 thoughts on “Execute system command within VB Console App

  1. Awesome! I was looking for something like this, wanted to build my own cmd like program while retaining all the abilities of the cmd app, this worked like a charm. Added a pre-parser to the input, if it didnt match any local commands, sent it off the sys() function. also can disable certain command-prompt commands this way too.

  2. Really awesome.
    But i am having an error with “powershell $host.version.major” command
    Will you please suggest me the code for running this command i.e. when i ll click on cmd.exe it should show me the system powershell version.

  3. Pingback: How To Fix Command Execute In Vb Net Errors - Windows Vista, Windows 7 & 8

  4. Pingback: Vb 2010 Execute Program

  5. Hello! I just wanted to tell you about the error I’m getting.

    I’m using:
    Microsoft Visual Studio 2010
    Version 10.0.30319.1 RTMRel
    Microsoft .NET Framework
    Version 4.0.30319 RTMRel

    Here’s the error I get:

    PInvokeStackImbalance was detected
    A call to PInvoke function ‘ConsoleApplication2!ConsoleApplication2.Module1::sys’ has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: