声明HMODULE会在运行时抛出异常

时间:2012-01-05 23:20:35

标签: c++ dll handle loadlibrary

我正在编写一个C ++ DLL,在其中一个函数中加载另一个DLL。但是,当我尝试运行它时,它在我第一次声明我的HMODULE变量时崩溃,甚至在我尝试调用LoadLibrary之前。

具体来说,我的代码是:

HMODULE my_hmod;
my_hmod = LoadLibrary("C:\\path_to_dll");

它在这两行之间崩溃了(我已经介入它并注意到它在“ostream”中崩溃,如果这有用的话)。

例外情况如下:

  

PixelMotionBlur.exe中0x00a7392d(d3d9.dll)的未处理异常:0xC0000005:访问冲突读取位置0x00000000。

任何帮助将不胜感激!如果需要,我会发布更多代码/调试信息。

编辑:这是实际的功能代码:

    D3D9Wrapper::IDirect3D9* WINAPI Direct3DCreate9(UINT Version)
{
    InitializeDLL();
    HMODULE hD3D;
    if(IsWow64())
    {
        hD3D = LoadLibrary("C:\\Windows\\SysWOW64\\d3d9.dll");
    }
    else
    {
        hD3D = LoadLibrary(g_Globals.RealD3D9DLL.CString());
    }

    if( hD3D == NULL )
    {
        g_Globals.ErrorFile() << "LoadLibrary on d3d9.dll failed\n";
        return NULL;
    }

    D3D9Wrapper::D3DCREATE pCreate = (D3D9Wrapper::D3DCREATE)GetProcAddress(hD3D, _T("Direct3DCreate9"));
    if( pCreate == NULL )
    {
        g_Globals.ErrorFile() << "coud not find Direct3DCreate9 in d3d9.dll\n";
        return NULL;
    }

    D3D9Base::LPDIRECT3D9 pD3D = pCreate(D3D_SDK_VERSION);
    if( pD3D == NULL )
    {
        return NULL;
    }

    //return (D3D9Wrapper::IDirect3D9*)pD3D;
    return D3D9Wrapper::IDirect3D9::GetDirect3D(pD3D);
}

如果它有帮助,这个DLL的功能(大多数实际上不是我的代码,但经许可使用)是通过在目标.exe文件夹中放置一个名为d3d9的新dll来挂钩所有对d3d9.dll的调用。 (现在我正在使用Direct X 9 sdk中的示例.exes测试它。)

编辑2:这是函数的完整反汇编:

    D3D9Wrapper::IDirect3D9* WINAPI Direct3DCreate9(UINT Version)
{
008B6960  push        ebp  
008B6961  mov         ebp,esp  
008B6963  and         esp,0FFFFFFF8h  
008B6966  sub         esp,8  
    InitializeDLL();
008B6969  cmp         byte ptr [g_Globals (8CAC40h)],0  
008B6970  push        esi  
008B6971  push        edi  
008B6972  jne         Direct3DCreate9+48h (8B69A8h)  
008B6974  mov         edi,offset g_Globals (8CAC40h)  
008B6979  call        Globals::Init (8B6A80h)  
008B697E  mov         eax,dword ptr [g_Globals+34h (8CAC74h)]  
008B6983  push        8C0D98h  
008B6988  push        eax  
008B6989  call        std::operator<<<std::char_traits<char> > (8938F0h)  
008B698E  call        dword ptr [__imp_D3D9CallbackInitialize (8B81A4h)]  
008B6994  mov         ecx,dword ptr [g_Globals+34h (8CAC74h)]  
008B699A  push        8C0DB0h  
008B699F  push        ecx  
008B69A0  call        std::operator<<<std::char_traits<char> > (8938F0h)  
008B69A5  add         esp,10h  
    HMODULE hD3D;
    if(IsWow64())
008B69A8  push        8C0E60h  
008B69AD  push        8C0E70h  
008B69B2  mov         dword ptr [esp+14h],0  
008B69BA  call        dword ptr [__imp__GetModuleHandleA@4 (8B80D0h)]  
008B69C0  mov         edi,dword ptr [__imp__GetProcAddress@8 (8B80C4h)]  
008B69C6  push        eax  
008B69C7  call        edi  
008B69C9  mov         esi,eax  
008B69CB  test        esi,esi  
008B69CD  je          Direct3DCreate9+94h (8B69F4h)  
008B69CF  lea         edx,[esp+0Ch]  
008B69D3  push        edx  
008B69D4  call        dword ptr [__imp__GetCurrentProcess@0 (8B80B8h)]  
008B69DA  push        eax  
008B69DB  call        esi  
008B69DD  test        eax,eax  
008B69DF  jne         Direct3DCreate9+94h (8B69F4h)  
008B69E1  mov         eax,dword ptr [g_Globals+3Ch (8CAC7Ch)]  
008B69E6  push        8C0E7Ch  
008B69EB  push        eax  
008B69EC  call        std::operator<<<std::char_traits<char> > (8938F0h)  
008B69F1  add         esp,8  
008B69F4  cmp         dword ptr [esp+0Ch],0  
008B69F9  je          Direct3DCreate9+0A2h (8B6A02h)  
    {
        hD3D = LoadLibrary("C:\\Windows\\SysWOW64\\d3d9.dll");
008B69FB  push        8C0E98h  
    }
    else
008B6A00  jmp         Direct3DCreate9+0B1h (8B6A11h)  
    {
        hD3D = LoadLibrary(g_Globals.RealD3D9DLL.CString());
008B6A02  mov         eax,dword ptr [g_Globals+1Ch (8CAC5Ch)]  
008B6A07  test        eax,eax  
008B6A09  jne         Direct3DCreate9+0B0h (8B6A10h)  
008B6A0B  mov         eax,offset g_Globals+1Ch (8CAC5Ch)  
008B6A10  push        eax  
008B6A11  call        dword ptr [__imp__LoadLibraryA@4 (8B80CCh)]  
    }

    if( hD3D == NULL )
008B6A17  test        eax,eax  
008B6A19  jne         Direct3DCreate9+0D9h (8B6A39h)  
    {
        g_Globals.ErrorFile() << "LoadLibrary on d3d9.dll failed\n";
008B6A1B  mov         ecx,dword ptr [g_Globals+3Ch (8CAC7Ch)]  
008B6A21  push        8C0EB8h  
008B6A26  push        ecx  
008B6A27  call        std::operator<<<std::char_traits<char> > (8938F0h)  
008B6A2C  add         esp,8  
        return NULL;
008B6A2F  xor         eax,eax  
}
008B6A31  pop         edi  
008B6A32  pop         esi  
008B6A33  mov         esp,ebp  
008B6A35  pop         ebp  
008B6A36  ret         4  
    }
    D3D9Wrapper::D3DCREATE pCreate = (D3D9Wrapper::D3DCREATE)GetProcAddress(hD3D, _T("Direct3DCreate9"));
