如何从dll访问调用应用程序中的变量?

时间:2022-04-10 23:50:01

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.

我正在尝试一些小概念测试来改变罗技MS3200键盘的一个功能(缩放功能)。我有从变焦控制发送的键。所以,我有一个主应用程序,以及一个包含钩子程序的DLL。

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:

这里是钩子程序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().

我需要能够从dll访问SetWindowsHookEx(hHook)返回的内容,以便调用CallNextHookEx()。

4 个解决方案

#1


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

这可能是可能的,但是不值得花时间去调查。

Instead, move the hook setting code to the DLL.

而是将钩子设置代码移动到DLL。

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

哦,我认为你需要将DLL模块句柄传递给钩子设置函数,而不是NULL

#2


One possible way to handle this would be to have another exported function in your DLL to pass the hHook to, and save that in a variable local within the DLL for use in the HookProc.

处理此问题的一种可能方法是在DLL中使用另一个导出函数来传递hHook,并将其保存在DLL中的本地变量中,以便在HookProc中使用。


HANDLE dllHook;
void __declspec (dllexport) HookHandle(HANDLE hHook)
{
  dllHook = hHook;
}

int __declspec (dllexport) HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if(nCode < 0 && dllHook!= NULL)
    {
        return CallNextHookEx(dllHook, nCode, wParam, lParam);
    }
    //....
}

int WINAPI dllmain(HINSTANCE hMod, DWORD data, LPVOID lpVOid)
{
  dllHook = NULL; //initialize
  return 0;
}

#3


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.

DLL中的代码无法直接访问调用应用程序中的变量,因为无法保证加载DLL的应用程序将定义这些变量,即使这样做,DLL也无法知道它将存储在何处。

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.

你可以在DLL中有一个全局变量和一个额外的入口点来设置它,你在调用SetWindowsHookEx后调用它。在调用CallNextHookEx之前,DLL需要等到这个入口点被调用。

#4


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

在NT / XP / 2003等上,CallNextHookEx的第一个参数被忽略。请参阅CallNextHookEx的文档:

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

hth

#1


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

这可能是可能的,但是不值得花时间去调查。

Instead, move the hook setting code to the DLL.

而是将钩子设置代码移动到DLL。

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

哦,我认为你需要将DLL模块句柄传递给钩子设置函数,而不是NULL

#2


One possible way to handle this would be to have another exported function in your DLL to pass the hHook to, and save that in a variable local within the DLL for use in the HookProc.

处理此问题的一种可能方法是在DLL中使用另一个导出函数来传递hHook,并将其保存在DLL中的本地变量中,以便在HookProc中使用。


HANDLE dllHook;
void __declspec (dllexport) HookHandle(HANDLE hHook)
{
  dllHook = hHook;
}

int __declspec (dllexport) HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if(nCode < 0 && dllHook!= NULL)
    {
        return CallNextHookEx(dllHook, nCode, wParam, lParam);
    }
    //....
}

int WINAPI dllmain(HINSTANCE hMod, DWORD data, LPVOID lpVOid)
{
  dllHook = NULL; //initialize
  return 0;
}

#3


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.

DLL中的代码无法直接访问调用应用程序中的变量,因为无法保证加载DLL的应用程序将定义这些变量,即使这样做,DLL也无法知道它将存储在何处。

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.

你可以在DLL中有一个全局变量和一个额外的入口点来设置它,你在调用SetWindowsHookEx后调用它。在调用CallNextHookEx之前,DLL需要等到这个入口点被调用。

#4


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

在NT / XP / 2003等上,CallNextHookEx的第一个参数被忽略。请参阅CallNextHookEx的文档:

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

hth