Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem accessing member functions from LRESULT CALLBACK Function() { }

problem accessing member functions from LRESULT CALLBACK Function() { }

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abiemann
    wrote on last edited by
    #1

    Problem Statement: SetWindowsHookEx() won't permit the 2nd parameter to be a function in a class. Therefore, I have created a separate global function in my .cpp file "MouseHookWndProc" that will be called. However, how is it possible to reach functions in my (Dialog) class from the global function "MouseHookWndProc" ? Source code:

    //DIALOG CONSTRUCTOR
    S2000_CP_DLG::S2000_CP_DLG(CWnd* pParent) : CDialog(S2000_CP_DLG::IDD, pParent)
    {
    mouseHook = SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseHookWndProc, GetModuleHandle(NULL), NULL);
    }

    LRESULT CALLBACK MouseHookWndProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    if (nCode < 0)
    return CallNextHookEx(mouseHook, nCode, wParam, lParam);

    char chTempText\[100\];
    sprintf(chTempText,"MouseHook:0x%X",wParam);
    //S2000\_CP\_DLG::CpMessage(chTempText,0);//show mouse action on dialog
    
    return CallNextHookEx(mouseHook, nCode, wParam, lParam);
    

    }

    class S2000_CP_DLG : public CDialog
    {
    DECLARE_DYNAMIC(S2000_CP_DLG)
    public:
    void CpMessage(char* pszMessage, int nVal);// show messages in edit control
    CEdit m_editMessages;
    }

    My Attempt to solve it: The obvious solution for me was to make CpMessage() a static (as well as the m_editMessages since that member variable is used by the function). This compiled, however Linking failed:

    error LNK2019: unresolved external symbol "public: static class CEdit S2000_CP_DLG::m_editMessages" (?m_editMessages@S2000_CP_DLG@@2VCEdit@@A) referenced in function "protected: virtual void __thiscall S2000_CP_DLG::DoDataExchange(class CDataExchange *)" (?DoDataExchange@S2000_CP_DLG@@MAEXPAVCDataExchange@@@Z) S2000_CP_DLG.obj

    void S2000_CP_DLG::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);

    DDX\_Control(pDX, IDC\_MESSAGES, this->m\_editMessages);//Edit Control
    

    }

    The problem persists... and unfortunately adding "this->" to the DoDataExchange() didn't work :sigh: does anyone have any recommendations ?

    _ 1 Reply Last reply
    0
    • A abiemann

      Problem Statement: SetWindowsHookEx() won't permit the 2nd parameter to be a function in a class. Therefore, I have created a separate global function in my .cpp file "MouseHookWndProc" that will be called. However, how is it possible to reach functions in my (Dialog) class from the global function "MouseHookWndProc" ? Source code:

      //DIALOG CONSTRUCTOR
      S2000_CP_DLG::S2000_CP_DLG(CWnd* pParent) : CDialog(S2000_CP_DLG::IDD, pParent)
      {
      mouseHook = SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseHookWndProc, GetModuleHandle(NULL), NULL);
      }

      LRESULT CALLBACK MouseHookWndProc(int nCode, WPARAM wParam, LPARAM lParam)
      {
      if (nCode < 0)
      return CallNextHookEx(mouseHook, nCode, wParam, lParam);

      char chTempText\[100\];
      sprintf(chTempText,"MouseHook:0x%X",wParam);
      //S2000\_CP\_DLG::CpMessage(chTempText,0);//show mouse action on dialog
      
      return CallNextHookEx(mouseHook, nCode, wParam, lParam);
      

      }

      class S2000_CP_DLG : public CDialog
      {
      DECLARE_DYNAMIC(S2000_CP_DLG)
      public:
      void CpMessage(char* pszMessage, int nVal);// show messages in edit control
      CEdit m_editMessages;
      }

      My Attempt to solve it: The obvious solution for me was to make CpMessage() a static (as well as the m_editMessages since that member variable is used by the function). This compiled, however Linking failed:

      error LNK2019: unresolved external symbol "public: static class CEdit S2000_CP_DLG::m_editMessages" (?m_editMessages@S2000_CP_DLG@@2VCEdit@@A) referenced in function "protected: virtual void __thiscall S2000_CP_DLG::DoDataExchange(class CDataExchange *)" (?DoDataExchange@S2000_CP_DLG@@MAEXPAVCDataExchange@@@Z) S2000_CP_DLG.obj

      void S2000_CP_DLG::DoDataExchange(CDataExchange* pDX)
      {
      CDialog::DoDataExchange(pDX);

      DDX\_Control(pDX, IDC\_MESSAGES, this->m\_editMessages);//Edit Control
      

      }

      The problem persists... and unfortunately adding "this->" to the DoDataExchange() didn't work :sigh: does anyone have any recommendations ?

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Create a global pointer of your class.

      S2000_CP_DLG* pThis = 0;

      In the constructor or the OnInitDialog function assign the address of the object to the pointer.

      pThis = this;

      Now you can access the class members using pThis.

      «_Superman_» I love work. It gives me something to do between weekends.

      A 1 Reply Last reply
      0
      • _ _Superman_

        Create a global pointer of your class.

        S2000_CP_DLG* pThis = 0;

        In the constructor or the OnInitDialog function assign the address of the object to the pointer.

        pThis = this;

        Now you can access the class members using pThis.

        «_Superman_» I love work. It gives me something to do between weekends.

        A Offline
        A Offline
        abiemann
        wrote on last edited by
        #3

        nice!

        pThis->CpMessage(chTempText,0);

        compiled and linked without a hitch.:thumbsup:

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups