使用SetWindowsHookEx的C ++仅适用于添加了奇怪的vcl代码。在BCB2009

时间:2012-09-05 12:23:17

标签: c++ c++builder setwindowshookex c++builder-2007

使用SetWindowsHookEx

我有一个奇怪的情况

我有一个bcb 2009项目,上面有一个表格和一份备忘录。

在创建中我们加载Dll并将函数处理程序附加到两侧。

这个想法是当按键板被击中时,备忘录框中会出现一条消息,当发生鼠标事件时,备忘录框中会出现另一个文本。

奇怪的是,当我从调试信息中清除代码时,它停止工作。这意味着钩子被触发一次而不是结束。 在调试中,我使用一些VCL TStringList将密钥stokes数据记录到磁盘。玩这个代码我终于通过添加

检测到了

[代码]

TList* lList = new TList();
delete lList;

对于每个钩子函数(键盘,鼠标),代码都会再次运行。

我的代码中有什么问题我必须这样做?

这是15年来我第一次制作dll。所以它可以是创建dll或导出函数的真正基础。

欢迎提出任何建议。

问候

JVDN

一些新的附加信息:

[已解决]我的目标是win XP嵌入式。我的应用程序创建一个错误,通过Windows关闭资源管理器。并且钩子在xp中不是全局工作,而是仅在本地工作。但它正在开发我的开发平台win 7 x64全局类型和鼠标记事本导致应用程序中的消息。 [解决方案]将WH_KEYBOARD修改为WH_KEYBOARD_LL,将鼠标从WH_MOUSE修改为WH_MOUSE_LL,解决嵌入Windows XP的接收键和鼠标。

dll和应用程序都没有运行时库或包。

DLL代码

[代码]

//---------------------------------------------------------------------------

#include <vcl.h>
#include <windows.h>
#pragma hdrstop
//---------------------------------------------------------------------------
//   Important note about DLL memory management when your DLL uses the
//   static version of the RunTime Library:
//
//   If your DLL exports any functions that pass String objects (or structs/
//   classes containing nested Strings) as parameter or function results,
//   you will need to add the library MEMMGR.LIB to both the DLL project and
//   any other projects that use the DLL.  You will also need to use MEMMGR.LIB
//   if any other projects which use the DLL will be performing new or delete
//   operations on any non-TObject-derived classes which are exported from the
//   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
//   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,
//   the file BORLNDMM.DLL should be deployed along with your DLL.
//
//   To avoid using BORLNDMM.DLL, pass string information using "char *" or
//   ShortString parameters.
//
//   If your DLL uses the dynamic version of the RTL, you do not need to
//   explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------

typedef void __stdcall ( *typFn)(WPARAM,LPARAM);


static typFn gGUIProcessingKeyboard = NULL;

static HHOOK gGUIProcessingKeyboardHook = NULL;


static typFn gGUIProcessingMouse = NULL;;

static HHOOK gGUIProcessingMouseHook = NULL;


#pragma argsused

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

{

  return 1;

}

//---------------------------------------------------------------------------



extern "C"

