Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 9th, 2012  |  syntax: None  |  size: 1.91 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Variable keeps getting set to NULL after function in DLL
  2. #include ... // various includes
  3.  
  4.     // p is NULL
  5.     QObject *p;
  6.     HHOOK hhk;
  7.  
  8.     BOOL WINAPI DllMain(__in HINSTANCE hinstDLL, __in DWORD fdwReason, __in LPVOID  lpvReserved)
  9.     {
  10.         return TRUE;
  11.     }
  12.  
  13.     LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
  14.     {
  15.         MOUSEHOOKSTRUCT *mouseInfo = (MOUSEHOOKSTRUCT*)lParam;
  16.  
  17.         QMouseEvent::Type type;
  18.         QPoint pos = QPoint(mouseInfo->pt.x, mouseInfo->pt.y);
  19.         Qt::MouseButton bu;
  20.         Qt::MouseButtons bus;
  21.         Qt::KeyboardModifiers md = Qt::NoModifier;
  22.         ... // very large switch statement
  23.         // here is where i need some valid data in p
  24.         QCoreApplication::postEvent(p, new QMouseEvent(type, pos, bu, bus, md));
  25.         return CallNextHookEx(NULL, nCode, wParam, lParam);
  26.     }
  27.  
  28.     // note: MOUSEHOOKSHARED_EXPORT is the same as __declspec(dllexport)
  29.     // this function is called by the application that loads the dll
  30.     extern "C" MOUSEHOOKSHARED_EXPORT void install(QObject *mParent, DWORD threadID, HINSTANCE hInst)
  31.     {
  32.         p = mParent; // p is assigned here and keeps the value of mParent untill the function returns
  33.         hhk = SetWindowsHookEx(WH_MOUSE, MouseProc, hInst, threadID);
  34.     } // after this function returns, p is NULL
  35.  
  36.     extern "C" MOUSEHOOKSHARED_EXPORT void uninstall()
  37.     {
  38.         UnhookWindowsHookEx(hhk);
  39.     }
  40.        
  41. // EarthWidget is a class that is derived from QWidget(which is derived from QObject)
  42.     void EarthWidget::LoadAll()
  43.     {
  44.         HINSTANCE DLLinst = LoadLibrary("MouseHook.dll");
  45.         ... // get functions in DLL using GetProcAddress & typedefs, etc...
  46.         // i pass in 'this' as mParent
  47.         install(this, GetWindowThreadProcessId((HWND)earthplugin->GetRenderHwnd(), NULL), DLLinst);
  48.         // note that GetWindowThreadProcessId does work and does return a valid thread id, so no problem there
  49.      }