tags:

views:

118

answers:

4

I'm trying a little concept test to change one of the features of the logitech MS3200 keyboard (the zoom feature). I've got the keys that are sent from the zoom control. So, I have a main app, and a dll that contains a hook procedure.

Here's the main app:

#include <stdio.h>
#include <windows.h>

HANDLE hHook;

int main()
{
    HINSTANCE hMod = GetModuleHandle(NULL);

    hHook = SetWindowsHookEx(WH_KEYBOARD, HookProc,0,0);

    if(hHook == NULL)
     printf("Unable to set hook! Error: %d", GetLastError());
    else
     printf("Hook set successfully!");

    while(TRUE)
    {
     Sleep(1000);
    }

    return 0;
}

And here is the hook procedure dll:

#include <windows.h>

int __declspec (dllexport) HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if(nCode < 0)
    {
     return CallNextHookEx(hHook, nCode, wParam, lParam);
    }


    if(wParam == VK_ADD || wParam == VK_SUBTRACT)
    {
     short status = GetKeyState(VK_CONTROL);

     if(status == 1)
     {
      if(wParam == VK_ADD)
       wParam = VK_UP;
      else
       wParam = VK_DOWN;
     }
    }

    return CallNextHookEx(hHook, nCode, wParam, lParam);
}

int WINAPI dllmain(HINSTANCE hMod, DWORD data, LPVOID lpVOid)
{
  return 0;
}

I need to be able to access what's returned by SetWindowsHookEx (hHook) from the dll, in order to call CallNextHookEx().

A: 

Code in a DLL can't directly access variables in the calling app, because there's no guarantee that the app that loads the DLL will have those variables defined, and even if it did, the DLL has no way to know where it would be stored.

You could have a global variable in the DLL and an extra entry point to set it, which you call after calling SetWindowsHookEx. The DLL would need to wait until this entry point is called before calling CallNextHookEx.

Graeme Perrow
+1  A: 
crashmstr
+2  A: 

It's probably possible, but it's not worth your time to investigate.

Instead, move the hook setting code to the DLL.

Oh, and I think you need to pass the DLL module handle to the hook setting function, not a NULL

Arkadiy
makes sense. thanks
scottm
A: 

On NT/XP/2003 etc the first param to CallNextHookEx is ignored. See the documentation for CallNextHookEx:

http://msdn.microsoft.com/en-us/library/ms644974%28VS.85%29.aspx

hth

fusi