{ __declspec(dllexport) void SetGUIProcessingKeyboard(typFn aHandle);

 __declspec(dllexport) void ReleaseGUIProcessingKeyboard(typFn aHandle);

 __declspec(dllexport) LRESULT CALLBACK wireKeyboardProc(int code, WPARAM wParam,LPARAM lParam);

 __declspec(dllexport) void SetKeyboardHookHandle(HHOOK aHook );


 __declspec(dllexport) void SetGUIProcessingMouse(typFn aHandle);

 __declspec(dllexport) void ReleaseGUIProcessingMouse(typFn aHandle);

 __declspec(dllexport) void SetMouseHookHandle(HHOOK aHook );

 __declspec(dllexport) LRESULT CALLBACK wireMouseProc(int code, WPARAM wParam,LPARAM lParam);




 /**
  * Set the keyboard loop back handle
  */

void SetGUIProcessingKeyboard(typFn aHandle)
{
  if (aHandle != gGUIProcessingKeyboard)
  {
    gGUIProcessingKeyboard = aHandle;
  }
}


 /**
  * Release the keyboard loop back handle
  */
void ReleaseGUIProcessingKeyboard(typFn aHandle)
{
  gGUIProcessingKeyboard = NULL;
}

/**
 * Set the handle used for tapping the Keyboard
 */
void SetKeyboardHookHandle(HHOOK aHook )
{
  gGUIProcessingKeyboardHook = aHook;
}


 /**
  * Tapping the keyboard from the other applications
  */
LRESULT CALLBACK wireKeyboardProc(int code, WPARAM wParam,LPARAM lParam)
{
    TList* lList = new TList();
    delete lList;
    if (code < 0) {
      return CallNextHookEx(gGUIProcessingKeyboardHook, code, wParam, lParam);
    }
    if (NULL != gGUIProcessingKeyboard)
    {
      gGUIProcessingKeyboard( wParam,lParam);
    }
    return CallNextHookEx(gGUIProcessingKeyboardHook, code, wParam, lParam);
}


 /**
  * Set the mouse loop back handle
  */

void SetGUIProcessingMouse(typFn aHandle)
{
  if (aHandle != gGUIProcessingMouse)
  {
    gGUIProcessingMouse = aHandle;
  }
}


 /**
  * Release the mouse loop back handle
  */

void ReleaseGUIProcessingMouse(typFn aHandle)
{
  gGUIProcessingMouse = NULL;
}


/**
 * Set the handle used for tapping the mouse
 */

void SetMouseHookHandle(HHOOK aHook )
{
  gGUIProcessingMouseHook = aHook;
}


 /**
  * Tapping the mouse from the other applications
  */

LRESULT CALLBACK wireMouseProc(int code, WPARAM wParam,LPARAM lParam)
{
    TList* lList = new TList();
    delete lList;
//  if (gGUIProcessingMouseHook != NULL)
//  {
    if (code < 0) {
      return CallNextHookEx(gGUIProcessingMouseHook, code, wParam, lParam);
    }
    if (NULL != gGUIProcessingMouse)
    {
      gGUIProcessingMouse( wParam,lParam);
    }
    return CallNextHookEx(gGUIProcessingMouseHook, code, wParam, lParam);
    //  }
    //  return 0;

    }

    } // extern C

这是应用程序。

[code cpp]

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "MonitoringToolMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

typedef void __stdcall ( __closure *typFn)(WPARAM,LPARAM);

TForm1 *Form1;
HHOOK TForm1::mHook = NULL;
typedef void __stdcall (*typSetHook)(HHOOK);
typedef LRESULT CALLBACK (  *typHookFunc)(int,WPARAM,LPARAM);
static HHOOK gMyGUIProcessingKeyboardHook = NULL;
static HHOOK gMyGUIProcessingMouseHook = NULL;


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}

void __stdcall TForm1::MyKeyboardProc(
  WPARAM wParam,
  LPARAM lParam
)
{
  if (Form1 != NULL)
  {
    Form1->Memo1->Lines->Add(L"GotA keyboard");
  }
}
void __stdcall TForm1::MyMouseProc(
  WPARAM wParam,
  LPARAM lParam
)
{
  if (Form1 != NULL)
  {
    Form1->Memo1->Lines->Add(L"Pip pip");
  }
}

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  if (NULL == mHinst)
  {
    mHinst = LoadLibrary("KeyboardMouseHookDLL.dll");
  }

  if (mHinst)
  {
    typedef void (*Install)(typFn);

    // the keyboard

    typSetHook SetHook = (typSetHook) GetProcAddress( mHinst, "_SetKeyboardHookHandle" );
    typHookFunc wireKeyboardProc = (typHookFunc)GetProcAddress(mHinst, "wireKeyboardProc" );

    Install install = (Install) GetProcAddress(mHinst, "_SetGUIProcessingKeyboard");

    if (install)
    {
       install(&MyKeyboardProc);
    }

    if ((NULL != wireKeyboardProc) &&
        (NULL != SetHook) )
    {
      gMyGUIProcessingKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)wireKeyboardProc,mHinst,NULL);
      SetHook(gMyGUIProcessingKeyboardHook);
    }

    // The mouse
    typSetHook  SetMouseHook = (typSetHook) GetProcAddress(mHinst, "_SetMouseHookHandle");
    typHookFunc wireMouseProc = (typHookFunc)GetProcAddress(mHinst, "wireMouseProc");

    Install installMouse = (Install) GetProcAddress(mHinst, "_SetGUIProcessingMouse");

    if (installMouse)
    {
       installMouse(&MyMouseProc);
    }

    if ((NULL != wireMouseProc) &&
        (NULL != SetMouseHook) )
    {
      gMyGUIProcessingMouseHook = SetWindowsHookEx(WH_MOUSE,(HOOKPROC)wireMouseProc,mHinst,NULL);
      SetMouseHook(gMyGUIProcessingMouseHook);
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  if (NULL == mHinst)
  {
    mHinst = LoadLibrary("KeyboardMouseHookDLL.dll");
  }

  if (mHinst)
  {

    if (NULL != gMyGUIProcessingKeyboardHook )
    {
      UnhookWindowsHookEx(gMyGUIProcessingKeyboardHook);
      gMyGUIProcessingKeyboardHook = NULL;
    }
    typedef void (*Uninstall)(typFn);

    Uninstall uninstall = (Uninstall) GetProcAddress(mHinst, "_ReleaseGUIProcessingKeyboard");

    if (uninstall)
    {
      uninstall(&MyKeyboardProc);
    }

    if (NULL != gMyGUIProcessingMouseHook )
    {
      UnhookWindowsHookEx(gMyGUIProcessingMouseHook);
      gMyGUIProcessingMouseHook = NULL;
    }

    Uninstall uninstallMouse = (Uninstall) GetProcAddress(mHinst, "_ReleaseGUIProcessingMouse");

    if (uninstallMouse)
    {
      uninstallMouse(&MyMouseProc);
    }

    FreeLibrary(mHinst);
    mHinst = NULL;
  }
}
//---------------------------------------------------------------------------

