Lowlevelkeyboardproc is never called

Hi,



I have to port a Windows CE application to Windows XP.

In Windows CE this application runs fine. It contains a lowlevelkeyboardproc that is called when a key is pressed.



The different Windows CE projects are build with several makefile-, sources- & *.def- files.

I build new projects with Visual Studio 2010 and added the *.cpp & *.h files.

Further there has been some Windows CE specific stuff in the code, which I replaced thru Windows XP stuff.


Concerning the lowlevelkeyboardproc I changed nothing in the existing Windows CE code.

The lowlevelkeyboardproc stuff is implemented in a service (*.exe) and a DLL.
The service loads the DLL. The DLL is the part that contains the hook function.






1. Inside the service a thread is create. Concerning the lowlevelkeyboardproc the following stuff is done in this thread:
---------------------------------------------------------------------------------------------------------------------------

// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// MyServiceThread
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
DWORD WINAPI MyServiceThread (LPVOID lpParameter)
{

// ... some other code that is executed

HMODULE hinstDLL = LoadLibrary(TEXT("MyHook.dll"));
BOOL_FUNC pInstallHook = (BOOL_FUNC)GetProcAddress(hinstDLL,"InstallHook");
pInstallHook();


MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}


BOOL_FUNC pUnInstallHook = (BOOL_FUNC)GetProcAddress(hinstDLL,"UnInstallHook");
pUnInstallHook();

// ... some other code that is executed

return 0;
}





2. the lowlevelkeyboardproc related stuff in my MyHook.dll is:
---------------------------------------------------------------------------------------------------------------------------

........


// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// DllMain
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD fdwReason,
LPVOID lpReserved )
{
static HANDLE hFileMap = NULL;

if( DLL_PROCESS_ATTACH == fdwReason )
{
InitializeCriticalSection(&csStatusPos);
EnterCriticalSection(&csStatusPos);

hFileMap = CreateFileMapping((HANDLE)-1, (LPSECURITY_ATTRIBUTES) NULL, PAGE_READWRITE, (DWORD) 0, (DWORD) sizeof(SHARED_MEM_VAR), TEXT("MY_SHARED_MEM_VAR") );
p_my_shared_mem = (PSHARED_MEM_VAR)MapViewOfFile( hFileMap, FILE_MAP_WRITE, (DWORD) 0, (DWORD) 0, (DWORD) sizeof(SHARED_MEM_VAR));

p_my_shared_mem->m_hDllInst = (HINSTANCE) hModule;
p_my_shared_mem->m_MyKeyBoardHookObject = new MyKeyBoardHookClass;
p_my_shared_mem->m_MyKeyBoardHookObject->Init();
....

LeaveCriticalSection(&csStatusPos);
}

if(DLL_PROCESS_DETACH == fdwReason)
{
EnterCriticalSection(&csStatusPos);

delete p_my_shared_mem->m_MyKeyBoardHookObject;
.....

UninstallHook();
UnmapViewOfFile( (LPCVOID) p_my_shared_mem);
CloseHandle(hFileMap);

LeaveCriticalSection(&csStatusPos);
DeleteCriticalSection(&csStatusPos);
}


return TRUE;
}

// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// InstallHook
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
EXTERN BOOL InstallHook()
{


p_my_shared_mem->m_hMyKeybdHook = SetWindowsHookEx( WH_KEYBOARD_LL,
(HOOKPROC)MyLowLevelKeyboardHook,
p_my_shared_mem->m_hDllInst,
0 );


return ! ( NULL == p_my_shared_mem->m_hMyKeybdHook );
}

// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// UninstallHook
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
EXTERN BOOL UninstallHook()
{

UnhookWindowsHookEx(p_my_shared_mem->m_hMyKeybdHook);

return FALSE;
}

// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// MyLowLevelKeyboardHook
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
EXTERN HOOKPROC MyLowLevelKeyboardHook(int nCode, WPARAM wParam, LPARAM lParam)
{

........


}



........






1. Can somebody see, why MyLowLevelKeyboardHook is never called in Windows XP, when a key has been pressed?

2. I tried a lot of stuff to get my Hook installed.... I read a lot in the MSDN about Hooks &
especially lowlevelkeyboardproc etc... I searched in old forum threads for a solution. ...
But I wasn't able to fix the problem. My hook still isn't called.

==> Can somebody tell me please all the important points, that are required to
get a own lowlevelkeyboardproc global hook working.

3. Does somebody know a good link(s) to a place(s), that explains everything about this topic?

4. Does somebody know a good link(s) to a place(s), with a good free sample code?






Thanks in advance for all your answers and help.

regards dave

I set the check box "Allow service to interact with desktop" and now the keyboard hook is called.


But unfortunately the unhandled exception "privileged instruction" is thrown, after leaving the keyboard hook function.
Even with a total empty keyboard hook function, this error occurs:

EXTERN HOOKPROC KeyboardHook(int nCode, WPARAM wParam, LPARAM lParam)
{
return (HOOKPROC)CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}


How can this problem be solved?
Topic archived. No new replies allowed.