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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. using HWND vs CWnd* with MFC.

using HWND vs CWnd* with MFC.

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studiographicstutorialquestion
6 Posts 4 Posters 1 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.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    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.

    J Z 2 Replies Last reply
    0
    • M Maximilien

      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.

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      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 given HWND. So, I'd say you're better off using CWnd *s. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      D 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        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 given HWND. So, I'd say you're better off using CWnd *s. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        D Offline
        D Offline
        David Salter
        wrote on last edited by
        #3

        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.

        J 1 Reply Last reply
        0
        • D David Salter

          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.

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          It is the mapping HWND->CWnd that does not hold across threads, not the other way around. Handling a CWnd object from a thread other than the one which created the CWnd is just fine (modulo concurrency problems). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          1 Reply Last reply
          0
          • M Maximilien

            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.

            Z Offline
            Z Offline
            Zoltan Csizmadia
            wrote on last edited by
            #5

            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

            J 1 Reply Last reply
            0
            • Z Zoltan Csizmadia

              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

              J Offline
              J Offline
              Joaquin M Lopez Munoz
              wrote on last edited by
              #6

              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 a CWnd * obtained via CWnd::FromHandle is right? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

              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