下面是我为全局键盘钩子建立的DLL程序(基本上改自programming windows的源程序)
//edrtest.c
#include <windows.h>
#include "edrlib.h"HHOOK hHook;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
HANDLE g_hinstDll;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("StrProg") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
     
     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szAppName, TEXT ("DLL Demonstration Program"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInstance, NULL) ;
     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{     
     switch (message)
     {
 case WM_CREATE:
       hHook=SetWindowsHookEx(WH_KEYBOARD,KbProc,g_hinstDll,0);
 return 0;
          
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}//edrlib.c#include <windows.h>
#include "edrlib.h"
HHOOK hHook;EXPORT LRESULT CALLBACK KbProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if (nCode < 0)
return CallNextHookEx(hHook,nCode, wParam, lParam);
    switch(wParam)
{
case VK_SHIFT:
MessageBox(NULL,TEXT("XX"),TEXT("XX"),MB_OK);
break;
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}BOOL WINAPI DllMain(HINSTANCE hInstance,DWORD dwReason,LPVOID lpvReserved)
{
switch (dwReason) 
{
case DLL_PROCESS_ATTACH:
g_hinstDll = hInstance;
break;
}
return TRUE;
}//edrlib.h
#ifdef __cplusplus
#define EXPORT extern "C" __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endifHANDLE g_hinstDll;
EXPORT LRESULT CALLBACK KbProc(int,WPARAM,LPARAM);如果不用dll做全局钩子是可以实现hook住SHIFT键的,但是象上面一样
用dll却实现不了hook,请大虾们帮我看看问题出在哪里?(编译是可以通过的
设置应该也没问题,我改的是programming windows里面的一个dll小程序)
对了,顺便问一下,dll程序怎么调试呀?每次想设断点都失败。谢谢

解决方案 »

  1.   

    //edrtest.c
    #include <windows.h>
    #include "edrlib.h"HHOOK hHook;
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         static TCHAR szAppName[] = TEXT ("StrProg") ;
         HWND         hwnd ;
         MSG          msg ;
         WNDCLASS     wndclass ;     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.lpszMenuName  = NULL ;
         wndclass.lpszClassName = szAppName ;
         
         if (!RegisterClass (&wndclass))
         {
              MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
         
         hwnd = CreateWindow (szAppName, TEXT ("DLL Demonstration Program"),
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              NULL, NULL, hInstance, NULL) ;
         
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
         
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
         return msg.wParam ;
    }LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {     
         switch (message)
         {
     case WM_CREATE:
                       StarHook();
     return 0;
              
         case WM_DESTROY:
              EndHook();
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    //edrlib.c#include <windows.h>
    #include "edrlib.h"#pragma data_seg(".HOOKDATA")//Shared data among all instances.
    HHOOK hHook = NULL;
    HINSTANCE g_hinstDll = NULL
    #pragma data_seg()
    #pragma comment(linker, "/SECTION:.HOOKDATA,RWS")//linker directive
    EXPORT LRESULT CALLBACK KbProc(int nCode,WPARAM wParam,LPARAM lParam)
    {
    if (nCode < 0)
    return CallNextHookEx(hHook,nCode, wParam, lParam);
        switch(wParam)
    {
    case VK_SHIFT:
    MessageBox(NULL,TEXT("XX"),TEXT("XX"),MB_OK);
    break;
    }
    return CallNextHookEx(hHook, nCode, wParam, lParam);
    }BOOL WINAPI DllMain(HINSTANCE hInstance,DWORD dwReason,LPVOID lpvReserved)
    {
    switch (dwReason) 
    {
    case DLL_PROCESS_ATTACH:
    g_hinstDll = hInstance;
    break;
    }
    return TRUE;
    }StarHook()
    {
            hHook=SetWindowsHookEx(WH_KEYBOARD,KbProc,g_hinstDll,0);}EndHook()
    {
            if(hHook)
                  UnhookWindowsHookEx(hHook);
    }
    //edrlib.h
    #ifdef __cplusplus
    #define EXPORT extern "C" __declspec (dllexport)
    #else
    #define EXPORT __declspec (dllexport)
    #endifHANDLE g_hinstDll;
    EXPORT LRESULT CALLBACK KbProc(int,WPARAM,LPARAM);
    EXPORT StarHook();
    EXPORT EndHook();
      

  2.   

    BCB里可以调试DLL你的代码看上去没有错啊,
      

  3.   

    sorry 上面有点错误。//edrtest.c
    #include <windows.h>
    #include "edrlib.h"HHOOK hHook;
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         static TCHAR szAppName[] = TEXT ("StrProg") ;
         HWND         hwnd ;
         MSG          msg ;
         WNDCLASS     wndclass ;     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.lpszMenuName  = NULL ;
         wndclass.lpszClassName = szAppName ;
         
         if (!RegisterClass (&wndclass))
         {
              MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
         
         hwnd = CreateWindow (szAppName, TEXT ("DLL Demonstration Program"),
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              NULL, NULL, hInstance, NULL) ;
         
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
         
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
         return msg.wParam ;
    }LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {     
         switch (message)
         {
     case WM_CREATE:
                       StarHook();
     return 0;
              
         case WM_DESTROY:
              EndHook();
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    //edrlib.c#include <windows.h>
    #include "edrlib.h"#pragma data_seg(".HOOKDATA")//Shared data among all instances.
    HHOOK hHook = NULL;
    HINSTANCE g_hinstDll = NULL
    #pragma data_seg()
    #pragma comment(linker, "/SECTION:.HOOKDATA,RWS")//linker directiveLRESULT CALLBACK KbProc(int nCode,WPARAM wParam,LPARAM lParam);LRESULT CALLBACK KbProc(int nCode,WPARAM wParam,LPARAM lParam)
    {
    if (nCode < 0)
    return CallNextHookEx(hHook,nCode, wParam, lParam);
        switch(wParam)
    {
    case VK_SHIFT:
    MessageBox(NULL,TEXT("XX"),TEXT("XX"),MB_OK);
    break;
    }
    return CallNextHookEx(hHook, nCode, wParam, lParam);
    }BOOL WINAPI DllMain(HINSTANCE hInstance,DWORD dwReason,LPVOID lpvReserved)
    {
    switch (dwReason) 
    {
    case DLL_PROCESS_ATTACH:
    g_hinstDll = hInstance;
    break;
    }
    return TRUE;
    }Void StarHook()
    {
            hHook=SetWindowsHookEx(WH_KEYBOARD,KbProc,g_hinstDll,0);}Void EndHook()
    {
            if(hHook)
                  UnhookWindowsHookEx(hHook);
    }//edrlib.h
    #ifdef __cplusplus
    #define EXPORT extern "C" __declspec (dllexport)
    #else
    #define EXPORT __declspec (dllexport)
    #endifEXPORT Void StarHook();
    EXPORT Void EndHook();
      

  4.   

    局键盘钩子建立的DLL程序:1:变量要设置成共享! #segment “ShareData”
    2:你的回调函数Export干什么? 它不是这一点阿!