BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

DLL <-> Form communication problem (Gloobal Key hook)

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Stan R.
Guest





PostPosted: Fri Nov 25, 2005 2:28 am    Post subject: DLL <-> Form communication problem (Gloobal Key hook) Reply with quote




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? Also, when that line is uncommented, I
see the character added to my memo (I'm only using that for testing as
I'm trying to learn how to do this right.) But if I comment it, I dont
see anything added to the the memo.

Or am I going abou this the wrong way?

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.


Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Fri Nov 25, 2005 8:05 am    Post subject: Re: DLL <-> Form communication problem (Gloobal Key hook) Reply with quote




"Stan R." <none@none> wrote


Quote:
I
can't seem to get this working right.

That is because your code is not implementing the hook correctly. You must
export the callback function, and you must share the DLL's hook-related data
across multiple instances of your DLL.

Try this code instead:

--- KeyCap_MainForm.h ---

#include "KeyCap_DLL.h"

class TKeyCapForm : public TForm
{
//...
private:
MESSAGE void __fastcall OnSystemKeyStroke(TMessage &Message);
//...
protected:
virtual void __fastcall CreateWindowHandle(const TCreateParams
&Params);
virtual void __fastcall DestroyWindowHandle(void);
//...
public:
//...
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_SYSKEYSTROKE, TMessage,
OnSystemKeyStroke)
END_MESSAGE_MAP(TForm)
};


--- KeyCap_MainForm.cpp ---

#include "KeyCap_MainForm.h"

DLLFunc_InstallHook lpInstallHook = NULL;
DLLFunc_UninstallHook lpUninstallHook = NULL;
DLLFunc_SetHookWnd lpSetHookWnd = NULL;
HINSTANCE DllHandle = NULL;

__fastcall TKeyCapForm::TKeyCapForm(TComponent* Owner)
: TForm(Owner)
{
DllHandle = LoadLibrary("KeyCap.dll");
if( DllHandle != NULL )
{
lpInstallHook = (DLLFunc_InstallHook) GetProcAddress(DllHandle,
"InstallHook");
lpUninstallHook = (DLLFunc_UninstallHook)
GetProcAddress(DllHandle, "UninstallHook");
lpSetHookWnd = (DLLFunc_SetHookWnd) GetProcAddress(DllHandle,
"SetHookWnd");

if( (lpInstallHook != NULL) && (lpUninstallHook != NULL) &&
(lpSetHookWnd != NULL) )
{
if( lpInstallHook() )
return;

ShowMessage("Can't install hook");
}
else
ShowMessage("Can't load hook functions");

FreeLibrary(DllHandle);
DllHandle = NULL;
}
else
ShowMessage("Can't load hook dll");
}

__fastcall TKeyCapForm::~TKeyCapForm()
{
if( DllHandle != NULL )
{
if( !lpUninstallHook() )
ShowMessage("Can't uninstall hook");

FreeLibrary(DllHandle);
}
}

void __fastcall TKeyCapForm::CreateWindowHandle(const TCreateParams
&Params)
{
TForm::CreateWindowHandle(Params);
if( HandleAllocated() && (lpSetHookWnd != NULL) )
lpSetHookWnd(Handle);
}

void __fastcall TKeyCapForm::DestroyWindowHandle(void)
{
TForm::DestroyWindowHandle();
if( lpSetHookWnd != NULL )
lpSetHookWnd(NULL);
}

void __fastcall TKeyCapForm::OnSystemKeyStroke(TMEssage &Message)
{
// return 0 to allow Windows to finish processing the key
// or return non-zero to prevent further processing

/*
if( Message.LParam == VK_SNAPSHOT )
{
// Print Screen key caught, do something
Message.Result = 1;
}
*/

// Add a line to the Memo.
Out->Lines->Add("Key: " + AnsiString(Message.WParam) + ", Flags: " +
AnsiString(Message.LParam));

Message.Result = 0;
}


--- KeyCap_DLL.h ---

#ifndef KeyCapDLLH
#define KeyCapDLLH

#define WM_SYSKEYSTROKE (WM_USER + 100)

#ifdef BUILDING_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C" {
#endif

EXPORT BOOL __stdcall InstallHook(void);
EXPORT BOOL __stdcall UninstallHook(void);
EXPORT void __stdcall SetHookWnd(HWND hwnd);
EXPORT LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM
lParam);

typedef BOOL (__stdcall *DLLFunc_InstallHook) (void);
typedef BOOL (__stdcall *DLLFunc_InstallHook)(void);
typedef void (__stdcall *DLLFunc_SetHookWnd)(HWND);

