DLL Proxy call LoadLibrary cause exception : Access violation reading location 0x00000250

0

I'm trying to create a proxy dll for dinput8.dll. The .exe load my custom dll (proxy dll) dinput8.dll. dinput8_x86.dll is renamed version of the origin .dll that loaded by my proxy dll. Everything work fine, but when i remove the MessageBox code line, then rebuild the project, i have got the error:

Exception thrown at 0x772BBC61 in localhost_dinput8.exe: 0xC0000005: Access violation reading location 0x00000250.

Unhandled exception at 0x772BBC61 in localhost_dinput8.exe: 0xC000041D: An unhandled exception was encountered during a user callback.

The exception throw out when trace over LoadLibrary(_T(".\\dinput8_x86.dll"));. I don't know why, can anyone help me please, i don't want to show that message box everytime my dll is loaded. If i put back the code line: MessageBox -> the exception is GONE. Here is my example code of proxy DLL.

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#pragma pack(1)

FARPROC p[5] = { 0 };

extern "C" BOOL WINAPI DllMain(HINSTANCE hInst, DWORD reason, LPVOID)
{
    static HINSTANCE hL;
    if (reason == DLL_PROCESS_ATTACH)
    {
        hL = LoadLibrary(_T(".\\dinput8_x86.dll")); // The exception at here
        if (!hL) return false;
        p[0] = GetProcAddress(hL, "DllGetClassObject");
        p[1] = GetProcAddress(hL, "DllCanUnloadNow");
        p[2] = GetProcAddress(hL, "DirectInput8Create");
        p[3] = GetProcAddress(hL, "DllRegisterServer");
        p[4] = GetProcAddress(hL, "DllUnregisterServer");
//MessageBox(NULL,_T("Hello World"),_T("Hi"),NULL);//Exception gone when uncomment this line
    }
    if (reason == DLL_PROCESS_DETACH)
        FreeLibrary(hL);
    return TRUE;
}

extern "C" __declspec(naked) void Proxy_DllGetClassObject()
{
    __asm
    {
        jmp p[0 * 4];
    }
}

extern "C" __declspec(naked) void Proxy_DllCanUnloadNow()
{
    __asm
    {
        jmp p[1 * 4];
    }
}

extern "C" __declspec(naked) void Proxy_DirectInput8Create()
{
    __asm
    {
        jmp p[2 * 4];
    }
}

extern "C" __declspec(naked) void Proxy_DllRegisterServer()
{
    __asm
    {
        jmp p[3 * 4];
    }
}

extern "C" __declspec(naked) void Proxy_DllUnregisterServer()
{
    __asm
    {
        jmp p[4 * 4];
    }
}

Here is my definition file: dinput8.def

EXPORTS
DllGetClassObject=Proxy_DllGetClassObject @1
DllCanUnloadNow=Proxy_DllCanUnloadNow @2
DirectInput8Create=Proxy_DirectInput8Create @3
DllRegisterServer=Proxy_DllRegisterServer @4
DllUnregisterServer=Proxy_DllUnregisterServer @5

Additional information:

  • Im using Win7 Professional x64 update full

  • Visual Studio 2015

  • Project build on win32 x86 profile


Edit:

I realize that if I replace MessageBox to any other API such as MessageBeep or ShowCursor which in WinUser.h, the exception not show anymore. But if i remove it, exception show up. I still don't know why?

c++
exception
dll
proxy
hook
asked on Stack Overflow Dec 23, 2017 by Trần Leo • edited Dec 23, 2017 by Trần Leo

1 Answer

0

From Dynamic-Link Library Best Practices:

You should never perform the following tasks from within DllMain:

Call LoadLibrary or LoadLibraryEx (either directly or indirectly). This can cause a deadlock or a crash.

answered on Stack Overflow Dec 23, 2017 by user7860670 • edited Jun 20, 2020 by Community

User contributions licensed under CC BY-SA 3.0