Click here to Skip to main content
15,896,557 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: function pointer Pin
Nish Nishant6-Mar-02 16:39
sitebuilderNish Nishant6-Mar-02 16:39 
GeneralRe: function pointer Pin
Steve Severance7-Mar-02 10:28
Steve Severance7-Mar-02 10:28 
GeneralMulti-threading Pin
amarg6-Mar-02 7:12
amarg6-Mar-02 7:12 
GeneralRe: Multi-threading Pin
Nish Nishant6-Mar-02 7:54
sitebuilderNish Nishant6-Mar-02 7:54 
GeneralRe: Multi-threading Pin
Joaquín M López Muñoz6-Mar-02 9:23
Joaquín M López Muñoz6-Mar-02 9:23 
GeneralRe: Multi-threading Pin
Morozov Alexey6-Mar-02 13:13
Morozov Alexey6-Mar-02 13:13 
GeneralA solution Pin
amarg7-Mar-02 7:04
amarg7-Mar-02 7:04 
GeneralRe: Multi-threading Pin
Stephen C. Steel8-Mar-02 8:39
Stephen C. Steel8-Mar-02 8:39 

A CWnd is really two distinct entities: a C++ object which you access using a CWnd* (typically this), and the Window object created by the system, which you access using a window hande (HWND). In order for MFC to make these two separate entities work like one, it must frequently find the HWND associated with a particular CWnd object, and or find the CWnd object associated with a particular HWND.

The first operation, mapping from CWnd to the associated HWND is easy (just look up the m_hWnd member variable). This works fine in any thread.

The second mapping, from HWND to CWnd* uses what is know as a handle map (a table which tracks which HWNDs were created by CWnd objets in that thread). This handle map is thread specific. Furthermore, since you can create HWNDs without even creating a CWnd object (e.g. dialog controls created by the dialog template), MFC sometimes creates generic temporary CWnd objects which you can use for the duration of a message handler to access these HWNDs via a CWnd object.

The GetParent() member function just calls the Windows API funtion ::GetParent (m_hWnd), which returns the HWND of the parent. MFC then uses the (thread specific) handle maps to get a CWnd object associated with the parent HWND. If the parent window was not created via a CWnd object in the same thread, this will a temporary generic CWnd object. So in your case, when the second thread calls GetParent() it gets a pointer to a temporary generic CWnd and NOT the CMyCWndDerivedClass object that created the parent HWND in the first thread. If you cast the CWnd pointer to a pointer your derived class, you'll acess whatever memory just happens to sit after the temporary CWnd object.

You can confirm this with

CWmd *pWnd = GetParent ();
ASSERT_KINDOF (CMyCWndDerivedClass, pWnd);

This will work in your main thread, but fail in your derived thread.

When working with multiple threads, it is much simpler to have only the original main thread responsible for the GUI. Worker threads can update data, and then use ::PostMessage() with an HWND to send a message the main thread (that created the HWND) that it should update its appearance. Since all windows messages are handled by the single GUI thread that creates all HWNDs, the handle maps work just as expected in a single threaded application. It you let multiple threads deal with HWNDs and CWnd objects directly, then you need to understand temporary and permanent handle maps AND when and how they are used by MFC. Believe me, the first approach is much simpler.


Stephen C. Steel
Kerr Vayne Systems Ltd.


GeneralChange Controls Size?! Pin
6-Mar-02 6:52
suss6-Mar-02 6:52 
GeneralRe: Change Controls Size?! Pin
Andres Manggini6-Mar-02 9:38
Andres Manggini6-Mar-02 9:38 
GeneralRe: Change Controls Size?! Pin
Carlos Antollini6-Mar-02 9:55
Carlos Antollini6-Mar-02 9:55 
GeneralRe: Change Controls Size?! Pin
moliate6-Mar-02 10:29
moliate6-Mar-02 10:29 
GeneralRe: Change Controls Size?! Pin
7-Mar-02 2:34
suss7-Mar-02 2:34 
GeneralExternal Control of VC++ Interface Pin
Blade[DMS]6-Mar-02 6:10
Blade[DMS]6-Mar-02 6:10 
Generalagain anchor property Pin
Mazdak6-Mar-02 6:05
Mazdak6-Mar-02 6:05 
QuestionHow do I pass arguments to a view? Pin
6-Mar-02 5:03
suss6-Mar-02 5:03 
AnswerRe: How do I pass arguments to a view? Pin
Tomasz Sowinski6-Mar-02 5:53
Tomasz Sowinski6-Mar-02 5:53 
GeneralRe: How do I pass arguments to a view? Pin
6-Mar-02 6:26
suss6-Mar-02 6:26 
AnswerRe: How do I pass arguments to a view? Pin
Joaquín M López Muñoz6-Mar-02 5:57
Joaquín M López Muñoz6-Mar-02 5:57 
GeneralRe: How do I pass arguments to a view? Pin
6-Mar-02 7:13
suss6-Mar-02 7:13 
GeneralRe: How do I pass arguments to a view? Pin
Joaquín M López Muñoz6-Mar-02 9:00
Joaquín M López Muñoz6-Mar-02 9:00 
AnswerRe: How do I pass arguments to a view? Pin
Jonathan Craig6-Mar-02 8:49
Jonathan Craig6-Mar-02 8:49 
GeneralDoc/View borders Pin
Derek Lakin6-Mar-02 4:16
Derek Lakin6-Mar-02 4:16 
GeneralRe: Doc/View borders Pin
Joaquín M López Muñoz6-Mar-02 5:46
Joaquín M López Muñoz6-Mar-02 5:46 
GeneralRe: Doc/View borders Pin
Derek Lakin6-Mar-02 7:06
Derek Lakin6-Mar-02 7:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.