Click here to Skip to main content
15,893,588 members
Articles / Programming Languages / C++/CLI

Using Pragmas to Create a Proxy DLL

Rate me:
Please Sign up or sign in to vote.
4.28/5 (12 votes)
20 Mar 2007CPOL4 min read 81.8K   1.3K   39  
An article on how to use MSVC pragmas to create a function forwarding DLL.
#include <windows.h>
#include <advapi32_fwd.h>
#pragma pack(1)

static HINSTANCE hLThis = 0;
static HINSTANCE hADVAPI32 = 0;
static TCHAR prtBuf[0x1000];
static DWORD dwWidth, dwHeight, dwFog, dwMIP, dwTexture, dwTrans;

//! The real API functions.
static LONG( APIENTRY *ADVAPI32_RegQueryValueExA )(
    HKEY hKey,
    LPCTSTR lpValueName,
    LPDWORD lpReserved,
    LPDWORD lpType,
    LPBYTE lpData,
    LPDWORD lpcbData
);
static LONG( APIENTRY *ADVAPI32_RegSetValueExA )(
    HKEY hKey,
    LPCTSTR lpValueName,
    DWORD Reserved,
    DWORD dwType,
    const BYTE* lpData,
    DWORD cbData
);

//! Hooked functions.
extern "C" LONG APIENTRY HookedRegQueryValueExA(
        HKEY hKey,
        LPCTSTR lpValueName,
        LPDWORD lpReserved,
        LPDWORD lpType,
        LPBYTE lpData,
        LPDWORD lpcbData
    )
{
    LONG retVal = ADVAPI32_RegQueryValueExA( hKey, lpValueName, lpReserved, lpType, lpData, lpcbData );

    if( !lstrcmpi( lpValueName, "Display Height" ) )
    {
        *( DWORD * )lpData = dwHeight;
#ifdef _DEBUG
        wsprintf( prtBuf, "Faking display height to %d.\n", dwHeight );
        OutputDebugString( prtBuf );
#endif
    }
    else if( !lstrcmpi( lpValueName, "Display Width" ) )
    {
        *( DWORD * )lpData = dwWidth;
#ifdef _DEBUG
        wsprintf( prtBuf, "Faking display width to %d.\n", dwWidth );
        OutputDebugString( prtBuf );
#endif
    }
    else if( !lstrcmpi( lpValueName, "Fog" ) )
    {
        *( DWORD * )lpData = dwWidth;
#ifdef _DEBUG
        wsprintf( prtBuf, "Faking fog to %d.\n", dwFog );
        OutputDebugString( prtBuf );
#endif
    }
    else if( !lstrcmpi( lpValueName, "MIP Mapping" ) )
    {
        *( DWORD * )lpData = dwWidth;
#ifdef _DEBUG
        wsprintf( prtBuf, "Faking MIP mapping to %d.\n", dwMIP );
        OutputDebugString( prtBuf );
#endif
    }
    else if( !lstrcmpi( lpValueName, "Texture Filtering" ) )
    {
        *( DWORD * )lpData = dwWidth;
#ifdef _DEBUG
        wsprintf( prtBuf, "Faking texture filtering to %d.\n", dwTexture );
        OutputDebugString( prtBuf );
#endif
    }
    else if( !lstrcmpi( lpValueName, "Translucency" ) )
    {
        *( DWORD * )lpData = dwWidth;
#ifdef _DEBUG
        wsprintf( prtBuf, "Faking translucency to %d.\n", dwTrans );
        OutputDebugString( prtBuf );
#endif
    }
    return retVal;
}

extern "C" LONG APIENTRY HookedRegSetValueExA(
        HKEY hKey,
        LPCTSTR lpValueName,
        DWORD Reserved,
        DWORD dwType,
        const BYTE* lpData,
        DWORD cbData
    )
{
    LONG retVal = ADVAPI32_RegSetValueExA( hKey, lpValueName, Reserved, dwType, lpData, cbData );
#ifdef _DEBUG
    wsprintf( prtBuf, "Set value for '%s'.\n", lpValueName );
    OutputDebugString( prtBuf );
    wsprintf( prtBuf, "New value '%s'.\n", lpData );
    OutputDebugString( prtBuf );
#endif
    return retVal;
}

//! Get resolutions.
BOOL GetReso( DWORD *width, DWORD *height )
{
    HKEY hKey;
    if( ERROR_SUCCESS == RegOpenKeyEx( HKEY_CURRENT_USER,
        "SOFTWARE\\MechProxy", 0, KEY_READ, &hKey ) )
    {
        //! Read values from registry.
        DWORD flag, type, count = 4;
#define GET_REG_DWORD( str, var ) \
flag = 0;\
RegQueryValueEx( hKey, str, NULL, &type, (LPBYTE)&var, &count )
        GET_REG_DWORD( "Display Width", dwWidth );
        GET_REG_DWORD( "Display Height", dwHeight );
        GET_REG_DWORD( "Fog", dwFog );
        GET_REG_DWORD( "MIP Mapping", dwMIP );
        GET_REG_DWORD( "Texture Filtering", dwTexture );
        GET_REG_DWORD( "Translucency", dwTrans );

        //! Close key.
        RegCloseKey( hKey );
        return TRUE;
    }
    return(FALSE);
}

//! Attach or detach this proxy.
BOOL WINAPI DllMain( HINSTANCE hInst,DWORD reason,LPVOID )
{
    if( reason == DLL_PROCESS_ATTACH )
    {
        OutputDebugString( "ADVAPI32 Proxy DLL starting.\n" );
        hLThis = hInst;
        hADVAPI32 = LoadLibrary( "advapi32" );
        if( !hADVAPI32 )
        {
            return FALSE;
        }
        *( void ** )&ADVAPI32_RegQueryValueExA =( void * )GetProcAddress( hADVAPI32, "RegQueryValueExA" );
        *( void ** )&ADVAPI32_RegSetValueExA =( void * )GetProcAddress( hADVAPI32, "RegSetValueExA" );

        //! Query for the resolution to use.
        if( FALSE == GetReso( &dwWidth, &dwHeight ) )
        {
            dwHeight =( DWORD )-1;
            dwWidth =( DWORD )-1;
        }
    }
    else if( reason == DLL_PROCESS_DETACH )
    {
        FreeLibrary( hADVAPI32 );
        OutputDebugString( "ADVAPI32 Proxy DLL signing off.\n" );
    }
    return TRUE;
}
//! End of file.

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Finland Finland
Started coding with Commodore VIC-20, and still doing it. When time permits...

Comments and Discussions