#ifdef __cplusplus
}
#endif

#endif


--- KeyCap_DLL.cpp ---

#include <windows.h>
#pragma hdrstop

#define BUILDING_DLL
#include "KeyCap_DLL.h"

typedef struct
{
HWND hWnd;
HHOOK hHook;
} THookInfo, *PHookInfo;

HINSTANCE DllInstance = NULL;
HANDLE hMapping = NULL;
PHookInfo pHookInfo = NULL;

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*
lpReserved)
{
switch( reason )
{
case DLL_PROCESS_ATTACH:
{
DllInstance = hinst;

hMapping = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
sizeof(THookInfo),
TEXT("KeyCapHookInfo"));

if( hMapping != NULL )
{
bool bCreated = (GetLastError() !=
ERROR_ALREADY_EXISTS);

pHookInfo = (PHookInfo) MapViewOfFile(
hMapping,
FILE_MAP_WRITE,
0, 0, 0);

if( pHookInfo != NULL )
{
if( bCreated == true )
{
pHookInfo->hWnd = NULL;
pHookInfo->hHook = NULL;
}
return 1;
}

CloseHandle(hMapping);
hMapping = NULL;
}

break;
}

case DLL_PROCESS_DETACH:
{
if( pHookInfo )
UnmapViewOfFile(pHookInfo);
if( hMapping )
CloseHandle(hMapping);
break;
}
}

return 0;
}

BOOL __stdcall InstallHook(void)
{
if( pHookInfo->hHook == NULL )
pHookInfo->hHook = SetWindowsHookEx(WH_KEYBOARD,
(HOOKPROC)KeyboardProc, DllInstance, 0);
return (pHookInfo->hHook != NULL);
}

BOOL __stdcall UninstallHook(void)
{
if( pHookInfo->hHook != NULL )
{
if( UnhookWindowsHookEx(pHookInfo->hHook) )
pHookInfo->hHook = NULL;
}
return (pHookInfo->hHook == NULL);
}

void __stdcall SetHookWnd(HWND hwnd)
{
pHookInfo->hWnd = hwnd;
}

LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
if( (code == HC_ACTION) && (pHookInfo->hWnd != NULL) )
{
int iRet = SendMessage(pHookInfo->hWnd, WM_SYSKEYSTROKE ,
wParam, lParam);
if( iRet != 0 )
return iRet;
}
return CallNextHookEx(pHookInfo->hHook, code, wParam, lParam);
}


Gambit



Back to top
Stan R.
Guest





PostPosted: Fri Nov 25, 2005 11:26 pm    Post subject: Re: DLL <-> Form communication problem (Gloobal Key hook) Reply with quote



Remy Lebeau (TeamB) wrote:
Quote:
"Stan R." <none@none> wrote in message
news:438676bf (AT) newsgroups (DOT) borland.com...

I
can't seem to get this working right.

That is because your code is not implementing the hook correctly.
You must export the callback function, and you must share the DLL's
hook-related data across multiple instances of your DLL.

Try this code instead:
[snip code]


Thank you very much.

I only ask one more thing, I would like to know why the code I posted
was kinda partially working though? I just want to under stand this
more.

What I saw happening was, when it would worked, it would seem to trigger
twice for each key stroke. It seemed to stop working when my main form
window lost focus, though it did work at least once when I switched to
another appand pressed a key and stopped workign after that.

Again, I just want to have a greater understanding for this stuff. It's
godo to be given the correct way, but it's also better to know why
exactly the not so good way /was/ not so good :)

Thank you.



Back to top
Stan R.
Guest





PostPosted: Sat Nov 26, 2005 12:46 am    Post subject: Re: DLL <-> Form communication problem (Gloobal Key hook) Reply with quote

Remy Lebeau (TeamB) wrote:
Quote:
"Stan R." <none@none> wrote in message
news:438676bf (AT) newsgroups (DOT) borland.com...

I
can't seem to get this working right.

That is because your code is not implementing the hook correctly.
You must export the callback function, and you must share the DLL's
hook-related data across multiple instances of your DLL.

Try this code instead:

--- KeyCap_MainForm.h ---

#include "KeyCap_DLL.h"

class TKeyCapForm : public TForm
{
//...
private:
MESSAGE void __fastcall OnSystemKeyStroke(TMessage &Message);
//...
protected:
virtual void __fastcall CreateWindowHandle(const TCreateParams
&Params);
virtual void __fastcall DestroyWindowHandle(void);
//...
public:
//...
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_SYSKEYSTROKE, TMessage,
OnSystemKeyStroke)
END_MESSAGE_MAP(TForm)
};


