Search code examples
c++qtdlldll-injection

Variable keeps getting set to NULL after function in DLL


I need valid data to be in the global variable QObject *p. However, assigning anything to this variable inside of a function works within the scope of the function, but after the function returns, p is set back to NULL, even though p is global. Here is my code:

    #include ... // various includes

    // p is NULL
    QObject *p;
    HHOOK hhk;

    BOOL WINAPI DllMain(__in HINSTANCE hinstDLL, __in DWORD fdwReason, __in LPVOID  lpvReserved)
    {
        return TRUE;
    }

    LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        MOUSEHOOKSTRUCT *mouseInfo = (MOUSEHOOKSTRUCT*)lParam;

        QMouseEvent::Type type;
        QPoint pos = QPoint(mouseInfo->pt.x, mouseInfo->pt.y);
        Qt::MouseButton bu;
        Qt::MouseButtons bus;
        Qt::KeyboardModifiers md = Qt::NoModifier;
        ... // very large switch statement
        // here is where i need some valid data in p
        QCoreApplication::postEvent(p, new QMouseEvent(type, pos, bu, bus, md));
        return CallNextHookEx(NULL, nCode, wParam, lParam);
    }

    // note: MOUSEHOOKSHARED_EXPORT is the same as __declspec(dllexport)
    // this function is called by the application that loads the dll
    extern "C" MOUSEHOOKSHARED_EXPORT void install(QObject *mParent, DWORD threadID, HINSTANCE hInst)
    {
        p = mParent; // p is assigned here and keeps the value of mParent untill the function returns
        hhk = SetWindowsHookEx(WH_MOUSE, MouseProc, hInst, threadID);
    } // after this function returns, p is NULL

    extern "C" MOUSEHOOKSHARED_EXPORT void uninstall()
    {
        UnhookWindowsHookEx(hhk);
    }

I have tried various data structure "workarounds" such as using struct typedef etc... i can't seem to get this to work. All i need is for p to retain the value of mParent.

EDIT: Here is where i execute install()

    // EarthWidget is a class that is derived from QWidget(which is derived from QObject)
    void EarthWidget::LoadAll()
    {
        HINSTANCE DLLinst = LoadLibrary("MouseHook.dll");
        ... // get functions in DLL using GetProcAddress & typedefs, etc...
        // i pass in 'this' as mParent
        install(this, GetWindowThreadProcessId((HWND)earthplugin->GetRenderHwnd(), NULL), DLLinst);
        // note that GetWindowThreadProcessId does work and does return a valid thread id, so no problem there
     }

EDIT: Found out what was wrong. The this pointer becomes out of scope when install is executed, therefore mParent, being a QObject, initializes itself to NULL. Thus p becomes NULL. this comes back into scope when install returns, however, at a completely different memory address. The solution, after extensive debugging and headaches, would be to create a class member function that takes a QObject as a parameter and passes that into install instead of this. Whatever you pass into that function must last as long as you need the DLL to last. That, or, you can create your own copy constructor that performs a deep copy.


Solution

  • You're creating a shallow copy of that parameter mParent. At some point, that pointer must be getting set to null (or you're passing it as null), which will result in p also becoming null.