Hiding command window when running VirtualBox Headless

I discovered a couple things today. First, VirtualBox 4 appears to be less resource intensive than Vmware Player for the same workload.

Second, while VirtualBox provides the ability to run in a headless manner (aka hidden, or in the background), the command line tool to do so must be left open (on windows at least)

Yeah… kinda defeats the purpose of being headless.

If you close the cmd window, it kills your VM.

The solution: well here, I’ll just attach it:
VBoxHeadlessSilent.exe
(Requires .NET Framework 4 Client Profile)

Just drop it in your c:\Program Files\Oracle\VirtualBox folder (or wherever you installed it to) and invoke it the same as you would VBoxHeadless.exe. Instead of staying open, it will print out the response from VBoxHeadless.exe and return, leaving it running in the background.

All this app does is create a process with a hidden window and passes through the parameters.

Update 9/13/2011: If you are having problems with this not working or otherwise erroring out, it might be because of spaces in the VM Name. Try renaming the VM without spaces and see if that fixes it.
(Thanks David!)

Here is the source code if you are curious.

using System;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace VBoxHeadlessSilent
{
    class Program
    {
        static void Main(string[] args)
        {
            string headlessPath = "";
            string argString = "";

            for (int i = 0; i < args.Length; i++)
            {
                argString += args[i] + " ";
            }

            headlessPath = Path.Combine(Environment.CurrentDirectory, "VBoxHeadless.exe");
            if (!File.Exists(headlessPath)) headlessPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Oracle", "VirtualBox", "VBoxHeadless.exe");
            if (!File.Exists(headlessPath)) headlessPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Oracle", "VirtualBox", "VBoxHeadless.exe");

            if (File.Exists(headlessPath))
            {
                Process p = new Process();
                p.StartInfo.FileName = headlessPath;
                p.StartInfo.Arguments = argString;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.StartInfo.RedirectStandardOutput = true;
                p.Start();
                Console.Write(p.StandardOutput.ReadToEnd());
                Environment.Exit(0);
            }
            else
            {
                Console.WriteLine("Could not find VBoxHeadless.exe!");
                Environment.Exit(1);
            }
        }
    }
}

7 thoughts on “Hiding command window when running VirtualBox Headless

  1. this doesn’t seem to be working for me. I’m running 4.0.8 on XP Professional. I’ve placed the file in the virtualbox program files directory and passed the parameters -s “vm name” (or uuid).

    When I use the vm name it displays the help output of vboxheadless.exe, then returning me to the prompt. When I use the UUID of the vm it shows the copyright and runs the VM but does not return to the prompt (acting exactly the same as invoking vboxheadless.exe directly).

  2. When I tried using this, it opened a window display the guest OS. I could not interact with the guest OS and the window didnt show up on the toolbar. I am running Windows Vista. Can someone help me?

  3. As far as I know, the application is not using any Windows 7 specific features…

    Do these virtual machine names contain spaces by any chance? Did you run as administrator (don’t think you should need to)?

    It is possible that vboxheadless is returning something funky on stdout – or maybe it is returning something on stderr…

    I suppose I could try not redirecting anything – if you want to try, you could use visual studio express to compile a modified version – I’l see if I can find some time…

  4. Awesome, just what I was looking for.

    I usually run a bunch of Linux servers on a Windows 7 host, and I’ve always started them with a custom shortcut:

    “C:\somepath\VBoxSDL.exe” -vm “vm name goes here”

    Awesome, ’cause it doesn’t auto-capture your mouse when running command-line only servers, but, just as VBoxHeadless.exe, this leaves an annoying command window that can’t be closed, or else it takes down all the servers with it.

    Thus, as a solution, I copy pasted the code you posted above in a C# project (set as windows application), replaced all “Headless” references with “SDL”, and BAM, annoying window is now gone.

    I named my variant VBoxSilentDL.exe, just because.

    I really like your solution to this problem, and I’ll probably us it for more than this alone. It’s small, to the point, easy to read and it works = awesomeness.

  5. Pingback: Запуск VirtualBox без окон | Блог Бафа

Leave a Reply

Your email address will not be published. Required fields are marked *