Posts Tagged “LegacyWrapper”

    Today, I released version 2.1 of my nuget package LegacyWrapper. It comes with a major enhancement regarding the cross-architecture loading of DLLs: It is now possible to load 64bit DLLs from a 32bit process:

    using (var client = new WrapperClient(TestDllPath, TargetArchitecture.Amd64))
    {
        result = (int)client.Invoke<TestStdCallDelegate>("TestStdCall", new object[] { input });
    }
    

    As the second constructor parameter is optional and defaults to X86, the new release should be fully backwards compatible.

    When loading unmanaged / native libraries from managed .NET code, the normal way is to use the “Platform Invoke (P/Invoke)” mechanism.

    [DllImport("shell32.dll", CallingConvention = CallingConvention.Winapi)]
    static extern int DllGetVersion(ref DLLVERSIONINFO pdvi);
    

    The problem

    P/Invoke has an annoying limitation: You can not load a x86 (32bit) library into a 64bit process and vice versa. This is especially problematic when your application is compiled with the “AnyCPU”-flag - which lets the .NET runtime decide which architecture to use at runtime - and there is only one 32bit version of a specific dll. If recompiling the library against a different architecture is not an option, you have to find another solution.