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
atomic80Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DiplomatCommented:
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:

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20453601.html
0
atomic80Author Commented:
Yes, it works in C++ Builder!
I have tested so many other samples but this works ;-)

Thanks
0
DiplomatCommented:
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

atomic80Author Commented:
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.
0
DiplomatCommented:
Here's is another link regarding Message Hooks:

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20455827.html

In the above example, the key data is written to a file. But I believe you can substitute it with another message.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
atomic80Author Commented:
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 ?
0
atomic80Author Commented:
I found the error!
SendMessage don't work but PostMessage!
0
atomic80Author Commented:
And the FindWindow do not need. Bad!

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.