0

I need to play a audio file from a resource inside a hooked DLL:

PlaySoundA(MAKEINTRESOURCEA(IDR_WAVE1), hModule, SND_RESOURCE);

here my code:

HINSTANCE m_hinst_dll = nullptr;
extern "C" UINT_PTR mProcs[17]{ 0 };
 
LPCSTR import_names[] = {
    "GetFileVersionInfoA",
    "GetFileVersionInfoByHandle",
    "GetFileVersionInfoExA",
    "GetFileVersionInfoExW",
    "GetFileVersionInfoSizeA",
    "GetFileVersionInfoSizeExA",
    "GetFileVersionInfoSizeExW",
    "GetFileVersionInfoSizeW",
    "GetFileVersionInfoW",
    "VerFindFileA",
    "VerFindFileW",
    "VerInstallFileA",
    "VerInstallFileW",
    "VerLanguageNameA",
    "VerLanguageNameW",
    "VerQueryValueA",
    "VerQueryValueW"
};
 
void setupWrappers()
{
    CHAR sys_dir[MAX_PATH];
    GetSystemDirectoryA(sys_dir, MAX_PATH);
    char buffer[MAX_PATH];
    sprintf_s(buffer, "%s\\version.dll", sys_dir);
    m_hinst_dll = LoadLibraryA(buffer);
    for (int i = 0; i < 17; i++) {
        mProcs[i] = reinterpret_cast<UINT_PTR>(GetProcAddress(m_hinst_dll, import_names[i]));
    }
}
 
extern "C" void GetFileVersionInfoA_wrapper();
extern "C" void GetFileVersionInfoByHandle_wrapper();
extern "C" void GetFileVersionInfoExA_wrapper();
extern "C" void GetFileVersionInfoExW_wrapper();
extern "C" void GetFileVersionInfoSizeA_wrapper();
extern "C" void GetFileVersionInfoSizeExA_wrapper();
extern "C" void GetFileVersionInfoSizeExW_wrapper();
extern "C" void GetFileVersionInfoSizeW_wrapper();
extern "C" void GetFileVersionInfoW_wrapper();
extern "C" void VerFindFileA_wrapper();
extern "C" void VerFindFileW_wrapper();
extern "C" void VerInstallFileA_wrapper();
extern "C" void VerInstallFileW_wrapper();
extern "C" void VerLanguageNameA_wrapper();
extern "C" void VerLanguageNameW_wrapper();
extern "C" void VerQueryValueA_wrapper();
extern "C" void VerQueryValueW_wrapper();
 
BOOL APIENTRY DllMain( HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
            setupWrappers();
 
            PlaySoundA(MAKEINTRESOURCEA(IDR_WAVE1), hModule, SND_RESOURCE);

            break;
 
        case DLL_PROCESS_DETACH:
 
            break;
    }
    return TRUE;
}

without this line:

PlaySoundA(MAKEINTRESOURCEA(IDR_WAVE1), hModule, SND_RESOURCE);

the dll work perfectly but if I add it the game not start.

To do another test I have created a empty desktop project with this line and work perfectly and I can hear the audio.

If I add this line:

 sndPlaySoundA("mnu_back.wav", SND_FILENAME | SND_ASYNC);

in my dll work.

So the problem is the resource that not work on my hooked DLL.

Any idea about what can be the cause ?

Thanks !

marcoluca987
  • 59
  • 1
  • 5
  • It's probably not a good idea to call that from inside `DllMain`, and I'm guessing that by "does not start" you mean that it hangs. Perhaps you should do this from a thread (which you can start there), or trigger it from an intercepted host function. Related: https://stackoverflow.com/a/57629805 and also read https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-best-practices which strongly suggests you limit your actions in that context. – Hasturkun Sep 20 '22 at 14:34
  • You might also be interested in something like [Detours](https://github.com/microsoft/Detours), where some of the examples [hook the program's entry point](https://github.com/microsoft/Detours/search?q=loadlibrary) as they aren't allowed to call `LoadLibrary` inside `DllMain` (as doing so might cause a deadlock or crash), and that could also be a convenient place for creating a new thread, if needed. – Hasturkun Sep 20 '22 at 14:41
  • you have right now work !!! thank you !! if you write it as response I will accept it. – marcoluca987 Sep 20 '22 at 14:47

0 Answers0