我在使用 SetWindowsHookEx 时遇到了一个奇怪的情况
我有一个 bcb 2009 项目,上面有一个表格和一个备忘录。
在创建中,我们加载 Dll 并将函数处理程序附加到两侧。
这个想法是,当按下键盘时,备忘录框中会出现一条消息,而当鼠标事件发生时,备忘录框中会出现其他文本。
奇怪的是,当我从调试信息中清除代码时,它停止工作。这意味着钩子被触发了一次,然后就结束了。在调试中,我使用了一些 VCL TStringList 将 key stokes 数据记录到磁盘。使用该代码我终于通过添加检测到
[代码]
TList* lList = new TList();
delete lList;
对于每个钩子函数(键盘、鼠标),代码都可以再次工作。
我必须这样做的代码有什么问题?
这是我 15 年来第一次制作 dll。所以它可以是创建 dll 或导出函数的真正基础。
欢迎提出任何建议。
问候
合资公司
一些新的附加信息:
[已解决]我的目标是嵌入式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
这是应用程序。
[代码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