?
Solved

KeyBoard Hook with Borland C++ Builder

Posted on 2003-02-24
8
Medium Priority
?
3,091 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:atomic80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 3

Expert Comment

by:Diplomat
ID: 8014813
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
 

Author Comment

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

Thanks
0
 
LVL 3

Expert Comment

by:Diplomat
ID: 8018179
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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:atomic80
ID: 8019111
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
 
LVL 3

Accepted Solution

by:
Diplomat earned 200 total points
ID: 8020437
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
 

Author Comment

by:atomic80
ID: 8025888
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
 

Author Comment

by:atomic80
ID: 8025980
I found the error!
SendMessage don't work but PostMessage!
0
 

Author Comment

by:atomic80
ID: 8025994
And the FindWindow do not need. Bad!

Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question