gpt4 book ai didi

c - Windows 7 dsound.dll 从 dll 崩溃加载

转载 作者:行者123 更新时间:2023-12-03 15:30:56 27 4
gpt4 key购买 nike

在 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)加载。帮助!?! :)

最佳答案

永远不要从 DllMain 调用 LoadLibrary。来自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.

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

关于c - Windows 7 dsound.dll 从 dll 崩溃加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2729633/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com