Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i am registered here for long time but i was just following some tutorials and not activate with comments or new topics. I have a problem that i can't resolve for a couple of days. i made an application that install a global hook for keyboard but it can't hook keyboard msg in console application and browsers. I need some help to figure out the problem and i think i hope i can find this help here.
dll code is copiled with GNU GCC compiler and codeblocks as ide:
//dllmain
#include "main.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <cstdio>
#include <fstream>


using namespace std;

#ifdef __cplusplus
extern "C"
{
#endif

void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}




LRESULT CALLBACK DLL_EXPORT KeyboardProc(int code, WPARAM wParam, LPARAM lParam) {
    SomeFunction("test"); //here is just an example not all code in this function the big ideea is that this callback is not called when a console app or a browser application have focus
    LRESULT RetVal = CallNextHookEx( 0, code, wParam, lParam );
   return RetVal;
}


BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{

    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            // attach to process
            // return FALSE to fail DLL load
            break;

        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            // attach to thread
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}


#ifdef __cplusplus
}
#endif


//main.h
#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

/*  To use this exported function of dll, include this header
*  in your project.
*/

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C"
{
#endif

void DLL_EXPORT SomeFunction(const LPCSTR sometext);

LRESULT CALLBACK DLL_EXPORT KeyboardProc(
    int code,
    WPARAM wParam,
    LPARAM lParam
);
#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__

</windows.h></fstream></cstdio></windows.h>



hook set up code is compiled with MS VS 2008:
//those 3 are declared in global namespace
HHOOK hhandle;
HMODULE dllHandle;
HOOKPROC hookProcAddress;


//hook setup
    dllHandle = LoadLibrary(L"hook.dll");
   if (!dllHandle) cout << "LoadLibrary Call Failed, Error number: " << GetLastError() << endl;
   hookProcAddress = (HOOKPROC)GetProcAddress(dllHandle, "KeyboardProc@12");
   if (!hookProcAddress)cout << "GetProcAddress Call Failed, Error number: " << GetLastError() << endl;
   hhandle = SetWindowsHookEx(WH_KEYBOARD, hookProcAddress, dllHandle, 0);
   if (!hhandle) cout << "SetWindowsHookEx Call Failed, Error number: " << GetLastError() << endl;


i am using windows vista x32 ultimate edition

le: nobody can help me?
Posted
Updated 12-Oct-11 15:49pm
v3
Comments
Sergey Alexandrovich Kryukov 11-Oct-11 18:40pm    
Explain what's the problem, exactly. Did you use a separate DLL to install the hook? A global hook requires that.
--SA
vlasceanu 18-Oct-11 15:36pm    
yes i used a separate dll for hook. first code block i posted is the code of the dll itself. i don't understand why when internet explorer have focus i don't get any key event.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900