Thread: DLL problem

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    DLL problem

    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <shlwapi.h>
    #include "ddraw.h"
    #include "testdll.h"
    #include "..\apihijack.h"
    
    // Text buffer for sprintf
    char Work[256];
    
    HINSTANCE hDLL;
    
    // Function pointer types.
    typedef DWORD (WINAPI *GetVersion_Type)( void );
    typedef BOOL (WINAPI *GetVersionExA_Type)( LPOSVERSIONINFO lpVersionInfo );
    
    // Function prototypes.
    DWORD WINAPI MyGetVersion( void );
    BOOL WINAPI MyGetVersionEx( LPOSVERSIONINFO lpVersionInfo );
    
    // Hook structure.
    enum
    {
        gFN_GetVersion = 0,
    	gFN_GetVersionExA = 1
    };
    
    SDLLHook gHook = 
    {
        "KERNEL32.DLL",
        false, NULL,		// Default hook disabled, NULL function pointer.
        {
            { "GetVersion", MyGetVersion },
    		{ "GetVersionExA", MyGetVersionEx },
            { NULL, NULL }
        }
    };
    
    // Hook functions.
    DWORD WINAPI MyGetVersion( void )
    {
        // Let the world know we're working.
        MessageBeep( MB_ICONINFORMATION );
    
        OutputDebugString( "TESTDLL: MyGetVersion called.\n" );
    
        GetVersion_Type OldFn = 
            (GetVersion_Type)gHook.Functions[gFN_GetVersion].OrigFn;
        return OldFn();
    }
    
    BOOL WINAPI MyGetVersionEx( LPOSVERSIONINFO lpVersionInfo )
    {
        // Let the world know we're working.
        MessageBeep( MB_ICONINFORMATION );
    
        OutputDebugString( "TESTDLL: MyGetVersionEx called.\n" );
    
        GetVersionExA_Type OldFn = 
            (GetVersionExA_Type)gHook.Functions[gFN_GetVersionExA].OrigFn;
        return OldFn( lpVersionInfo );
    }
    
    // CBT Hook-style injection.
    BOOL APIENTRY DllMain( HINSTANCE hModule, DWORD fdwReason, LPVOID lpReserved )
    {
        if ( fdwReason == DLL_PROCESS_ATTACH )  // When initializing....
        {
            hDLL = hModule;
    
            // We don't need thread notifications for what we're doing.  Thus, get
            // rid of them, thereby eliminating some of the overhead of this DLL
            DisableThreadLibraryCalls( hModule );
    
            // Only hook the APIs if this is the Everquest proess.
            GetModuleFileName( GetModuleHandle( NULL ), Work, sizeof(Work) );
            PathStripPath( Work );
    
            OutputDebugString( "TESTDLL checking process: " );
            OutputDebugString( Work );
            OutputDebugString( "\n" );
    
            if ( stricmp( Work, "BF2_EditorSetup_Jan04.exe" ) == 0 )
                HookAPICalls( &gHook );
        }
    
        return TRUE;
    }
    
    // This segment must be defined as SHARED in the .DEF
    #pragma data_seg (".HookSection")		
    // Shared instance for all processes.
    HHOOK hHook = NULL;	
    #pragma data_seg ()
    
    TESTDLL_API LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam) 
    {
        return CallNextHookEx( hHook, nCode, wParam, lParam); 
    }
    
    TESTDLL_API void InstallHook()
    {
        OutputDebugString( "TESTDLL hook installed.\n" );
        hHook = SetWindowsHookEx( WH_CBT, HookProc, hDLL, 0 ); 
    }
    
    TESTDLL_API void RemoveHook()
    {
        OutputDebugString( "TESTDLL hook removed.\n" );
        UnhookWindowsHookEx( hHook );
    }
    There are 5 errors
    Line 37:excess elements in aggregate initialzier
    Line 97:function 'LRESULT HookProc(int, WPARAM,LPARAM)' definition is marked dllimport
    Line 102:function 'void InstallHook()' definition is marked dllimport
    Line 108:fucntion 'void RemoveHook()' definition is marked dllimport
    [Build Error] [dllmain.o] Error 1
    i did not write this thus I do not take credit for it, it is hijacking the GetVersionExA from a install file I am trying to isntall but it was made before winxpx64 came out thus, the version numbers are not up to date so this will let it install, i would contact the writer of the dll, ubt alas, he is not online.
    Any help would be greatly appreciated. Any other needed information I will be hjappy to give, the ddraw.h is in "" because I had to copy->paste it into the project folder to get detected.
    Last edited by bikr692002; 02-21-2006 at 10:16 AM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If you compile a DLL you should export functions (dllexport), not import them (dllimport).

    I dunno what TESTDLL_API is, but if it isn't 'extern "C" ' then you should prolly add it
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    What do you mean?

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Show us the definition of TESTDLL_API.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Ok,
    Code:
    LIBRARY TESTDLL
    DESCRIPTION "DirectDraw Hooking Test DLL."
    SECTIONS .HookSection READ WRITE SHARED
    EXPORTS
    	InstallHook
    	RemoveHook
    	HookProc
    and heres testdll.h
    Code:
    // TestDLL header file.
    #ifdef TESTDLL_EXPORTS
    #define TESTDLL_API __declspec(dllexport)
    #else
    #define TESTDLL_API __declspec(dllimport)
    #endif
    
    TESTDLL_API LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam);
    
    TESTDLL_API void InstallHook();
    TESTDLL_API void RemoveHook();

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    #define TESTDLL_EXPORTS before including the header.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Ok, thanks, tomorrow i'll try it out, I'm at my moms rigth now and my computer is not here

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Quote Originally Posted by Tonto
    #define TESTDLL_EXPORTS before including the header.
    In which file? The main dll?

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Ok, now I have it down to 2 errors, thanks ^.^
    It is Line 38:Excess elements in aggregate initializer
    and [Build Error] [dllmain.o] Error 1
    Heres the line of code, lines 23->38
    Code:
    enum
    {
        gFN_GetVersion = 0,
    	gFN_GetVersionExA = 1
    };
    
    SDLLHook gHook = 
    {
        "KERNEL32.DLL",
        false, NULL,		// Default hook disabled, NULL function pointer.
        {
            { "GetVersion", MyGetVersion },
    		{ "GetVersionExA", MyGetVersionEx },
            { NULL, NULL }
        }
    };

  10. #10
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Here's the definition of SDLLHook I found:

    Code:
    struct SDLLHook
    {
        // DLL name array, terminated with a NULL field;
        char* Name[MAX_DLL_NUMBER];
    
        // Set true to call the default for all non-hooked
        // functions before they are executed.
        bool UseDefault;
        void* DefaultFn;
    
        // Function hook array.  Terminated with a NULL Name field.
        SFunctionHook Functions[];
    };
    To initialize via an aggregate initializer;

    Code:
    SDLLHook TextHook = 
    {
        {
           "KERNEL32.DLL",
           NULL
        },
        false, NULL, 
        {
            { "GetVersion", MyGetVersion },
            { "GetVersionExA", MyGetVersionEx },
            { NULL, NULL }
        }
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DLL Function / Load Library Problem
    By cboard_member in forum Windows Programming
    Replies: 5
    Last Post: 12-10-2005, 10:11 AM
  2. Replies: 1
    Last Post: 09-18-2005, 09:06 PM
  3. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM
  4. std::string vs char* DLL problem
    By aker_y3k in forum C++ Programming
    Replies: 13
    Last Post: 10-02-2002, 09:05 AM
  5. VCL and DLL class problem
    By borland_man in forum C++ Programming
    Replies: 1
    Last Post: 02-13-2002, 11:07 AM