简体   繁体   English

C++/CLI 包装器只能在 x86 机器上正常工作,我需要 x64 机器

[英]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.我有 x64 本机 C++ 库,我必须将其传递给 C# 项目。 I built C++/CLI wrapper, based on this tutorial, and everything was ok.我根据本教程构建了 C++/CLI 包装器,一切正常。 But, the project compiles only on x86 architecture.但是,该项目仅在 x86 架构上编译。 When I tried adding that native C++ library to project, I received runetime error.当我尝试将本机 C++ 库添加到项目时,我收到了 runetime 错误。 Project doesn't work on x64 architecture because wrapper for some reasons requires x86.项目不适用于 x64 架构,因为包装器出于某些原因需要 x86。 And, on the other hand, it doesn't work on x86 because that library requires x64.而且,另一方面,它在 x86 上不起作用,因为该库需要 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.总的来说,我对 C++/CLI、包装器和 C# 的经验很少,而且我不知道如何解决这个问题。 When tring to compile Solution, I receive runetime error尝试编译解决方案时,我收到 runetime 错误

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 ...这是一个使用此包装器的 C++/CLI 类

//**********************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.我无法修改该原生 C++ 库,因此它必须适用于 x64 架构。 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.您能否向我解释一下,为什么包装器不想在 x64 上编译,但在 x86 上可以完美运行,有没有办法解决这个问题。 Perfect answer would provide an example of C++/CLI Wrapper that is working on x64 architecture.完美的答案将提供一个在 x64 架构上工作的 C++/CLI Wrapper 示例。 Thanks in advance提前致谢

EDIT, oh, and I forget to add properties of my project so.编辑,哦,我忘了添加我的项目的属性。 OS is Win10 (x64);操作系统为 Win10 (x64); .NET target framework 4.5.1; .NET 目标框架 4.5.1; Core project(the lowest layer project, not presented here) is built as static .lib, and Wrapper is a dynamic .dll.核心项目(最底层的项目,这里不做介绍)构建为静态的.lib,Wrapper 是动态的.dll。 VisualStudio 2017 v15.9.14 VisualStudio 2017 v15.9.14

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.最近我在其他选项中遇到了野生 X86 标志,这给了我类似的错误。

In advanced, check Target machine .在高级中,检查Target machine

Try to enable verbose output for linker and compiler, and check for any occurrence of x86.尝试为链接器和编译器启用详细输出,并检查是否出现任何 x86。

If all of that is ruled out, make sure that your lib was really compiled and is valid, eg.如果所有这些都被排除,请确保您的库确实已编译并且有效,例如。 via dependency walker.通过依赖步行者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM