Click here to Skip to main content
15,616,987 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, I have this code in DirectX 9, in which I need to get the DC from a COM interface and draw it. I do get the DC and it contains the image, but I get a black screen. Any ideas why?

LPDIRECT3DSURFACE9 pRenderSurface = NULL, pRenderSurfaceTMP = NULL;

    m_pRenderTexture->GetSurfaceLevel(0, &pRenderSurface);
    if (pRenderSurface == NULL)
        return FALSE;

    m_pD3DDevice->CreateOffscreenPlainSurface(m_nWidth, m_nHeight,
        D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &pRenderSurfaceTMP, 0);


    HDC hDC = NULL;
    hr = pRenderSurfaceTMP->GetDC(&hDC);
    if (FAILED(hr))
        return FALSE;   

    if (m_pViewObject != NULL)
        // RECT is relative to the windowless container rect
        RECTL rcRect = {0, 0, m_nWidth, m_nHeight};  

        // Draw onto the DC!
        hr = m_pViewObject->Draw(DVASPECT_CONTENT, 1,
            NULL, NULL, NULL, hDC, &rcRect, NULL, NULL,


    // Draw the surface
    m_pD3DDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof(Vertex) );
    m_pD3DDevice->SetTexture( 0, m_pRenderTexture );

    hr = m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

    m_hbrBackground = NULL;

Thanks in advance,
SoMad 28-Jun-12 4:30am    
You are only doing the check if(FAILED(hr)) one place. I suggest you perform the same check for all the DD calls that might fail such as CreateOffscreenPlainSurface(), GetRenderTargetData(), SetStreamSource(), etc.
Add an ASSERT or a breakpoint if the return value is one of the failure codes.

Soren Madsen
yakobom 1-Jul-12 2:41am    
I just rechecked and I get a D3DERR_INVALIDCALL on GetRenderTargetData (and that's weird, since I am able to get a valid DC from pRenderSurfaceTMP). Any suggestions?

1 solution

Hi SoMad,
Basically you are correct, but I debugged it line by line and everything seemed ok. Plus, I do know I have the picture in the dc (I printed it to a file from hDC).

Do you have any idea what can be wrong?
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900