gpt4 book ai didi

c++ - 无法在 DLL 中创建 D3D11 SwapChain

转载 作者:行者123 更新时间:2023-11-30 03:30:00 24 4
gpt4 key购买 nike

我目前正在尝试创建一个应用程序,它停止渲染游戏并每隔几秒截取一次 Dx11 游戏的屏幕截图。
我的方法是创建一个基本的 SwapChain,然后在内存中搜索与那个具有相同 vTable 的东西。

现在我的问题是我完全卡在了 SwapChain 创建上。每次都会导致 DXGI_ERROR_INVALID_CALL,我无法弄清楚哪个参数有问题。

这是我的代码。我使用带有 x86 设置的 VS2017,没有任何其他设置。

#include <Windows.h>
#include <iostream>

#include <d3d11.h>
#pragma comment(lib, "d3d11.lib")


bool CreateSwapChain() {

// Gets handle of game
HWND window = FindWindow(NULL, L"WARFRAME");
if (window == NULL) return false;

DXGI_SWAP_CHAIN_DESC swap_desc;
memset(&swap_desc, 0, sizeof(struct DXGI_SWAP_CHAIN_DESC));
swap_desc.BufferDesc.Width = NULL; // Output window width
swap_desc.BufferDesc.Height = NULL; // Output window height
swap_desc.BufferDesc.Format = DXGI_FORMAT_UNKNOWN; // No idea what the right value is here
swap_desc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; // No specific scanline method
swap_desc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; // Seems to be the best option
swap_desc.SampleDesc.Count = 1; // Multisampling count (default)
swap_desc.SampleDesc.Quality = 0; // Multisampling quality (default
swap_desc.BufferUsage = DXGI_USAGE_READ_ONLY; // Dont need anything to do with the window
swap_desc.BufferCount = 1; // Only one (the window)
swap_desc.OutputWindow = window; // Output window
swap_desc.Windowed = TRUE; // Game is windowed
swap_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // Discard backbuffer after Present call
swap_desc.Flags = NULL;

D3D_FEATURE_LEVEL feature_level[1];
feature_level[0] = D3D_FEATURE_LEVEL_11_0;

IDXGISwapChain** swapchain_result = nullptr;

HRESULT swapchain = D3D11CreateDeviceAndSwapChain(
NULL, // Adapter. NULL because default adapter
D3D_DRIVER_TYPE_HARDWARE, // Driver. Hardware because it provides the best performance
NULL, // Software. NULL because driver is not software
NULL, // Flags. No Flags because none are needed
feature_level, // Feature level. Features supported by Direct3D 11.0
1, // Number of feature levels
D3D11_SDK_VERSION, // SDK Version. Default
&swap_desc, // Swapchain description defined above
swapchain_result, // Swapchain output
NULL, // Return feature level
NULL, // Discard feature level result
NULL // Discard device context result
);

std::cout << "Swapchain result: " << std::hex << swapchain << std::endl;
std::cout << "Swapchain pointer: " << std::hex << swapchain_result << std::endl;

return true;
}


FILE* pCout;

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdReason, LPVOID lpvReserved) {

if (fwdReason == DLL_PROCESS_ATTACH) {
AllocConsole();
freopen_s(&pCout, "CONOUT$", "w", stdout);
std::cout << "DLL attached" << std::endl;

// Create swapchain
std::cout << "Swapchain function result: " << CreateSwapChain() << std::endl;
}

else if (fwdReason == DLL_PROCESS_DETACH) {

}

return true;
}

最佳答案

看看这个链接。

https://learn.microsoft.com/zh-cn/windows/desktop/direct3ddxgi/d3d10-graphics-programming-guide-dxgi#dxgi-responses-from-dllmain

Because a DllMain function can't guarantee the order in which it loads and unloads DLLs, we recommend that your app's DllMain function not call Direct3D or DXGI functions or methods, including functions or methods that create or release objects.

在新线程中运行函数 CreateSwapChain 可能会解决问题。

关于c++ - 无法在 DLL 中创建 D3D11 SwapChain,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45253975/

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