Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 built 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 with 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 created. 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
Posted
Updated 2-May-11 8:13am
v3
Comments
Code tags are added

You said this was running in a Windows Service?? If true, it won't work because services run under a different Desktop. It doesn't get keyboard messages because theres no point in sending keyboard and mouse input to a desktop that the user cannot see.

Tag your service as "Allow service to interact with desktop" and that MIGHT fix the problem.
 
Share this answer
 
Comments
Niklas L 2-May-11 15:40pm    
Good call! 5.
Thanks for that fast an good answer.

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?
 
Share this answer
 
Comments
Stefan_Lang 4-May-11 3:10am    
You should add your response as a comment in the solution you'r responding to, not as a new solution - otherwise the author of that solution will not be notified of your comment and follow-up question. (use the blue 'Add Comment' link in the lower right corner of each Solution box)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900