--- KeyCap_MainForm.cpp ---

#include "KeyCap_MainForm.h"

DLLFunc_InstallHook lpInstallHook = NULL;
DLLFunc_UninstallHook lpUninstallHook = NULL;
DLLFunc_SetHookWnd lpSetHookWnd = NULL;
HINSTANCE DllHandle = NULL;

__fastcall TKeyCapForm::TKeyCapForm(TComponent* Owner)
: TForm(Owner)
{
DllHandle = LoadLibrary("KeyCap.dll");
if( DllHandle != NULL )
{
lpInstallHook = (DLLFunc_InstallHook)
GetProcAddress(DllHandle, "InstallHook");
lpUninstallHook = (DLLFunc_UninstallHook)
GetProcAddress(DllHandle, "UninstallHook");
lpSetHookWnd = (DLLFunc_SetHookWnd)
GetProcAddress(DllHandle, "SetHookWnd");

if( (lpInstallHook != NULL) && (lpUninstallHook != NULL) &&
(lpSetHookWnd != NULL) )
{
if( lpInstallHook() )
return;

ShowMessage("Can't install hook");
}
else
ShowMessage("Can't load hook functions");

FreeLibrary(DllHandle);
DllHandle = NULL;
}
else
ShowMessage("Can't load hook dll");
}

__fastcall TKeyCapForm::~TKeyCapForm()
{
if( DllHandle != NULL )
{
if( !lpUninstallHook() )
ShowMessage("Can't uninstall hook");

FreeLibrary(DllHandle);
}
}

void __fastcall TKeyCapForm::CreateWindowHandle(const TCreateParams
&Params)
{
TForm::CreateWindowHandle(Params);
if( HandleAllocated() && (lpSetHookWnd != NULL) )
lpSetHookWnd(Handle);
}

void __fastcall TKeyCapForm::DestroyWindowHandle(void)
{
TForm::DestroyWindowHandle();
if( lpSetHookWnd != NULL )
lpSetHookWnd(NULL);
}

void __fastcall TKeyCapForm::OnSystemKeyStroke(TMEssage &Message)
{
// return 0 to allow Windows to finish processing the key
// or return non-zero to prevent further processing

/*
if( Message.LParam == VK_SNAPSHOT )
{
// Print Screen key caught, do something
Message.Result = 1;
}
*/

// Add a line to the Memo.
Out->Lines->Add("Key: " + AnsiString(Message.WParam) + ",
Flags: " + AnsiString(Message.LParam));

Message.Result = 0;
}


--- KeyCap_DLL.h ---

#ifndef KeyCapDLLH
#define KeyCapDLLH

#define WM_SYSKEYSTROKE (WM_USER + 100)

#ifdef BUILDING_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C" {
#endif

EXPORT BOOL __stdcall InstallHook(void);
EXPORT BOOL __stdcall UninstallHook(void);
EXPORT void __stdcall SetHookWnd(HWND hwnd);
EXPORT LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam,
LPARAM lParam);

typedef BOOL (__stdcall *DLLFunc_InstallHook) (void);
typedef BOOL (__stdcall *DLLFunc_InstallHook)(void);
^^^^^^^^^^^

Oops! (Yeah I caught and fixed that :-)

Quote:
typedef void (__stdcall *DLLFunc_SetHookWnd)(HWND);

#ifdef __cplusplus
}
#endif

#endif


--- KeyCap_DLL.cpp ---

#include <windows.h
#pragma hdrstop

#define BUILDING_DLL
#include "KeyCap_DLL.h"

typedef struct
{
HWND hWnd;
HHOOK hHook;
} THookInfo, *PHookInfo;

HINSTANCE DllInstance = NULL;
HANDLE hMapping = NULL;
PHookInfo pHookInfo = NULL;

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason,
void* lpReserved)
{
switch( reason )
{
case DLL_PROCESS_ATTACH:
{
DllInstance = hinst;

hMapping = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
sizeof(THookInfo),
TEXT("KeyCapHookInfo"));

if( hMapping != NULL )
{
bool bCreated = (GetLastError() !=
ERROR_ALREADY_EXISTS);

pHookInfo = (PHookInfo) MapViewOfFile(
hMapping,
FILE_MAP_WRITE,
0, 0, 0);

if( pHookInfo != NULL )
{
if( bCreated == true )
{
pHookInfo->hWnd = NULL;
pHookInfo->hHook = NULL;
}
return 1;
}

CloseHandle(hMapping);
hMapping = NULL;
}

break;
}

case DLL_PROCESS_DETACH:
{
if( pHookInfo )
UnmapViewOfFile(pHookInfo);
if( hMapping )
CloseHandle(hMapping);
break;
}
}

