Click here to Skip to main content
15,917,514 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralDesign opinions/help needed Pin
Steve Messer10-Nov-03 16:06
Steve Messer10-Nov-03 16:06 
GeneralRe: Design opinions/help needed Pin
Antti Keskinen11-Nov-03 11:21
Antti Keskinen11-Nov-03 11:21 
GeneralRe: Design opinions/help needed Pin
Steve Messer11-Nov-03 18:05
Steve Messer11-Nov-03 18:05 
GeneralRe: Design opinions/help needed Pin
Antti Keskinen12-Nov-03 8:25
Antti Keskinen12-Nov-03 8:25 
GeneralRe: Design opinions/help needed Pin
Steve Messer12-Nov-03 12:07
Steve Messer12-Nov-03 12:07 
GeneralRe: Design opinions/help needed Pin
Antti Keskinen13-Nov-03 0:44
Antti Keskinen13-Nov-03 0:44 
GeneralRe: Design opinions/help needed Pin
Steve Messer13-Nov-03 1:35
Steve Messer13-Nov-03 1:35 
GeneralRe: Design opinions/help needed Pin
Antti Keskinen13-Nov-03 5:17
Antti Keskinen13-Nov-03 5:17 
In short, you can pass the HWND of the Windows object bound to a CWnd from the DLL back to the main application. This is legal. Now for some explanation:

I ran into a strange problem while creating the example app, and when debugging, my application fired an assertion as I created the CDialog in the DLL and then passed the active C++ object's address to the main application. Here, the term "active" refers to an MFC object means that is bound to an existing Windows window/GDI/whatever object. An "inactive" object would be one that has not had it's 'Create' method (or any other initialization method) called.

Here is an exerpt from wincore.cpp, a MFC source file which defines the behaviour of CWnd::AssertValid, among other things.

C++
    ASSERT((p = pMap->LookupPermanent(m_hWnd)) != NULL ||
	   (p = pMap->LookupTemporary(m_hWnd)) != NULL);
    ASSERT((CWnd*)p == this);   // must be us
<DIV>
    // Note: if either of the above asserts fire and you are
    // writing a multithreaded application, it is likely that
    // you have passed a C++ object from one thread to another
    // and have used that object in a way that was not intended.
    // (only simple inline wrapper functions should be used)
    //
    // In general, CWnd objects should be passed by HWND from
    // one thread to another.  The receiving thread can wrap
    // the HWND with a CWnd object by using CWnd::FromHandle.
    //
    // It is dangerous to pass C++ objects from one thread to
    // another, unless the objects are designed to be used in
    // such a manner.
</DIV>

So, like you see on the commented lines, passing the addresses of created C++ objects from one thread to another is a dangerous business, especially if the object is bound to a Windows GDI object already.

The reasons for this are in the core functionality of the MFC. Like you might already know, MFC is just a (huge) collection C++-based classes, which encapsulate the functionality of the major Windows base services (Windows, pens, brushes, DCs, sockets and so on). This is why it is so difficult for most people to make the difference between MFC and the Windows API.

Building on this theory, you now understand why the passing of an active CWnd object's address is dangerous: the object itself is created on a seperate thread, and if you call it's methods, they are executed in the context of the seperate thread as well. The Windows window object bound to the CWnd exists on the seperate thread as well. So, if you were to use the DLL's existing, active CWnd object's address or a reference to it in order to construct a new CWnd object in the main application, you would most likely be greeted by an access violation. Cross-thread communication is very vulnerable to causing those. However, you can store the object's address and call it's methods through a pointer. This is completely fine and legal Smile | :)

Alternatively, you can always use a simple HWND variable in the main application, and use Windows API's SendMessage to post messages to that HWND.
Just create an exported function to the DLL which returns the HWND of the Windows window bound to the CDialog/CWnd object in question. Then call this function through a pointer from the main thread, and vot, you have the HWND.

MFC Reference, however, cautions us from binding two CWnd objects to a same HWND handle, as this may result in serious implications if one of the object is destroyed while the other is still attached to the Windows window object. The destructor of CWnd, by default, attempts to destroy the underlying Windows window object as well. So, if you pass the HWND, use the API's SendMessage to send your message to the window. Do not wrap the HWND into a new CWnd object.

The most safest way is to use pointers. Cross-thread pointers are a very safe way to exchange information between two (or more) threads.

-Antti Keskinen


----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
GeneralRe: Design opinions/help needed Pin
Steve Messer13-Nov-03 7:18
Steve Messer13-Nov-03 7:18 
GeneralOnGridEndEdit and MessageBox Pin
adonisv10-Nov-03 12:48
adonisv10-Nov-03 12:48 
GeneralFILETIME compilation error Pin
rmnowick10-Nov-03 12:31
rmnowick10-Nov-03 12:31 
GeneralRe: FILETIME compilation error Pin
Dave Bryant10-Nov-03 14:04
Dave Bryant10-Nov-03 14:04 
QuestionHow to change a toolbar bitmap image? Pin
ElizabethC10-Nov-03 11:08
ElizabethC10-Nov-03 11:08 
AnswerRe: How to change a toolbar bitmap image? Pin
Roger Allen11-Nov-03 0:40
Roger Allen11-Nov-03 0:40 
GeneralRe: How to change a toolbar bitmap image? Pin
ElizabethC12-Nov-03 6:47
ElizabethC12-Nov-03 6:47 
GeneralRe: How to change a toolbar bitmap image? Pin
Roger Allen12-Nov-03 6:49
Roger Allen12-Nov-03 6:49 
GeneralRe: How to change a toolbar bitmap image? Pin
ElizabethC12-Nov-03 10:49
ElizabethC12-Nov-03 10:49 
Generalopengl with visual c++ Pin
bik10-Nov-03 10:48
bik10-Nov-03 10:48 
GeneralRe: opengl with visual c++ Pin
Andrew Walker10-Nov-03 12:24
Andrew Walker10-Nov-03 12:24 
GeneralRe: opengl with visual c++ Pin
Orhun Birsoy10-Nov-03 13:07
Orhun Birsoy10-Nov-03 13:07 
Generalfile system in Mac Pin
pnpfriend10-Nov-03 10:31
pnpfriend10-Nov-03 10:31 
GeneralRe: file system in Mac Pin
Johnny ²10-Nov-03 11:24
Johnny ²10-Nov-03 11:24 
GeneralRe: file system in Mac Pin
pnpfriend12-Nov-03 4:00
pnpfriend12-Nov-03 4:00 
GeneralFile out Pin
Anonymous10-Nov-03 9:30
Anonymous10-Nov-03 9:30 
GeneralRe: File out Pin
Shay Harel10-Nov-03 9:45
Shay Harel10-Nov-03 9:45 

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.