Thread: How do you create swap chain I always get failure on the CreateSwapChainForHwnd call

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    1

    How do you create swap chain I always get failure on the CreateSwapChainForHwnd call

    Code:
    DXGI_SWAP_CHAIN_DESC1 desc;
        ZeroMemory(&desc, sizeof(DXGI_SWAP_CHAIN_DESC1));
        desc.BufferCount = 1;
        desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
        desc.SampleDesc.Count = 1;      //multisampling setting
        desc.SampleDesc.Quality = 0;    //vendor-specific flag
        desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
        desc.Width = 300;
        desc.Height = 300;
        desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    
        // Create the DXGI device object to use in other factories, such as Direct2D.
        IDXGIDevice3* dxgiDevice;
    
        // Create swap chain.
        IDXGIAdapter* adapter;
        IDXGIFactory2* factory;
        IDXGISwapChain1* swapchain;
    
        hr = device->QueryInterface(__uuidof(IDXGIDevice3), (void**)&dxgiDevice);
    
        hr = dxgiDevice->GetAdapter(&adapter);
    
        if (SUCCEEDED(hr))
        {
    
            hr = adapter->GetParent(__uuidof(IDXGIFactory2), (void**)&factory);
    
            hr = factory->CreateSwapChainForHwnd(
                device,
                m_amahwnd,
                &desc,
                NULL,
                NULL,
                &swapchain
            );
    
            try
            {
                if (!SUCCEEDED(hr))
                {
                    throw(std::invalid_argument("Blah"));
                }
            }
            catch (std::invalid_argument& blah)
            {
                MessageBox(m_amahwnd, L"Blah", L"blah", MB_OK);
            }
    I don't know how to get a swap chain created, should I go back and do a CreateDevice*AND*SwapChain?

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    1
    Hello Goukenfan,

    I have not had much exposure to Windows C++ for quite a while but I'll try to help. Just to be clear, the problem you are facing is that line 29 in code above fails i.e.
    Code:
    hr = factory->CreateSwapChainForHwnd(
                device,
                m_amahwnd,
                &desc,
                NULL,
                NULL,
                &swapchain
            );
    My first instinct would be to confirm that the factory pointer is not NULL. The next thing I would ask would be about the arguments you are passing into the CreateSwapChainForHwnd method. If in doubt, read the documentation
    IDXGIFactory2::CreateSwapChainForHwnd (dxgi1_2.h) - Win32 apps | Microsoft Docs

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Failure to create delay in a loop with printf(). Why?
    By TropicalStarfis in forum C Programming
    Replies: 2
    Last Post: 02-15-2012, 01:02 PM
  2. How to create DLL file in C language and call from C and VB
    By karthickbabu in forum C Programming
    Replies: 2
    Last Post: 10-24-2007, 07:50 AM
  3. Skipping DX swap chain node - not good...
    By Magos in forum Game Programming
    Replies: 0
    Last Post: 03-02-2006, 08:37 PM
  4. Call to new to create objects that go into a vector
    By matth in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2006, 02:59 PM
  5. API Call to create folder
    By cboard_member in forum Windows Programming
    Replies: 2
    Last Post: 12-01-2005, 12:08 PM

Tags for this Thread