1

我正在开发像 MSN Plus 这样的实时信使插件。我将使用代理连接到 MSN msimg32.dll。与 Internet 中的其他代理示例不同msimg32.dll,其中导出的函数在 中检索DLLMain,因为 MSDN 表示LoadLibraryDLLMain. 这已通过测试确认,在自定义msimg32.dll范围内加载系统将导致访问冲突。DllMainmsimg32.dll

我的方法是加载c:\windows\system32\msimg32.dll一个导出的函数,我在 MSN plus 的msimg32.dll. 但是我在函数中面临一个严重错误,返回时GetMsimg32FnAddr变量libName已损坏GetMsimg32FnAddr

我的问题是如何避免我的源代码中的堆栈损坏。此外,我关于代理的想法可以msimg32.dll吗?

我的开发环境是:VS2005,win7企业版,32位。最新版本的 Live Messenger。最新版本的 MSN 加。

VS2005 报告:

运行时检查失败 #2 - 变量“libName”周围的堆栈已损坏。

GetMsimg32FnAddr返回时

以下是我的代理源代码msimg32.dll

msimgproxy.h

/*****************************************************************************/
#ifndef MSIMGPROXY_H
#define MSIMGPROXY_H
#include <windows.h>
#pragma warning( disable:4273 ) // Silence compiler

//
// Function pointer typedefs for exports we are forwarding              
//
typedef BOOL (WINAPI *PFNTRANSPARENTBLT) (HDC,int,int,int,int,HDC,int,int,int,int,UINT);
typedef VOID (WINAPI *PFNVSETDDRAWFLAG)  (VOID);
typedef BOOL (WINAPI *PFNALPHABLEND)    (HDC,int,int,int,int,HDC,int,int,int,int,BLENDFUNCTION);
typedef BOOL (WINAPI *PFNGRADIENTFILL)  (HDC,PTRIVERTEX,ULONG,PVOID,ULONG,ULONG);
typedef BOOL (WINAPI *PFNDLLINITIALIZE)(HINSTANCE, DWORD, LPVOID);

// Function pointer typedef to injected-DLL initialization
typedef void (*PFNINIT) (void); 

#ifdef __cplusplus
extern "C" 
{
#endif 
    //
    // Function protoypes
    //
    BOOL WINAPI TransparentBlt(HDC, int, int, int, int, HDC, int, int, int, int, UINT);
    BOOL WINAPI AlphaBlend    (HDC, int , int, int, int, HDC, int , int, int, int, BLENDFUNCTION);
    BOOL WINAPI GradientFill  (HDC, PTRIVERTEX, ULONG, PVOID, ULONG, ULONG);
    BOOL WINAPI DllInitialize (HINSTANCE, DWORD, LPVOID);
    VOID WINAPI vSetDdrawflag (VOID);

#ifdef __cplusplus
}
#endif // __CPLUSPLUS
#endif // MSIMGPROXY_H

DllMain.cpp

#include "msimgproxy.h"
#include <tchar.h>

PFNTRANSPARENTBLT   pfnTransparentBlt   = NULL;
PFNALPHABLEND       pfnAlphaBlend       = NULL;
PFNDLLINITIALIZE    pfnDllInitialize    = NULL;
PFNGRADIENTFILL     pfnGradientFill     = NULL;
PFNVSETDDRAWFLAG    pfnVSetDdrawFlag    = NULL;
PFNINIT             pfnInit             = NULL;

bool        GetMsimg32FnAddr();
HMODULE     hMsimg32 = NULL;
HMODULE     hPlugDll = NULL;

TCHAR pszInjDllName[] = TEXT("msg_plugin.dll");

/************************************************************************/
/*                                                                      */
/* Proxy DLL Entry point                                                */
/*                                                                      */
/************************************************************************/

BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, 
                      LPVOID lpReserved )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:        
        {
            /*
            if (!GetMsimg32FnAddr())
            {
                OutputDebugString (TEXT("MSIMG32 Proxy: GetMsimg32FnAddr() failed\n"));
                return FALSE;
            }
            */

            OutputDebugString(TEXT("MSIMG32 Proxy: DLL attached\n"));

            // Load injected DLL and execute it's initialization function           
            if (! (hPlugDll = LoadLibrary(pszInjDllName)))
            {
                OutputDebugString (TEXT("MSIMG32 Proxy: Cannot load inject-DLL\n"));
                //return FALSE;
            }

            if (! (pfnInit = (PFNINIT) GetProcAddress (hPlugDll, "Init")))
            {
                OutputDebugString (TEXT("MSIMG32 Proxy: No valid address for executing Init\n"));
                //return FALSE;
            }

            if (pfnInit)
            {
                (*pfnInit)();
            }
            break;
        }

    case DLL_PROCESS_DETACH:
        {
            if (hMsimg32)
                if (FreeLibrary(hMsimg32))
                    OutputDebugString (TEXT("MSIMG32 Proxy: DLL detached.\n"));
            break;
        }
    }
    return TRUE;
}

// 
// Get original MSIMG32.DLL functions virtual addresses
//////////////////////////////////////////////////////////////////////////
bool GetMsimg32FnAddr()
{
    TCHAR libName[MAX_PATH];
    GetSystemDirectory (libName, MAX_PATH);
    _tcscat_s (libName, MAX_PATH*sizeof(TCHAR), TEXT("\\msimg32.dll"));
    hMsimg32 = LoadLibrary (libName);

    if (!hMsimg32)
        return false;

    if ( (pfnVSetDdrawFlag = (PFNVSETDDRAWFLAG) GetProcAddress(hMsimg32, "vSetDdrawflag")) == NULL)
        return false;

    if ( (pfnAlphaBlend = (PFNALPHABLEND) GetProcAddress(hMsimg32, "AlphaBlend")) == NULL)
        return false;

    if ( (pfnDllInitialize = (PFNDLLINITIALIZE) GetProcAddress(hMsimg32, "DllInitialize")) == NULL)
        return false;

    if ( (pfnGradientFill = (PFNGRADIENTFILL) GetProcAddress(hMsimg32, "GradientFill")) == NULL)
        return false;

    if ( (pfnTransparentBlt = (PFNTRANSPARENTBLT) GetProcAddress(hMsimg32, "TransparentBlt")) == NULL)
        return false;

    return true;
}

//
// Exported function definition
//////////////////////////////////////////////////////////////////////////
BOOL WINAPI TransparentBlt(HDC p1, int p2, int p3, int p4, int p5, HDC p6, int p7, int p8, 
                                   int p9, int p10, UINT p11)
{   
    return pfnTransparentBlt (p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11);
}

BOOL WINAPI AlphaBlend(HDC p1, int p2, int p3, int p4, int p5, HDC p6, int p7 , int p8,
                               int p9, int p10, BLENDFUNCTION dw)
{
    return pfnAlphaBlend (p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,dw);
}

BOOL WINAPI GradientFill (HDC p1, PTRIVERTEX p2, ULONG p3, PVOID p4, ULONG p5, ULONG p6)
{
    return pfnGradientFill (p1, p2, p3, p4, p5, p6);
}

BOOL WINAPI DllInitialize (HINSTANCE d1,DWORD d2, LPVOID d3)
{
    if (pfnDllInitialize == NULL)
    {
        GetMsimg32FnAddr();
    }

    return pfnDllInitialize (d1, d2, d3);
}

VOID WINAPI vSetDdrawflag (VOID)
{
    (*pfnVSetDdrawFlag)();
}
4

0 回答 0