gpt4 book ai didi

c# - 使用 CLR C++ 包装器从 C# 应用程序在 C++ 中加载 SDL 窗口不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 11:35:43 26 4
gpt4 key购买 nike

我的应用程序的目标是通过 C++ CLR 包装器从 C# 应用程序加载一个简单的 SDL 窗口。为了完成这项工作,我创建了 3 个项目:

  • 一个是加载 SDL 窗口的 C++ 库 (type = lib)。
  • 第二个是用 C++ CLR (type = dll) 编写的包装器。
  • 最后一个当然是C#基础应用(type = exe)。

C++ 库:

包括:

#ifndef __TARGET_DISPLAY_HPP__
# define __TARGET_DISPLAY_HPP__

#include <SDL/SDL.h>

class TargetDisplay
{
public:
TargetDisplay(void);
~TargetDisplay(void);

public:
void Init(void);
void Update(void);
void Render(void);
void Quit(void);
bool IsAlive(void);

private:
SDL_Window *fenetre;
SDL_GLContext contexteOpenGL;
SDL_Event evenements;
bool terminer;
};

#endif // !__TARGET_DISPLAY_HPP__

源代码:

#include "TargetDisplay.hpp"

//Initialization

TargetDisplay::TargetDisplay(void)
: terminer(false)
{

}

//Destruction

TargetDisplay::~TargetDisplay(void)
{

}

//Others

void TargetDisplay::Init(void)
{
SDL_Init(SDL_INIT_VIDEO);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

fenetre = SDL_CreateWindow("Test SDL 2.0", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, 320, 240, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);

contexteOpenGL = SDL_GL_CreateContext(fenetre);
}

void TargetDisplay::Update(void)
{
SDL_WaitEvent(&evenements);

if(evenements.window.event == SDL_WINDOWEVENT_CLOSE)
terminer = true;
}

void TargetDisplay::Render(void)
{

}

bool TargetDisplay::IsAlive(void)
{
return (true);
}

void TargetDisplay::Quit(void)
{
SDL_GL_DeleteContext(contexteOpenGL);
SDL_DestroyWindow(fenetre);
SDL_Quit();
}

到这里为止没有什么特别的:只是 SDL 窗口的基本初始化(仅供引用,编译为 .exe 应用程序的代码可以正确显示窗口)。

现在用 CLR C++ 编写的包装器:

包括:

#ifndef __WRAPPER_DISPLAY_HPP__
# define __WRAPPER_DISPLAY_HPP__

#include <iostream>
#include "../TargetCPP/TargetDisplay.hpp"

namespace Test
{
public ref class WrapperDisplay
{
public:
WrapperDisplay(void);
~WrapperDisplay(void);

public:
void Init(void);
void Update(void);
void Render(void);
bool IsAlive(void);
void Quit(void);

private:
TargetDisplay *m_pTarget;
};
}

#endif // !__WRAPPER_DISPLAY_HPP__

源代码:

#include "WrapperDisplay.hpp"

//Initialization

Test::WrapperDisplay::WrapperDisplay(void)
{
this->m_pTarget = new TargetDisplay();
}

//Destruction

Test::WrapperDisplay::~WrapperDisplay(void)
{

}

//Others

void Test::WrapperDisplay::Init(void)
{
this->m_pTarget->Init();
}

void Test::WrapperDisplay::Update(void)
{
this->m_pTarget->Update();
}

void Test::WrapperDisplay::Render(void)
{
this->m_pTarget->Render();
}

bool Test::WrapperDisplay::IsAlive(void)
{
return (this->m_pTarget->IsAlive());
}

void Test::WrapperDisplay::Quit(void)
{
this->m_pTarget->Quit();
}

如您所见,我已将我的 C++ 静态库链接到我的 CLR 项目中,并调用了所有方法(如果我将 CLR 项目编译为 .exe 应用程序并将 C++ 项目编译为 .lib 应用程序,SDL窗口也能正确显示)。

最后是 C# 主应用程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WrapperTest
{
class Program
{
static void Main(string[] args)
{
Test.WrapperDisplay wrapper = new Test.WrapperDisplay();

wrapper.Init();
/*wrapper.Update();
wrapper.Render();
wrapper.Quit();*/
}
}
}

如您所见,我只是调用了包含在我的 CLR 包装器中的方法(在这里,为了简单起见,我只调用了“Init”方法)。而已。但是编译后我有以下异常:

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

Additional information: Could not load file or assembly 'WrapperCLR.dll' or one of its dependencies. The specified module could not be found.

似乎没有找到WrapperCLR.dll,但实际上并非如此。实际上,如果我在“TargetDisplay.cpp”文件中删除所有 SDL 系统调用,程序就会编译并执行。

但是当然没有显示,因为 SDL 系统调用被禁用。所以 WrapperCLR.dll 被系统找到了。所以我想知道它是否没有找到SDL2.dll库。

也许无法通过 C++ CLR 包装器从 C# 应用程序将 SDL C++ 程序作为静态库加载?或者,也许我必须直接在我的源代码中的汇编文件中添加特定的链接信息。

最佳答案

我认为这只是您所说的各个 DLL 的路径问题。

在 Visual Studio 中的 IIRC,项目默认启动时当前目录设置为 $(ProjectDir) 并从 $(OutDir)\$(Configuration) 执行。

确保顶级 C# 项目的这些目录中有 WrapperCLR.dllSDL2.dll

当您禁用对 DLL 的所有调用时它会起作用,因为它的导入将被完全删除(优化)。

关于c# - 使用 CLR C++ 包装器从 C# 应用程序在 C++ 中加载 SDL 窗口不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23134491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com