Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6089617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:07:12+00:00 2026-05-23T12:07:12+00:00

I need valid data to be in the global variable QObject *p . However,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:07:13+00:00Added an answer on May 23, 2026 at 12:07 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to verify if the args are valid file names, is this the
i need to parse through a text file and process the data. the valid
I need to export data into an Access database. My code works, but it
I need to transform a valid XML document to the OFX v1.0.2 format .
I need to convert HTML documents into valid XML, preferably XHTML. What's the best
I need to select an entity that is valid at the midth of the
I need to be able to lock down the valid characters in a textbox,
I need some easy API to verify that a PE file is valid &
I need to determine if a user-supplied string is a valid file path (i.e.,
I'm writing an XSLT template that need to output a valid xml file for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.