Search code examples
c#c++c++-cli64-bitwrapper

C++/CLI wrapper is working properly only on x86 machine, I need x64 machine


I have x64 native C++ library that I have to pass to C# project. I built C++/CLI wrapper, based on this tutorial, and everything was ok. But, the project compiles only on x86 architecture. When I tried adding that native C++ library to project, I received runetime error. Project doesn't work on x64 architecture because wrapper for some reasons requires x86. And, on the other hand, it doesn't work on x86 because that library requires x64.

I have very little experience with C++/CLI, wrappers, and C# in general, and I don't have much idea how to go around this problem. When tring to compile Solution, I receive runetime error

System.BadImageFormatException: Could not load file or assembly 'Wrapper, Version=1.0.7178.20781, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was ma
de to load a program with an incorrect format..

Link to error documentation

Here is my Wrapper

using namespace System;
namespace CLI {
    template<class T>
    public ref class Wrapper
    {
    protected:
        T* m_Instance;
    public:
        Wrapper(T* instance)
            :m_Instance(instance)
        {
        }
        virtual ~Wrapper()
        {
            if (m_Instance != nullptr)
            {
                delete m_Instance;
            }
        }
        !Wrapper()
        {
            if (m_Instance != nullptr)
            {
                delete m_Instance;
            }
        }
        T* GetInstance()
        {
            return m_Instance;
        }
    };
}

...And here is a C++/CLI class that is using this wrapper

//**********************header file***********************

#include "Wrapper.h"
#include "../Core/Core.h"
using namespace System;
namespace CLI
{
    public ref class Model : public Wrapper<Core::Impl>
    {
    public:

        Model();
        bool test();
    };

//**********************Implementation******************************


#include "Model.h"

namespace CLI
{
    Model::Model()
        :Wrapper(new Core::Impl())
    {
        Console::WriteLine("Creating new Impl-wrapper object!!");
    }
    bool Model::test()
    {
        return m_Instance->test();
    }
}

Its pretty much exacly the same as from the tutorial that I used.

I can not modify that native C++ library, so it has to work on x64 architecture. Can you please explain to me, why wrapper doesn't want to compile on x64, but works perfectly on x86, and is there a way to go around this. Perfect answer would provide an example of C++/CLI Wrapper that is working on x64 architecture. Thanks in advance

EDIT, oh, and I forget to add properties of my project so. OS is Win10 (x64); .NET target framework 4.5.1; Core project(the lowest layer project, not presented here) is built as static .lib, and Wrapper is a dynamic .dll. VisualStudio 2017 v15.9.14


Solution

  • Double check your project setting, especially linker. Check Command line tab for linker. Recently I encountered wild X86 flag there in Additional options that gave me similar errors.

    In advanced, check Target machine.

    Try to enable verbose output for linker and compiler, and check for any occurrence of x86.

    If all of that is ruled out, make sure that your lib was really compiled and is valid, eg. via dependency walker.