using HWND vs CWnd* with MFC.
-
I always used CWnd when working with MFC, never really used HWND as I never really programmed fully Win32 applications. Is there an advantage to use HWND to keep objects in containers ( for example ? ) instead of CWnd* ?
std::vector<HWND> hwndVector;
std::vector<CWnd*> cwndVector;Thanks. Max.
-
I always used CWnd when working with MFC, never really used HWND as I never really programmed fully Win32 applications. Is there an advantage to use HWND to keep objects in containers ( for example ? ) instead of CWnd* ?
std::vector<HWND> hwndVector;
std::vector<CWnd*> cwndVector;Thanks. Max.
As for the efficiency, none at all (both types of objects occupy the same space). Using
HWND
will tend to make things more complicat, specially in multithreaded code, where it is not easy to retrive the "true"CWnd
attached to a givenHWND
. So, I'd say you're better off usingCWnd *
s. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
As for the efficiency, none at all (both types of objects occupy the same space). Using
HWND
will tend to make things more complicat, specially in multithreaded code, where it is not easy to retrive the "true"CWnd
attached to a givenHWND
. So, I'd say you're better off usingCWnd *
s. Joaquín M López Muñoz Telefónica, Investigación y DesarrolloJoaquín M López Muñoz wrote: Using HWND will tend to make things more complicat, specially in multithreaded code But if you're passing Window handles between threads you have to use HWNDs as the CWnd/HWND mapping doesn't go across the thread boundary properly.
-
Joaquín M López Muñoz wrote: Using HWND will tend to make things more complicat, specially in multithreaded code But if you're passing Window handles between threads you have to use HWNDs as the CWnd/HWND mapping doesn't go across the thread boundary properly.
It is the mapping
HWND
->CWnd
that does not hold across threads, not the other way around. Handling aCWnd
object from a thread other than the one which created theCWnd
is just fine (modulo concurrency problems). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
I always used CWnd when working with MFC, never really used HWND as I never really programmed fully Win32 applications. Is there an advantage to use HWND to keep objects in containers ( for example ? ) instead of CWnd* ?
std::vector<HWND> hwndVector;
std::vector<CWnd*> cwndVector;Thanks. Max.
If you have a single-threaded application storing CWnd* is more practical, easier to use. You CANNOT use the CWnd* in a different thread, than the thread, where the given CWnd* was created. What you can do is you pass the HWND for the given window to the thread, and in that thread you create your CWnd* using CWnd::FromHandle(hwnd), after that, you can use this CWnd* in this thread. So it's more practical to store HWND in this kind of environment. I could give you some examples, when you could use CWnd* in a multithreaded application, but it's very wrong. Zolee
-
If you have a single-threaded application storing CWnd* is more practical, easier to use. You CANNOT use the CWnd* in a different thread, than the thread, where the given CWnd* was created. What you can do is you pass the HWND for the given window to the thread, and in that thread you create your CWnd* using CWnd::FromHandle(hwnd), after that, you can use this CWnd* in this thread. So it's more practical to store HWND in this kind of environment. I could give you some examples, when you could use CWnd* in a multithreaded application, but it's very wrong. Zolee
You CANNOT use the CWnd* in a different thread, than the thread, where the given CWnd* I do not agree with that. Can you give some example in which using a
CWnd *
across threads is wrong and doing the same with aCWnd *
obtained viaCWnd::FromHandle
is right? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo