无法在DLL中创建D3D11 SwapChain

时间:2017-07-22 11:23:28

标签: c++ hook directx-11 direct3d

我目前正在尝试创建一个停止呈现游戏的应用程序,并每隔几秒拍摄一次Dx11游戏的截图。
我的方法是创建一个基本的SwapChain,然后在内存中搜索与那个具有相同vTable的内容。

现在我的问题是我完全停留在SwapChain的创建上。它每次都会产生DXGI_ERROR_INVALID_CALL,我无法弄清楚哪个参数出错了。

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

#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;
}

1 个答案:

答案 0 :(得分:2)

看看这个链接。

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

  

由于DllMain函数不能保证其加载和卸载DLL的顺序,因此我们建议您应用的DllMain函数不要调用Direct3D或DXGI函数或方法,包括创建或释放对象的函数或方法。

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