从 DllMain 调用 IDirect3D9::CreateDevice() 挂起

发布于 2024-10-03 10:05:04 字数 1080 浏览 0 评论 0原文

可以有什么理由呢?

从 DLL_PROCESS_ATTACH 上的 DllMain() 调用 IDirect3D9::CreateDevice() ,它挂起的

代码很简单,就像:

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    if (ul_reason_for_call = DLL_PROCESS_ATTACH) {
        IDirect3D9* d3d = Direct3DCreate9(D3D_SDK_VERSION);

        D3DPRESENT_PARAMETERS pp = {};
        pp.BackBufferWidth = 1;
        pp.BackBufferHeight = 1;
        pp.BackBufferFormat = D3DFMT_X8R8G8B8;
        pp.BackBufferCount = 1;
        pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        pp.Windowed = TRUE;

        IDirect3DDevice9* device = NULL;
        HRESULT hr = d3d->CreateDevice(
            D3DADAPTER_DEFAULT, 
            D3DDEVTYPE_HAL, 
            GetDesktopWindow(), 
            D3DCREATE_HARDWARE_VERTEXPROCESSING, 
            &pp, 
            &device);

        device->Release();
        d3d->Release();
    }
    return TRUE;
}

GetDesktopWindow() 用于简单起见,我尝试创建自己的窗口并使用它,结果相同

What can be a reason?

From DllMain() on DLL_PROCESS_ATTACH I'm calling IDirect3D9::CreateDevice() and it hangs

code is straightforward, just like:

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    if (ul_reason_for_call = DLL_PROCESS_ATTACH) {
        IDirect3D9* d3d = Direct3DCreate9(D3D_SDK_VERSION);

        D3DPRESENT_PARAMETERS pp = {};
        pp.BackBufferWidth = 1;
        pp.BackBufferHeight = 1;
        pp.BackBufferFormat = D3DFMT_X8R8G8B8;
        pp.BackBufferCount = 1;
        pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        pp.Windowed = TRUE;

        IDirect3DDevice9* device = NULL;
        HRESULT hr = d3d->CreateDevice(
            D3DADAPTER_DEFAULT, 
            D3DDEVTYPE_HAL, 
            GetDesktopWindow(), 
            D3DCREATE_HARDWARE_VERTEXPROCESSING, 
            &pp, 
            &device);

        device->Release();
        d3d->Release();
    }
    return TRUE;
}

GetDesktopWindow() is used for simplicity, I tried to create own window and use it, the same result

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断念 2024-10-10 10:05:04

您不能在 DllMain 中执行此类操作。具体来说,您不能从其他 DLL 调用函数。您只能在主应用程序调用导出函数时执行此操作。

引用 MSDN 上的文档

DllMain 中的线程持有加载程序锁,因此无法动态加载或初始化其他 DLL。

调用需要 Kernel32.dll 之外的 DLL 的函数可能会导致难以诊断的问题。例如,调用 User、Shell 和 COM 函数可能会导致访问冲突错误,因为某些函数会加载其他系统组件。

You cannot do these kind of things in DllMain. Specifically, you cannot call functions from other DLLs. You can only do this from an exported function, when it is called by the main application.

Quoting the docs on MSDN:

Threads in DllMain hold the loader lock so no additional DLLs can be dynamically loaded or initialized.

Calling functions that require DLLs other than Kernel32.dll may result in problems that are difficult to diagnose. For example, calling User, Shell, and COM functions can cause access violation errors, because some functions load other system components.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文