Link to home
Start Free TrialLog in
Avatar of atomic80
atomic80

asked on

KeyBoard Hook with Borland C++ Builder

Hello,

sorry for my bad english i am from germany.
Now my question.
Can anybody say me how i can write a simple program in "Borland C++ Builder" that count the key strokes in the system ?

Thanks
Avatar of Diplomat
Diplomat
Flag of United States of America image

Here's is the link for a simple example of keyboard hook program. It's written in Visual C++ V6 but I hope you can adapt it to C++ Builder:

https://www.experts-exchange.com/questions/20453601/Getting-keyboard-input-from-unfocussed-windows.html
Avatar of atomic80
atomic80

ASKER

Yes, it works in C++ Builder!
I have tested so many other samples but this works ;-)

Thanks
It's good to hear you can make it work. If you don't have additional question you can close this question by accepting my comments as answer. In the future I hope to help you with any additional question.
I have one question more ;-)

This is my source code:

#pragma data_seg(".shared")
HHOOK hHook  = NULL;
#pragma data_seg()
#pragma comment(linker,"/SECTION:.shared,RWS")

HWND      g_hWnd = NULL;
HINSTANCE g_hDll = NULL;


//###########################################################################
LRESULT CALLBACK ShellProc(int nCode, WPARAM wParam, LPARAM lParam)
{
  if(nCode < 0) CallNextHookEx(hHook, nCode, wParam, lParam);

  if(nCode == HSHELL_WINDOWCREATED)
  {
    SendMessage(g_hWnd, WM_USER, 0, 0);
  }

  return CallNextHookEx(hHook, nCode, wParam, lParam);
}
//---------------------------------------------------------------------------


//###########################################################################
bool _export SetHooks(HWND hwnd)
{
  if(hHook == NULL)
  {
    g_hWnd = hwnd;
    hHook = SetWindowsHookEx(WH_SHELL, (HOOKPROC)ShellProc, g_hDll, 0);
    if(hHook == NULL) return false;
  }
  return true;
}
//---------------------------------------------------------------------------


//###########################################################################
void _export RemoveHooks()
{
  if(hHook != NULL)
  {
    UnhookWindowsHookEx(hHook);
    hHook = NULL;
  }
}
//---------------------------------------------------------------------------


//###########################################################################
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
  switch(reason)
  {
    case DLL_PROCESS_ATTACH:
      g_hDll = hinst;
      return DisableThreadLibraryCalls(g_hDll);
    case DLL_PROCESS_DETACH:
      RemoveHooks();
    default:
      return TRUE;
  }
}
//---------------------------------------------------------------------------


It all works but i have a problem with SendMessage.
I will send a Message to my program when a event occurse but it do not work.
ASKER CERTIFIED SOLUTION
Avatar of Diplomat
Diplomat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello,

here the source from the sample:

#include "stdafx.h"
#include "keydll3.h"

#pragma data_seg(".HOOKDATA")//Shared data among all instances.
HHOOK hook = NULL;
HWND hwnd = NULL;
#pragma data_seg()

#pragma comment(linker, "/SECTION:.HOOKDATA,RWS")//linker directive

HINSTANCE hinstance = NULL;

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                          )
{
    switch (ul_reason_for_call)
     {
          case DLL_PROCESS_ATTACH:
          case DLL_THREAD_ATTACH:
          case DLL_THREAD_DETACH:
          case DLL_PROCESS_DETACH:
               break;
    }

     hinstance = (HINSTANCE)hModule;
     hook = NULL;

    return TRUE;
}


KEYDLL3_API void installhook(HWND h)
{
     hook = NULL;
     hwnd = h;
     hook = SetWindowsHookEx(WH_KEYBOARD,hookproc,hinstance,NULL);
     if(hook==NULL)
          MessageBox(NULL,"Unable to install hook","Error!",MB_OK);
}

KEYDLL3_API void removehook()
{
     UnhookWindowsHookEx(hook);
}

KEYDLL3_API LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam)
{
     if(ncode>=0)
     {
          if((lparam & 0x80000000) == 0x00000000)//Check whether key was pressed(not released).
          {
               hwnd = FindWindow("#32770","Keylogger Exe");//Find application window handle
               PostMessage(hwnd,WM_USER+755,wparam,lparam);//Send info to app Window.
          }
     }
     return ( CallNextHookEx(hook,ncode,wparam,lparam) );//pass control to next hook in the hook chain.
}

This sample is bad.
You give installhook the window - handle and the function save it but by any call of hookproc the handle is overwrited by FindWindow. ?????
Why i give the handle when the hookproc - function generates it self ?
I found the error!
SendMessage don't work but PostMessage!
And the FindWindow do not need. Bad!

Thanks