return 0;
}

BOOL __stdcall InstallHook(void)
{
if( pHookInfo->hHook == NULL )
pHookInfo->hHook = SetWindowsHookEx(WH_KEYBOARD,
(HOOKPROC)KeyboardProc, DllInstance, 0);
return (pHookInfo->hHook != NULL);
}

BOOL __stdcall UninstallHook(void)
{
if( pHookInfo->hHook != NULL )
{
if( UnhookWindowsHookEx(pHookInfo->hHook) )
pHookInfo->hHook = NULL;
}
return (pHookInfo->hHook == NULL);
}

void __stdcall SetHookWnd(HWND hwnd)
{
pHookInfo->hWnd = hwnd;
}

LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM
lParam) {
if( (code == HC_ACTION) && (pHookInfo->hWnd != NULL) )
{
int iRet = SendMessage(pHookInfo->hWnd, WM_SYSKEYSTROKE ,
wParam, lParam);
if( iRet != 0 )
return iRet;
}
return CallNextHookEx(pHookInfo->hHook, code, wParam, lParam);
}


Gambit

Thank Gambit. I used your code but I don't see the event being being
triggered in the form. When I close the main window I get an AV (the
standard "read of address ..." and it drops me in the dll's cpp file but
doesn't show where/what caused it. If I put a break point in the
callback function it stops there and as soon as I step is AV's.

My code is just as you gave it (fishing out the typing errors, like the
double `typedef BOOL (__stdcall *DLLFunc_InstallHook)(void)` above. I
even tried including the dll's lib file into the mainform's project (---
I have both the mainform and the dll projects open together as a project
group. Not sure if this is the best way to work on both at the same time
but it seems to work. ---)

I'm using BCB5, on XP SP2, if that help at all.

--
Stan



Back to top
Stan R.
Guest





PostPosted: Sat Nov 26, 2005 12:53 am    Post subject: Re: DLL <-> Form communication problem (Gloobal Key hook) Reply with quote

Stan R. wrote:
Quote:
Thank Gambit. I used your code but I don't see the event being being
triggered in the form. When I close the main window I get an AV (the
standard "read of address ..." and it drops me in the dll's cpp file
but doesn't show where/what caused it. If I put a break point in the
callback function it stops there and as soon as I step is AV's.

My code is just as you gave it (fishing out the typing errors, like
the double `typedef BOOL (__stdcall *DLLFunc_InstallHook)(void)`
above. I even tried including the dll's lib file into the mainform's
project (--- I have both the mainform and the dll projects open
together as a project group. Not sure if this is the best way to work
on both at the same time but it seems to work. ---)

I'm using BCB5, on XP SP2, if that help at all.

Ok got some more info. It seems like CreateWindowHandle and
DestroyWindowHandle are not getting called for some reason. Look in the
docs they seem to have the right signature. CreateWindowHandle isn't
being called, and thus, the window handle in the dll isn't being set so
the message never gets sent (doesn't make it past the IF statement in
the call back.)

--
Stan



Back to top
Stan R.
Guest





PostPosted: Sat Nov 26, 2005 1:04 am    Post subject: Re: DLL <-> Form communication problem (Gloobal Key hook) Reply with quote

Stan R. wrote:
Quote:
Stan R. wrote:
Thank Gambit. I used your code but I don't see the event being being
triggered in the form. When I close the main window I get an AV (the
standard "read of address ..." and it drops me in the dll's cpp file
but doesn't show where/what caused it. If I put a break point in the
callback function it stops there and as soon as I step is AV's.

My code is just as you gave it (fishing out the typing errors, like
the double `typedef BOOL (__stdcall *DLLFunc_InstallHook)(void)`
above. I even tried including the dll's lib file into the mainform's
project (--- I have both the mainform and the dll projects open
together as a project group. Not sure if this is the best way to work
on both at the same time but it seems to work. ---)

I'm using BCB5, on XP SP2, if that help at all.

Ok got some more info. It seems like CreateWindowHandle and
DestroyWindowHandle are not getting called for some reason. Look in
the docs they seem to have the right signature. CreateWindowHandle
isn't being called, and thus, the window handle in the dll isn't
being set so the message never gets sent (doesn't make it past the IF
statement in the call back.)

Ok what the heck...
I put a ShowMessage("CreateWindowHandle"); in that function and when I
start the program it shows up, but my break point doesnt BREAK on the
function (I tried putting a break on the fuctions heading and anywhere
below in its body, but it wont break. I see the blue dots and all, and
the break remains red (it doesn't turn green-yellow, so it's a valid
break point it seems.)

What am I missing here?
--
Stan



Back to top
Thomas Maeder [TeamB]
Guest





PostPosted: Sat Nov 26, 2005 8:14 am    Post subject: Re: DLL <-> Form communication problem (Gloobal Key hook) Reply with quote


Please direct your browser at http://info.borland.com/newsgroups/ and
read the newsgroup guidelines. One of them asks us not to quote entire
posts we are following up to; instead, please trim the quotes to the
parts relevant for your reply. Thanks!
Back to top
Rudy Velthuis [TeamB]
Guest





PostPosted: Sat Nov 26, 2005 10:55 am    Post subject: Re: DLL <-> Form communication problem (Gloobal Key hook) Reply with quote

At 01:46:00, 26.11.2005, Stan R. wrote:

Quote:
Thank Gambit. I used your code but I don't see the event being being
triggered in the form. When I close the main window I get an AV (the
standard "read of address ..." and it drops me in the dll's cpp file
but doesn't show where/what caused it. If I put a break point in the
callback function it stops there and as soon as I step is AV's.

My code is just as you gave it (fishing out the typing errors, like the
double `typedef BOOL (__stdcall *DLLFunc_InstallHook)(void)` above. I
even tried including the dll's lib file into the mainform's project
(--- I have both the mainform and the dll projects open together as a
project group. Not sure if this is the best way to work on both at the
same time but it seems to work. ---)

I'm using BCB5, on XP SP2, if that help at all.

Please do not overquote.

http://blogs.teamb.com/rudyvelthuis/articles/7509.aspx

I cancelled your extreme overquote, but quoted your text to make replies
still possible.

--
Rudy Velthuis [TeamB] http://velthuis.homepage.t-online.de

"Tragedy is when I cut my finger. Comedy is when you walk into an open
sewer and die."
- Mel Brooks

Back to top
Eckstein C.
Guest





PostPosted: Sat Nov 26, 2005 7:31 pm    Post subject: Re: DLL <-> Form communication problem (Gloobal Key hook) Reply with quote

Rudy Velthuis [TeamB] wrote:
Quote:
At 01:46:00, 26.11.2005, Stan R. wrote:

Thank Gambit. I used your code but I don't see the event being being
triggered in the form. When I close the main window I get an AV (the
standard "read of address ..." and it drops me in the dll's cpp file
but doesn't show where/what caused it. If I put a break point in the
callback function it stops there and as soon as I step is AV's.

My code is just as you gave it (fishing out the typing errors, like
the double `typedef BOOL (__stdcall *DLLFunc_InstallHook)(void)`
above. I even tried including the dll's lib file into the mainform's
project (--- I have both the mainform and the dll projects open
together as a project group. Not sure if this is the best way to
work on both at the same time but it seems to work. ---)

I'm using BCB5, on XP SP2, if that help at all.

Please do not overquote.

http://blogs.teamb.com/rudyvelthuis/articles/7509.aspx

I cancelled your extreme overquote, but quoted your text to make
replies still possible.

Ok, both you and another [TeamB] member posted this same thing, but
offered no other help. I see nothing wrong with good guidelines, and
posting links to them but it looked like he quoted that code for a
reason. He had comments with in the body of that code and thats what his
post was about. On top of that, he reposted a combination of his posts,
without the code, almost half a day before you posts. I think you both
jumped the gun here.

I jsut don't want this group to become like microsoft's new groups. Very
hard to get good help there.



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Nov 28, 2005 6:17 pm    Post subject: Re: DLL <-> Form communication problem (Gloobal Key hook) Reply with quote


"Stan R." <none@none> wrote


Quote:
What I saw happening was, when it would worked, it would
seem to trigger twice for each key stroke.

I would expect that since the hook is called when the key is pressed down as
well as when it is released. The lParam value contains several flags that
tell you exactly what the key is actually doing at the time the hook is
called.

Quote:
It seemed to stop working when my main form window lost focus

Your hook was not truely global, so only your app could access it.. You did
not set it up properly to work with other applications.


Gambit



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Nov 28, 2005 6:21 pm    Post subject: Re: DLL <-> Form communication problem (Gloobal Key hook) Reply with quote


"Stan R." <none@none> wrote


Quote:
Ok got some more info. It seems like CreateWindowHandle
and DestroyWindowHandle are not getting called for some
reason.

That is not possible. Those are VCL methods that are directly tied to the
form's Handle property. CreateWindowHandle() is the method that is directly
responsible for allocating the HWND for that Handle property. The form
would not be able to function at all if CreateWindowHandle() were never
called.


Gambit



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.