Thread: Please help with MFC cross-threaded window access

  1. #1
    Registered User
    Join Date
    Jul 2012

    Please help with MFC cross-threaded window access


    I cannot figure out how to solve this one problem for my life... I have an MFC CRichEditCtrl that I would like to change the text of from another thread in my application. This thread consists of a CALLBACK function that's being used for SetWindowsHookEx. If you've done C++ Windows hooking before, you know exactly what I'm talking about.

    My instinctive reaction is to make the CRichEditCtrl be public and static (and maybe make the Dialog that it's in be static too), but obviously that hasn't worked out very well for me. I've also tried making static "access" functions and such.

    I prefer not to have some lame workaround like magically passing in a pointer to my CALLBACK function or anything. Maybe I could access properties in the Dialog by accessing the current Dialog running, from "theApp" (which is declared inside "[ProgramNameHere].cpp")?

    Yes, I've Googled this problem, looked at MSDN documentation, etc. etc. etc. If you know the answer, please give me a simple explanation of how I can access the CRichEditCtrl in my Dialog.

    Thanks you.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    Depending on how often you need to do that it seems like it would make sense just to join the threads first.

  3. #3
    Registered User
    Join Date
    Jul 2012
    That won't work with a Windows hook - it's required that you make a separate thread for it.

  4. #4
    Registered User
    Join Date
    Jul 2012
    I found a solution for myself. I still would like to know how cross threaded access could work for non-static variables in an MFC application though!

    For those of you with a similar problem to mine:

    My solution was to make the Windows hook CALLBACK functions be static (note: only put "static " before the function declarations in the header file - don't place them in the actual code) and to make the CRichEditCtrl static as well. Also, I had to move the CRichEditCtrl declaration outside of the header file (outside of the class), and I didn't place "[ProgramNameHere]::" before the variable names. So basically, I made CRichEditCtrl a global static variable, in normal-people talking terms.

    This website helped me a little bit:

    If anyone knows the answer to my question above (at the top of this post), please let me know!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threaded callbacks...
    By megrez80 in forum C Programming
    Replies: 2
    Last Post: 11-29-2011, 11:56 AM
  2. Access Function in Child Window From Parent Window
    By Welder in forum Windows Programming
    Replies: 3
    Last Post: 05-20-2008, 03:06 AM
  3. Going from single-threaded to multi-threaded
    By Dino in forum C Programming
    Replies: 11
    Last Post: 03-23-2008, 01:14 PM
  4. Threaded models under VC++?
    By zopiro in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2007, 11:42 PM
  5. Replies: 2
    Last Post: 07-23-2002, 11:27 PM

Tags for this Thread