Download Windows 7 dsound.dll from crash dll

I get a crash when loading dsound.dll from another dll in windows 7. The following code fails:

#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.

(by default it is always 0x44). It works on Windows XP or when loading directly from .exe (not from a separate DLL). Help!?!:)

+3
source share
1 answer

You should never call LoadLibraryfrom DllMain. From the documentation :

. LoadLibrary LoadLibraryEx ( , ), DLL. , DLL , . entry-point FreeLibrary ( , FreeLibrary) , , DLL , .

DLL.

+3

Source: https://habr.com/ru/post/1743176/


All Articles