简体   繁体   English

用于C#使用的非托管C ++包装器

[英]Unmanaged C++ wrapper for C# usage

SOLVED: Thanks to Casey Price for their answer. 已解决:感谢Casey Price的回答。 I then ran into 2 other errors: BadImageFormatException and FileNotFoundException, the former was solved by matching the platform target (x64 or x86) for each project and the latter was solved by setting the output directory of the C# project to the directory containing the dll file. 然后我遇到了另外两个错误:BadImageFormatException和FileNotFoundException,前者通过匹配每个项目的平台目标(x64或x86)来解决,后者通过将C#项目的输出目录设置为包含dll文件的目录来解决。 。

I'm working on a game 'engine' which currently has a working graphics subsystem that draws/textures movable models. 我正在开发一个游戏“引擎”,该引擎目前具有可工作的图形子系统,可绘制/构造可移动模型。 I'm trying to write a C++/CLR wrapper so I can use the engine in a C# program (as a designer tool). 我正在尝试编写C ++ / CLR包装器,以便可以在C#程序(作为设计器工具)中使用引擎。

My wrapper project is a C++/CLR class library and contains the following 2 files (as well as resource.h/cpp and Stdafx.h/cpp) 我的包装器项目是C ++ / CLR类库,包含以下2个文件(以及resource.h / cpp和Stdafx.h / cpp)

// pEngineWrapper.h

#pragma once
#define null NULL

#include "..\pEngine\pEntry.h"

using namespace System;

namespace pEngineWrapper
{
    public ref class EngineWrapper
    {
        public:
        EngineWrapper();
        ~EngineWrapper();
        bool Initialise();

    private:
        pEntry* engine;
    };
}

and the .cpp file 和.cpp文件

// This is the main DLL file.

#include "stdafx.h"

#include "pEngineWrapper.h"

pEngineWrapper::EngineWrapper::EngineWrapper()
{
    engine = null;
}

pEngineWrapper::EngineWrapper::~EngineWrapper()
{
    delete engine;
    engine = null;
}

bool pEngineWrapper::EngineWrapper::Initialise()
{
    bool result;

    engine = new pEntry;

    result = engine->Initialise();
    if( result == false )
    {
        return false;
    }

    return true;
}

When I go to build this project however I get 14 errors: LNK2028, LNK2019, and LNK2001 which points to some classes within the engine. 但是,当我去构建此项目时,我遇到14个错误:LNK2028,LNK2019和LNK2001,它们指向引擎中的某些类。 I have included the errors in the file below. 我已将错误包含在下面的文件中。

https://www.dropbox.com/s/ewhaas8d1te7bh3/error.txt?dl=0 https://www.dropbox.com/s/ewhaas8d1te7bh3/error.txt?dl=0

I also get a lot of warnings regarding XMFLOAT/XMMATRIX which you may notice. 您也会注意到,我也收到许多有关XMFLOAT / XMMATRIX的警告。

In all of the engine classes I use the dllexport attribute 在所有引擎类中,我都使用dllexport属性

class __declspec(dllexport) pEntry

I feel like I'm missing the point and doing it all wrong seeing all of these errors but I haven't found any documents telling me anything considerably different than what I'm doing here 我觉得我错过了要点,看到所有这些错误都做错了,但是我没有找到任何文档可以告诉我任何与我在这里所做的事情大不相同的事情

you have to add a reference to the static .lib files for the game engine you are using as well as it's dll's or load the dll's manually 您必须为正在使用的游戏引擎及其dll添加对静态.lib文件的引用,或手动加载dll

to add the references to the .lib files right click your project->Properties->Linker->input add the lib file to the additional dependencies 要添加对.lib文件的引用,请右键单击您的项目->属性->链接器->输入,将lib文件添加到其他依赖项中

See also: DLL References in Visual C++ 另请参阅: Visual C ++中的DLL引用

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

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