从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)加载时使用。帮帮我!?! :)

最佳答案

您永远不要从LoadLibrary调用DllMain。从documentation:



而是可以创建和导出初始化函数,并在加载DLL之后调用它。

10-04 16:24