🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
5 messages
5 total messages Started by qyte Mon, 04 Sep 2006 21:48
Establishing a Keyboard Hook.........
#2987
Author: qyte
Date: Mon, 04 Sep 2006 21:48
70 lines
2530 bytes
I have built the following DLL...

#include <vcl.h>
#include <windows.h>
#pragma hdrstop

#pragma argsused

#define UM_KEYB (WM_USER+11069)

static HHOOK hkb=NULL;
HWND Handle;

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
     return 1;
}
//---------------------------------------------------------------------------

__declspec(dllexport) LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)
{
     PostMessage(Handle,UM_KEYB,wParam,lParam);
     return  CallNextHookEx( hkb, nCode, wParam, lParam );
}

__declspec(dllexport) bool installhook(HWND hwnd)
{
     hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,HInstance,0);
     Handle = hwnd;
     return hkb;
}

__declspec(dllexport) bool UnHook()
{
      return UnhookWindowsHookEx(hkb);
}

and on the App now...

int y=0;
__declspec(dllimport) LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam);
__declspec(dllimport) bool installhook(HWND hwnd);
__declspec(dllimport) bool UnHook();

__fastcall TForm1::TForm1(TComponent* Owner)
         : TForm(Owner)
{
     installhook(Handle);
}

void __fastcall TForm1::WndProc(TMessage &Message)
{
         if(Message.Msg==UM_KEYB)
         {
             Caption = y++;
             Message.Result=true;
         }
         else
             TForm::WndProc(Message);
}

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
     UnHook();
}


Why is this not working? What on earth am i doing wrong?
It only captures keystrokes from within my application...
I Want a system-wide hook...
Re: Establishing a Keyboard Hook.........
#2995
Author: Remy Lebeau (TeamB)
Date: Tue, 05 Sep 2006 02:16
35 lines
2249 bytes
"qyte" <gyte@vivodinet.gr> wrote in message 
news:44fc9f3a@newsgroups.borland.com...

> I have built the following DLL...

You are trying to use a global hook.  Your DLL is going to be injected into 
every running process, and thus each process is going to be running its own 
copy of the DLL's variables.  Each process is going to have its own copy of 
your HHOOK and HWND variables, but only the process that originally installs 
the hook will have accurate values for them.  So you need to store your 
variables into a block of shared memory so that every process has equal 
access to those values.  Otherwise your hook will not work correctly.  Use 
CreateFileMapping() and MapViewOfFile() to set asside the block of shared 
memory.

Please go to http://www.deja.com and search through the newsgroup archives. 
I have posted sample code on this topic many times before.

> Why is this not working?

Because you did not set up the DLL so that all of the other processes can 
access the HHOOK and HWND values that the DLL uses.

> It only captures keystrokes from within my application...

Because only your application has access to the correct HHOOK and HWND 
variable values.

> I Want a system-wide hook...

Then you must store the HHOOK and HWND values into shared memory.


Gambit 
Re: Establishing a Keyboard Hook.........
#2988
Author: Bob Gonder
Date: Tue, 05 Sep 2006 06:40
27 lines
1740 bytes
qyte wrote:

>I have built the following DLL...
>
>#include <vcl.h>

Why include vcl.h? You don't show any vcl code in the dll.

>int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
>{

"hinst" is the module handle for this dll.

>__declspec(dllexport) bool installhook(HWND hwnd)
>{
>     hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,HInstance,0);

HInstance is probably the handle of the application, not the DLL,
which you need.

>Why is this not working? What on earth am i doing wrong?
>It only captures keystrokes from within my application...

Probably because you are installling the hook for the application
instance, not the DLL instance.

See also GetModuleHandle()
Re: Establishing a Keyboard Hook.........
#2989
Author: qyte
Date: Tue, 05 Sep 2006 09:46
42 lines
2415 bytes
Bob Gonder wrote:
> qyte wrote:
> 
>> I have built the following DLL...
>>
>> #include <vcl.h>
> 
> Why include vcl.h? You don't show any vcl code in the dll.
> 
>> int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
>> {
> 
> "hinst" is the module handle for this dll.
> 
>> __declspec(dllexport) bool installhook(HWND hwnd)
>> {
>>     hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,HInstance,0);
> 
> HInstance is probably the handle of the application, not the DLL,
> which you need.
> 
>> Why is this not working? What on earth am i doing wrong?
>> It only captures keystrokes from within my application...
> 
> Probably because you are installling the hook for the application
> instance, not the DLL instance.
> 
> See also GetModuleHandle()
> 
> 

No you are wrong... :(
I Tested it and HInstance returns the same exact handle as
the LoadLibrary call returns from within the main program...
Apart from that the HInstance within the main app is COMPLETELY
different from that one and the one that LoadLibrary returns
obviously... I will test it anyway the GetModuleHandle() method
anyway just in case... :(

I was rather hasty in my previous post... The code works GREAT
but only if you are interested in catching keyboard strokes within
your application only, which is APPARENTLY UNACCEPTABLE in my case...
Re: Establishing a Keyboard Hook.........
#2990
Author: Ed Mulroy
Date: Tue, 05 Sep 2006 11:31
56 lines
2610 bytes
Ok, then try changing from this:

----------------------
__declspec(dllexport) bool installhook(HWND hwnd)
{
     hkb = SetWindowsHookEx(
         WH_KEYBOARD,
         (HOOKPROC) KeyboardProc,
         HInstance,
        0);

     Handle = hwnd;
     return hkb;
}

----------------------

To this:

----------------------
// variable created by the compiler's startup code
// instance handle/module handle of the DLL, the same
// value as is passed as a calling arg to DllEntryPoint

extern HINSTANCE _hInstance;

__declspec(dllexport) bool installhook(HWND hwnd)
{
     hkb = SetWindowsHookEx(
         WH_KEYBOARD,
         (HOOKPROC) KeyboardProc,
         _hInstance,
        0);

     Handle = hwnd;
     return hkb;
}

----------------------

.  Ed

> qyte wrote in message
> news:44fd4773$1@newsgroups.borland.com...
>
> No you are wrong... :(
> I Tested it and HInstance returns the same exact handle as
> the LoadLibrary call returns from within the main program...
> Apart from that the HInstance within the main app is COMPLETELY
> different from that one and the one that LoadLibrary returns
> obviously... I will test it anyway the GetModuleHandle() method
> anyway just in case... :(
>
> I was rather hasty in my previous post... The code works GREAT
> but only if you are interested in catching keyboard strokes within
> your application only, which is APPARENTLY UNACCEPTABLE in my case...
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads