VirtualKD 2.8 with VirtualBox 4.2

If you try to install VirtualKD 2.8 on VirtualBox 4.2.x, you get an error similar to this:

Unable to cast COM object of type ‘VirtualBox.VirtualBoxClass’ to interface type ‘VirtualBox.IVirtualBox’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{…}’ failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

It turns out it’s a pretty easy thing to fix. The Interop.VirtualBox.dll distributed with VirtualKD is built for the 4.1 VirtualBox interface, so you just have to rebuild it for your version of VirtualBox. Create a C# project, and paste the following code into the Program.cs file to build a new Interop.VirtualBox.dll for 4.2.x.

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;

namespace ConvertTypeLibToAssembly
{
    public class App
    {
        private enum RegKind
        {
            RegKind_Default = 0,
            RegKind_Register = 1,
            RegKind_None = 2
        }
    
        [ DllImport( "oleaut32.dll", CharSet = CharSet.Unicode, PreserveSig = false )]
        private static extern void LoadTypeLibEx( String strTypeLibName, RegKind regKind, 
            [ MarshalAs( UnmanagedType.Interface )] out Object typeLib );
        
        public static void Main()
        {
            Object typeLib;
            LoadTypeLibEx( @"C:\Program Files\Oracle\VirtualBox\VBoxC.dll", RegKind.RegKind_None, out typeLib ); 
            
            if( typeLib == null )
            {
                Console.WriteLine( "LoadTypeLibEx failed." );
                return;
            }
                
            TypeLibConverter converter = new TypeLibConverter();
            ConversionEventHandler eventHandler = new ConversionEventHandler();
            //AssemblyBuilder asm = converter.ConvertTypeLibToAssembly( typeLib, "Interop.Virtualbox.dll", 0, eventHandler, null, null, null, null );
            AssemblyBuilder asm = converter.ConvertTypeLibToAssembly(typeLib, "Interop.VirtualBox.dll", TypeLibImporterFlags.SafeArrayAsSystemArray, eventHandler, null, null, "VirtualBox", null); //using assembly name "VirtualBox" and SafeArrayAsSystemArray to be compatible to VisualStudio-Generated Interop-Assembly

            asm.Save("Interop.Virtualbox.dll");
        }
    }

    public class ConversionEventHandler : ITypeLibImporterNotifySink
    {
        public void ReportEvent( ImporterEventKind eventKind, int eventCode, string eventMsg )
        {
            // handle warning event here...
        }
        
        public Assembly ResolveRef( object typeLib )
        {
            // resolve reference here and return a correct assembly...
            return null; 
        }    
    }
}

Some of this code was grabbed from MSDN, and a single line was grabbed from VirtualBoxService. As the latter is GPL, this code may be GPL. I don’t claim any rights to it.

9 Responses to “VirtualKD 2.8 with VirtualBox 4.2”

  1. Bevan Collins says:

    Thanks for that

  2. Matt says:

    For those of us who don’t have a C# dev environment handy, and don’t want to go through the pain of trying to get Visual Studio downloaded, installed, deconflicted with older versions, etc., can you post the compiled output? Thanks!

  3. Julian de Navascues says:

    Thank you, it works great!

    For me failed with .NET Framework 4.0 and worked with 2.0.

  4. Darren says:

    For me, typelib seems to be filled with nothing but null right after LoadTypeLibEx, and as a result ConvertTypeToLibAssembly just hangs in an infinite loop.

    This seems to only happen with .NET Framework 2.0, but not 4.0.

    Interesting bug, tried to debug it and find the source of the issue with no success. Guess I’ll just downgrade my VirtualBox!

  5. Norbert says:

    Thanks guys. I finally got it working for VirtualBox 4.3 that way as well.

    The essential point is to switch the project to .NET Framework 2.0 (like Julian said). The strange thing is, that with 4.0 it takes a split second to create Interop.Virtualbox.dll. But with 2.0 it took a really long time (5 Minutes or more).

    So, like Daren, I thought it got stuck in an infinite loop or something. But it does finish. You just have to give it some time.

  6. Norbert says:

    Ah, I am sorry. I spoke to soon. The Interop.Virtualbox.dll was created correctly and the automatic integration into VirtualBox worked. But starting a VM fails with following error:

    “The version of the device registration structure is unknown to this VBox version. Either mixing incompatible versions or the structure isn’t correctly initialized. (VERR_PDM_UNKNOWN_DEVREG_VERSION).”

    I guess, VirtualKD-2.8 isn’t compatible with VBox 4.3.

  7. MB says:

    Regarding: “For those of us who don’t have a C# dev environment handy, and don’t want to go through the pain of trying to get Visual Studio downloaded, installed, deconflicted with older versions, etc., …”

    Just use the copy of the C# compiler that’s installed on your system by the .NET Framework:

    C:\Users\bamf>cd C:\Users\bamf\Documents\VirtualKD-2.8

    C:\Users\bamf\Documents\VirtualKD-2.8>\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe vbox42.cs
    Microsoft (R) Visual C# Compiler version 4.0.30319.34209
    for Microsoft (R) .NET Framework 4.5
    Copyright (C) Microsoft Corporation. All rights reserved.

    C:\Users\bamf\Documents\VirtualKD-2.8>

    For more details, see: http://baileysoriginalirishtech.blogspot.com/2015/01/this-one-weird-trick-visual-studio-team.html

  8. Dagnamit says:

    Successful build (Visual Studio 2013 Professional), new error when selecting “Integrate automatically” in VirtualKDSetup.exe.

    “Could not load file or assembly ‘Interop.VirtualBox, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. This assembly is built by a runtime newer than the current loaded runtime and cannot be loaded.”

  9. Nutter says:

    For vBox 5.0 and vKD 2.8 I tried building with both .Net 2 and .Net 4 and received Dagnamit’s error “Could not load file or assembly ‘Interop.VirtualBox, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. This assembly is built by a runtime newer than the current loaded runtime and cannot be loaded.”

Leave a Reply