Article View: borland.public.cppbuilder.nativeapi
Article #4816DLL <-> Form communication problem (Gloobal Key hook)
From: Stan R.
Date: Fri, 25 Nov 2005 06:28
Date: Fri, 25 Nov 2005 06:28
140 lines
4893 bytes
4893 bytes
I'm trying to write an application that creates a global key hook. I
have the MainForm project and the DLL project in a project group. I
can't seem to get this working right.
If I uncomment the ShowMessage in the KeyboardProc function, it comes up
on keystroke TWICE. Why is this?
Or am I going abou this the wrong way?
What I'm trying to do is cature keystrokes globally and trigger an event
in my form.
Thanks for any help.
KeyCap_MainForm.h:
[...]
publuc:
[...]
bool __fastcall OnSystemKeyStroke(char Key);
KeyCap_MainForm.cpp:
[...]
typedef bool (__stdcall *DLLFunc_SetHook) (TKeyCapForm *KeyCapForm);
typedef bool (__stdcall *DLLFunc_UnsetHook)(void);
DLLFunc_SetHook SetHook;
DLLFunc_UnsetHook UnsetHook;
HINSTANCE DllHandle;
//------------------------------------------------------------------------------
__fastcall TKeyCapForm::TKeyCapForm(TComponent* Owner) // constructor
: TForm(Owner)
{
// LOAD DLL
DllHandle = LoadLibrary("KeyCap.dll");
if (DllHandle != NULL) {
SetHook = (DLLFunc_SetHook)GetProcAddress(DllHandle, "SetHook");
// To set the keyboard hook
if (SetHook != NULL) {
if (!SetHook(this)) ShowMessage("Can't set hook");
}
}
}
//------------------------------------------------------------------------------
__fastcall TKeyCapForm::~TKeyCapForm() { // Destructor
if (DllHandle != NULL) {
UnsetHook = (DLLFunc_UnsetHook)GetProcAddress(DllHandle,
"UnsetHook");
// To release the keyboard hook
if (UnsetHook != NULL) {
if (!UnsetHook()) ShowMessage("Can't unset hook");
}
}
}
//------------------------------------------------------------------------------
bool __fastcall TKeyCapForm::OnSystemKeyStroke(char Key) {
/*if (wParam == VK_SNAPSHOT) {
// Print Screen key caught, do something
// return 0 to allow Windows to finish processing the key
// or return non-zero to prevent further processing
return 1;
}
else return 0;*/
// Add a line to the Memo.
Out->Lines->Add(Key);
return 1;
}
//------------------------------------------------------------------------------
KeyCap_DLL.cpp:
#include <vcl.h>
#include <windows.h>
#include "KeyCap_MainForm.h"
#pragma hdrstop
#pragma argsused
HINSTANCE DllInstance;
HHOOK KeyHookHandle;
TKeyCapForm *Form;
//------------------------------------------------------------------------------
extern "C" {
BOOL __declspec(dllexport) __stdcall SetHook(TKeyCapForm
*KeyCapForm);
BOOL __declspec(dllexport) __stdcall UnsetHook(void);
}
//------------------------------------------------------------------------------
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam){
if (code < 0) return CallNextHookEx(KeyHookHandle, code, wParam,
lParam);
//ShowMessage("Test [" + String((char)wParam) + "]");
// Use TKeyCapForm::OnSystemKeyStroke function to handle this !
return Form->OnSystemKeyStroke((char)wParam);
}
//------------------------------------------------------------------------------
BOOL __stdcall SetHook(TKeyCapForm *KeyCapForm) {
KeyHookHandle = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc,
DllInstance, 0);
Form = KeyCapForm;
return (KeyHookHandle != NULL);
}
//------------------------------------------------------------------------------
BOOL __stdcall UnsetHook(void) {
BOOL ret = TRUE;
if (KeyHookHandle != NULL) {
ret = UnhookWindowsHookEx(KeyHookHandle);
if (ret == TRUE) KeyHookHandle = NULL;
}
return ret;
}
//------------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*
lpReserved)
{
DllInstance = hinst;
return 1;
}
//------------------------------------------------------------------------------
--
Stan R.
Technical Details
Message-ID:
<4386aee1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed.hal-mli.net!feeder1.hal-mli.net!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,51028bdae794de7f,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Stan R." <none@none> Newsgroups: borland.public.cppbuilder.nativeapi Subject: DLL <-> Form communication problem (Gloobal Key hook) Date: Thu, 24 Nov 2005 22:28:02 -0800 Lines: 142 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 66.234.219.101 Message-ID: <4386aee1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1132900065 66.234.219.101 (24 Nov 2005 22:27:45 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed.hal-mli.net!feeder1.hal-mli.net!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail