首页 > 解决方案 > 如何为调用 DirectX 函数的 C# 窗体制作 C++ Dll?

问题描述

我想制作一个使用 C# Forms 进行窗口处理的 DirectX 应用程序,因为这是我最熟悉的。我制作了一个 C++ Dll 项目并添加了这些依赖项以在 Linker -> Input -> Additional Dependencies 下获取 DirectX。

d3d11.lib
dxgi.lib
dxguid.lib
uuid.lib
kernel32.lib
user32.lib
comdlg32.lib
advapi32.lib
shell32.lib
ole32.lib
oleaut32.lib

我将 System 下的 SubSystem 设置为 Windows。并将输出文件夹设置为 C# Forms 项目的 bin 文件夹,这样我就不必每次构建 C++ 项目时都复制粘贴。然后我添加两个文件,一个头文件和一个 cpp 文件。

测试导出.h

#pragma once

#include "pch.h"

#include <d3dcompiler.h>
#include <d3d11_1.h>
#include <directxcolors.h>
#include <wrl/client.h>

using namespace DirectX;

extern "C"
{
    __declspec(dllexport) void __cdecl Test();
}

测试导出.cpp

#include "pch.h"
#include "TestExport.h"

void Test()
{
    ID3DBlob *pBlob;
    D3D10CreateBlob(12, &pBlob);
    delete pBlob;
}

我知道 TestExport.cpp 中的函数目前没有做任何事情,但是当问题得到解决时,它将被一些真正的方法替换。问题是我一LNK2001 unresolved external symbol _D3D10CreateBlob@8调用该D3D10CreateBlob方法就会出错。我对 Dll 的了解不多,也不知道要搜索什么才能找到 awnser 或潜在客户

标签: c++visual-c++dlldirectx

解决方案


Thanks for helping me in finding or explain my problem :)

To link to DirectX i added the include path in Configuration Properties -> C/C++ -> General -> Additional Include Directories. visual studio thankfully has a macro for the include folder called $(WindowsSDK_IncludePath).

Then I went to Configuration Properties -> Linker -> General -> Additional Library Directories and set the x64 platform to $(WindowsSDK_LibraryPath_x64) and the x86 to $(WindowsSDK_LibraryPath_x86).

I also changed the Configuration Properties -> Linker -> Input -> Additional Dependencies to include

dxgi.lib
d3d11.lib
d3d12.lib
d3d10.lib
d3dcompiler.lib

which I think is the acctuall problem I had. That I didn't include d3d10.lib * facepalms *


推荐阅读