# [SOLVED] SetWindowsHook help



## altxerror (Aug 7, 2009)

i'm new to hooking in C++ and i need some help with an error i am getting.

error C2664: 'SetWindowsHookExW' : cannot convert parameter 2 from 'LRESULT (__stdcall *)(int,LPARAM,WPARAM)' to 'HOOKPROC'
1> This conversion requires a reinterpret_cast, a C-style cast or function-style cast

with the following code:


```
#include <windows.h>
#include <windowsx.h>
#include <vector>
#include <string>
#include <iomanip>
#include <iostream>
#include <conio.h>
#include <tchar.h>


/* UNMODIFIABLE DEFINITIONS! */

HHOOK hooked = 0;
HWND hWnd;

/* function ptototypes */

void clear_screen ( void ); // clear screen

void clear_screen (void)
{
  DWORD n;                         /* Number of characters written */
  DWORD size;                      /* number of visible characters */
  COORD coord = {0};               /* Top left screen position */
  CONSOLE_SCREEN_BUFFER_INFO csbi;

  /* Get a handle to the console */
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );

  GetConsoleScreenBufferInfo ( h, &csbi );

  /* Find the number of characters to overwrite */
  size = csbi.dwSize.X * csbi.dwSize.Y;

  /* Overwrite the screen buffer with whitespace */
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );

  /* Reset the cursor to the top left position */
  SetConsoleCursorPosition ( h, coord );
}

static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, LPARAM lparam, WPARAM wparam) /* Keyboard processsor: should capture all key events */
{
	KBDLLHOOKSTRUCT* AboutKeyboard = (KBDLLHOOKSTRUCT*) lparam;

	return static_cast<unsigned int>(AboutKeyboard->vkCode) && CallNextHookEx(hooked, int nCode, WPARAM Wparam, LPARAM lparam);
}



int WINAPI main(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
	 
	/* set the hook and check if it has a value ! NULL */

	hooked = SetWindowsHookEx(WH_KEYBOARD_LL, &LowLevelKeyboardProc, hThisInstance, NULL);
	  
	 if (hooked == NULL) /* somehow the HOOK is NULL */
	  {
		  MessageBox(hWnd,L"The Low Level Keyboard Processor Returned a HOOK with a value of NULL", L"LLKPROC Error", MB_OK || MB_ICONEXCLAMATION);
		  return 0;
	  }

	 /* we  will intialize the screen with function from text.h */

	 //yet to add code.


}
```
i've tried everything and i can't seem to get it to work. any ideas?

btw, i'm making a text based game in with this code.


----------



## Ninjaboi (Dec 27, 2010)

*Re: SetWindowsHook help*

Hello altxerror!


```
hooked = SetWindowsHookEx(WH_KEYBOARD_LL, &LowLevelKeyboardProc, hThisInstance, NULL);
```
In the second parameter, remove the ampersand (&) to provide the address of the function.

Source(s):

CALLBACK errror on class - CodeGuru Forums


----------



## altxerror (Aug 7, 2009)

*Re: SetWindowsHook help*

hi. sorry for not getting back to you. removing the ampersand did not correct the problem.

i still have the same error.


----------



## Ninjaboi (Dec 27, 2010)

*Re: SetWindowsHook help*


```
#include <windows.h>
#include <windowsx.h>
#include <vector>
#include <string>
#include <iomanip>
#include <iostream>
#include <conio.h>

/* UNMODIFIABLE DEFINITIONS! */

HHOOK hooked = 0;
HWND hWnd;

/* function ptototypes */

void clear_screen ( void ); // clear screen

void clear_screen ( void )
{

  DWORD n;                         /* Number of characters written */
  DWORD size;                      /* number of visible characters */
  COORD coord = {0};               /* Top left screen position */
  CONSOLE_SCREEN_BUFFER_INFO csbi;

  /* Get a handle to the console */
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );

  GetConsoleScreenBufferInfo ( h, &csbi );

  /* Find the number of characters to overwrite */
  size = csbi.dwSize.X * csbi.dwSize.Y;

  /* Overwrite the screen buffer with whitespace */
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );

  /* Reset the cursor to the top left position */
  SetConsoleCursorPosition ( h, coord );
}

static LRESULT CALLBACK LowLevelKeyboardProc( int nCode, LPARAM lparam, WPARAM Wparam ) /* Keyboard processsor: should capture all key events */
{
    KBDLLHOOKSTRUCT* AboutKeyboard = ( KBDLLHOOKSTRUCT* ) lparam;

    return static_cast<unsigned int>( AboutKeyboard->vkCode ) && CallNextHookEx(hooked, nCode, Wparam, lparam);
}



int WINAPI main( HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow )
{
     
    /* set the hook and check if it has a value ! NULL */

    hooked = SetWindowsHookExW( WH_KEYBOARD_LL, (HOOKPROC)&LowLevelKeyboardProc, hThisInstance, NULL);
      
     if ( hooked == NULL ) /* somehow the HOOK is NULL */
     {

          MessageBox( hWnd, L"The Low Level Keyboard Processor Returned a HOOK with a value of NULL", L"LLKPROC Error", MB_OK || MB_ICONEXCLAMATION );
          return 0;

     }

     /* we  will intialize the screen with function from text.h */

     //yet to add code.

     return 0;


}
```
I fixed the code and compiled and ran it perfectly. What I did was:



Capitalize the 'W' in the last parameter of LowLevelKeyboardProc().


```
LowLevelKeyboardProc( int nCode, LPARAM lparam, WPARAM [B]Wparam[/B] )
```


Removed the re-declared data types from this line:


```
return static_cast<unsigned int>( AboutKeyboard->vkCode ) && CallNextHookEx[B](hooked, nCode, Wparam, lparam)[/B];
```


Made the second parameter of SetWindowsHookExW() a HOOKPROC data type.


```
SetWindowsHookExW( WH_KEYBOARD_LL, [B](HOOKPROC)&LowLevelKeyboardProc[/B], hThisInstance, NULL);
```


Made the main function return the value '0'.


```
return 0;
```


Set the entry point of the program to 'main' in VS2008 by going to "Project >> Properties >> Configuration Properties >> Linker >> Advanced >> Entry Point.".


Ensured that the System type selected for the program was Console by going to "Project >> Properties >> Configuration Properties >> Linker >> System >> SubSystem.".
That should fix your problem.

Happy coding!


----------



## altxerror (Aug 7, 2009)

*Re: SetWindowsHook help*

thanks.

now i can get back to coding.


----------



## Ninjaboi (Dec 27, 2010)

*Re: SetWindowsHook help*

Your welcome.

If this problem has been solved, please mark the thread "Solved" by going to the top of the thread, select "Thread Tools", and select "Mark this thread as Solved". If you are unable to see this option, simply say so and we can mark it for you. If this problem turns out to not be solved, please specify so in your next post.

Thank you!


----------