表单标题

[代码]

//---------------------------------------------------------------------------

#ifndef MonitoringToolMainH
#define MonitoringToolMainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
  TMemo *Memo1;
  void __fastcall FormCreate(TObject *Sender);
  void __fastcall FormDestroy(TObject *Sender);
private:    // User declarations
  int __stdcall lKeyBoard();
  void __stdcall MyKeyboardProc( WPARAM wParam, LPARAM lParam );
  void __stdcall MyMouseProc( WPARAM wParam, LPARAM lParam );
  HINSTANCE mHinst;
static  HHOOK mHook;

public:     // User declarations
  __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

1 个答案:

答案 0 :(得分:0)

如果要安装全局系统挂钩,则会在每个正在运行的进程中注入挂钩DLL。由于每个进程都有自己的内存空间,因此您需要定义一个共享数据部分来放置钩子句柄之类的变量,否则它们对于每个进程都会有所不同。

#pragma data_seg(".SHARDAT")
static HHOOK gGUIProcessingKeyboardHook = NULL;
static HHOOK gGUIProcessingMouseHook = NULL;
#pragma data_seg()

另外,不要将函数指针注册到钩子DLL,因为您将要求其他进程调用应用程序中的注册函数。最好注册应用程序的HWND和窗口消息。

在DLL中创建一个导出函数,用于设置挂钩并存储HWND和自定义消息编号,例如:

#pragma data_seg(".SHARDAT")
static HHOOK g_keybHook = NULL;
    static HHOOK g_mouseHook = NULL;
HWND g_registeredWnd = NULL;
UINT g_registeredKeybMsg = 0;
UINT g_registeredMouseMsg = 0; 
#pragma data_seg()
HINSTANCE g_hInstance = NULL;

BOOL InstallHook(HWND registeredWnd, UINT registeredKeybMsg, UINT registeredMouseMsg)
{
    if (g_hHook != NULL) return FALSE;  // Hook already installed
    g_keybHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)KeybProc, g_hInstance, 0);
    if (g_keybHook == NULL) return FALSE;   // Failed to install hook
    g_registeredWnd = registeredWnd;
    g_registeredKeybMsg = registeredKeybMsg;
    g_registeredMouseMsg = registeredMouseMsg;
    return TRUE;
}

DllEntryPoint hinst g_hInstance reason == DLL_PROCESS_ATTACH int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved) { if ((reason == DLL_PROCESS_ATTACH) && (g_hInstance == NULL)) g_hInstance = hinst; return 1; } InstallHook {/ 1}}

registeredKeybMsg = RegisterWindowMessage("MyOwnKeybHookMsg");
registeredMouseMsg = RegisterWindowMessage("MyOwnMouseHookMsg");
InstallHook(hwnd, registeredKeybMsg, registeredMouseMsg);

在您的应用程序中,使用RegisterWindowMessage function注册2个窗口消息,并将这些值从挂钩DLL传递给{{1}}函数。然后您的应用程序需要在其消息循环中处理这些消息。

{{1}}