0

I'm trying to create a GDI device context from Direct2D but when I call CreateDCRenderTarget, it returns D2DERR_NO_HARDWARE_DEVICE error and my RenderTarget is null. Is there something wrong with the properties ?

ID2D1Factory* _pDirect2dFactory = NULL;
ID2D1DCRenderTarget *_pRenderTarget = NULL;

D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
    D2D1_RENDER_TARGET_TYPE_DEFAULT,
    D2D1::PixelFormat(
        DXGI_FORMAT_B8G8R8A8_UNORM,
        D2D1_ALPHA_MODE_IGNORE),
    0,
    0,
    D2D1_RENDER_TARGET_USAGE_NONE,
    D2D1_FEATURE_LEVEL_DEFAULT
    );

  HRESULT hr;

  hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &_pDirect2dFactory);

  if (_pDirect2dFactory)
  {
    // *** ERROR here *** : hr=0x8899000B (D2DERR_NO_HARDWARE_DEVICE)
    hr = _pDirect2dFactory->CreateDCRenderTarget(&props, &_pRenderTarget);
  }

EDIT : That code works fine when it is called from my executable but do not work when it is called from another program through my injected DLL.

EDIT2 : Well, that works now. This code was called when my DLL was attached and I think that D2D1.dll was not correctly attached to the process yet.

4

1 回答 1

0

对于那些在注入创建 d2d 的 dll 时仍然遇到同样问题的人,请尝试将函数放入新线程。就我而言,我通过 DllMain 中的 CreateThread 将 d2d 创建函数直接放入新线程中,它可以工作。

于 2018-04-30T20:51:54.467 回答