008B6A39  push        8C0ED8h  
008B6A3E  push        eax  
008B6A3F  call        edi  
    if( pCreate == NULL )
008B6A41  test        eax,eax  
008B6A43  jne         Direct3DCreate9+103h (8B6A63h)  
    {
        g_Globals.ErrorFile() << "coud not find Direct3DCreate9 in d3d9.dll\n";
008B6A45  mov         edx,dword ptr [g_Globals+3Ch (8CAC7Ch)]  
008B6A4B  push        8C0EE8h  
008B6A50  push        edx  
008B6A51  call        std::operator<<<std::char_traits<char> > (8938F0h)  
008B6A56  add         esp,8  
        return NULL;
008B6A59  xor         eax,eax  
}
008B6A5B  pop         edi  
008B6A5C  pop         esi  
008B6A5D  mov         esp,ebp  
008B6A5F  pop         ebp  
008B6A60  ret         4  
    }

编辑3:从抛出异常开始的调用堆栈:

>   d3d9.dll!std::operator<<<std::char_traits<char> >(std::basic_ostream<char,std::char_traits<char> > & _Ostr, const char * _Val)  Line 773 + 0x3 bytes    C++
    d3d9.dll!InitializeDLL()  Line 70 + 0x15 bytes  C++
    d3d9.dll!Direct3DCreate9(unsigned int Version)  Line 141    C++
    PixelMotionBlur.exe!DXUT_Dynamic_Direct3DCreate9(unsigned int SDKVersion)  Line 714 + 0xc bytes C++
    PixelMotionBlur.exe!DXUTDelayLoadD3D9()  Line 2706 + 0xa bytes  C++
    PixelMotionBlur.exe!DXUTGetD3D9Object()  Line 712 + 0x23 bytes  C++
    PixelMotionBlur.exe!CD3D9Enumeration::Enumerate(bool (_D3DCAPS9 *, _D3DFORMAT, _D3DFORMAT, bool, void *)* IsD3D9DeviceAcceptableFunc, void * pIsD3D9DeviceAcceptableFuncUserContext)  Line 309 + 0x5 bytes  C++
    PixelMotionBlur.exe!DXUTGetD3D9Enumeration(bool bForceEnumerate)  Line 255  C++
    PixelMotionBlur.exe!DXUTFindValidDeviceSettings(DXUTDeviceSettings * pOut, DXUTDeviceSettings * pIn, DXUTMatchOptions * pMatchOptions)  Line 174    C++
    PixelMotionBlur.exe!DXUTCreateDevice(bool bWindowed, int nSuggestedWidth, int nSuggestedHeight)  Line 2010 + 0x17 bytes C++
    PixelMotionBlur.exe!wWinMain(HINSTANCE__ * __formal, HINSTANCE__ * __formal, HINSTANCE__ * __formal, HINSTANCE__ * __formal)  Line 207  C++
    PixelMotionBlur.exe!__tmainCRTStartup()  Line 547 + 0x2c bytes  C
    PixelMotionBlur.exe!wWinMainCRTStartup()  Line 371  C
    kernel32.dll!75e1339a()     
    [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]  
    ntdll.dll!77829ed2()    
    ntdll.dll!77829ea5()    

2 个答案:

答案 0 :(得分:2)

基于调用堆栈,错误在InitializeDLL函数中。在没有看到该函数的源代码的情况下无法确定地说,但由于它是通过std :: ostream的NULL指针异常,我敢打赌你试图向std :: cout发送一个NULL字符串或者一些这样的

检查源文件的第70行,即InitializeDLL所在。如果没有看到问题,请发布InintializeDLL中的代码。

答案 1 :(得分:0)

确保您正在运行&amp;构建程序的调试版本。在VS2010中,确保选择了“Debug”构建配置&amp;在调试器菜单中确保您正在打破任何C ++异常。

你是对的 - 我能够做到以下几点。

HMODULE hD3D = LoadLibrary(L"C:\\Windows\\SysWOW64\\d3d9.dll");