Windows 7 dsound.dll 从 dll 崩溃加载

发布于 2024-08-30 18:42:59 字数 1115 浏览 3 评论 0原文

我在 Windows 7 中从另一个 DLL 加载 dsound.dll 时发生崩溃。以下代码崩溃:

#include <Windows.h>
#include <mmreg.h>
#include <dsound.h>
#include <assert.h>

HRESULT (WINAPI *pDirectSoundEnumerateA)(LPDSENUMCALLBACKA pDSEnumCallback, LPVOID pContext);
HMODULE hDsound;
BOOL CALLBACK DSEnum(LPGUID a, LPCSTR b, LPCSTR c, LPVOID d)
{
    return TRUE;
}
void CrashTest()
{
    HRESULT hr;
    hDsound = LoadLibraryA("dsound.dll");
    assert(hDsound);
    *(void**)&pDirectSoundEnumerateA = (void*)GetProcAddress(hDsound, "DirectSoundEnumerateA");
    assert(pDirectSoundEnumerateA);
    hr = pDirectSoundEnumerateA(DSEnum, NULL);
    assert(!FAILED(hr));
}
BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
{
    if (ul_reason_for_call == DLL_PROCESS_ATTACH)
    {
        DisableThreadLibraryCalls(hModule);
        CrashTest();
    }
}

出现此错误代码:(

Unhandled exception at ... in ...: 0xC0000005: Access violation reading location 0x00000044.

由于某种原因,它始终为 0x44)。它适用于 Windows XP 或直接从 .exe(而不是从单独的 DLL)加载时。帮助!?! :)

I'm getting a crash when loading dsound.dll from another DLL in Windows 7. The following code crashes:

#include <Windows.h>
#include <mmreg.h>
#include <dsound.h>
#include <assert.h>

HRESULT (WINAPI *pDirectSoundEnumerateA)(LPDSENUMCALLBACKA pDSEnumCallback, LPVOID pContext);
HMODULE hDsound;
BOOL CALLBACK DSEnum(LPGUID a, LPCSTR b, LPCSTR c, LPVOID d)
{
    return TRUE;
}
void CrashTest()
{
    HRESULT hr;
    hDsound = LoadLibraryA("dsound.dll");
    assert(hDsound);
    *(void**)&pDirectSoundEnumerateA = (void*)GetProcAddress(hDsound, "DirectSoundEnumerateA");
    assert(pDirectSoundEnumerateA);
    hr = pDirectSoundEnumerateA(DSEnum, NULL);
    assert(!FAILED(hr));
}
BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
{
    if (ul_reason_for_call == DLL_PROCESS_ATTACH)
    {
        DisableThreadLibraryCalls(hModule);
        CrashTest();
    }
}

with this error code:

Unhandled exception at ... in ...: 0xC0000005: Access violation reading location 0x00000044.

(it's always 0x44 for some reason). It works on Windows XP or when loading directly from the .exe (not from a separate DLL). Help!?! :)

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

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

发布评论

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

评论(1

瞄了个咪的 2024-09-06 18:42:59

您绝对不应该从 DllMain 调用 LoadLibrary。来自文档

入口点函数应该只执行简单的初始化或终止任务。它不能调用 LoadLibrary 或 LoadLibraryEx 函数(或调用这些函数的函数),因为这可能会在 DLL 加载顺序中创建依赖循环。这可能会导致 DLL 在系统执行其初始化代码之前被使用。同样,入口点函数在进程终止期间不得调用 FreeLibrary 函数(或调用 FreeLibrary 的函数),因为这可能会导致系统执行其终止代码后使用 DLL。

相反,您可以创建并导出初始化函数并在加载 DLL 后调用它。

You should never call LoadLibrary from DllMain. From the documentation:

The entry-point function should perform only simple initialization or termination tasks. It must not call the LoadLibrary or LoadLibraryEx function (or a function that calls these functions), because this may create dependency loops in the DLL load order. This can result in a DLL being used before the system has executed its initialization code. Similarly, the entry-point function must not call the FreeLibrary function (or a function that calls FreeLibrary) during process termination, because this can result in a DLL being used after the system has executed its termination code.

Instead, you can create and export an initialization function and call it after loading the DLL.

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