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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why a HANDLE created by CreateFile can be assigned to an object

Why a HANDLE created by CreateFile can be assigned to an object

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
5 Posts 3 Posters 0 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.
  • D Offline
    D Offline
    digitalspace xjtu
    wrote on last edited by
    #1

    Some code segment in chapter 10 of "Windows via C/C++"(5th Edition) is

    // Open the source file without buffering & get its size
    CEnsureCloseFile hFileSrc = CreateFile(pszFileSrc,
    GENERIC_READ, FILE_SHARE_READ, NULL,
    OPEN_EXISTING, FILE_FLAG_NO_BUFFERING |
    FILE_FLAG_OVERLAPPED, NULL);

    Here "CEnsureCloseFile" is a class. The Author define Template class "CEnsureCleanup" which was used to clean some objects, then use a this Template define class CEnsureCloseFile My question is why the HANDLE created by "CreateFile" can be assigned to object hFileSrc . It seems rare. Do you offen do sth like this?

    A C 2 Replies Last reply
    0
    • D digitalspace xjtu

      Some code segment in chapter 10 of "Windows via C/C++"(5th Edition) is

      // Open the source file without buffering & get its size
      CEnsureCloseFile hFileSrc = CreateFile(pszFileSrc,
      GENERIC_READ, FILE_SHARE_READ, NULL,
      OPEN_EXISTING, FILE_FLAG_NO_BUFFERING |
      FILE_FLAG_OVERLAPPED, NULL);

      Here "CEnsureCloseFile" is a class. The Author define Template class "CEnsureCleanup" which was used to clean some objects, then use a this Template define class CEnsureCloseFile My question is why the HANDLE created by "CreateFile" can be assigned to object hFileSrc . It seems rare. Do you offen do sth like this?

      A Offline
      A Offline
      Albert Holguin
      wrote on last edited by
      #2

      Since it's just a pointer, it can be set to anything that can handle that appropriately (the constructor for this object likely takes in that type of HANDLE). His class probably just makes sure a file is properly closed without requiring you to explicitly close the file (open file handles can be an issue). http://en.wikipedia.org/wiki/Assignment_operator_(C%2B%2B)[^]

      D 1 Reply Last reply
      0
      • D digitalspace xjtu

        Some code segment in chapter 10 of "Windows via C/C++"(5th Edition) is

        // Open the source file without buffering & get its size
        CEnsureCloseFile hFileSrc = CreateFile(pszFileSrc,
        GENERIC_READ, FILE_SHARE_READ, NULL,
        OPEN_EXISTING, FILE_FLAG_NO_BUFFERING |
        FILE_FLAG_OVERLAPPED, NULL);

        Here "CEnsureCloseFile" is a class. The Author define Template class "CEnsureCleanup" which was used to clean some objects, then use a this Template define class CEnsureCloseFile My question is why the HANDLE created by "CreateFile" can be assigned to object hFileSrc . It seems rare. Do you offen do sth like this?

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        digitalspace.xjtu wrote:

        why the HANDLE created by "CreateFile" can be assigned to object hFileSrc .

        because the class has an overloaded '=' operator. i don't have the source, but i imagine it looks like this:

        TYPE operator=(TYPE t) {
        Cleanup();
        m_t = (UINT_PTR) t;
        return(*this);
        }

        i do stuff like this all the time. i have dozen of little classes designed to take a handle of some kind in the constructor and then call the appropriate release/delete in the destructor.

        image processing toolkits | batch image processing

        D 1 Reply Last reply
        0
        • A Albert Holguin

          Since it's just a pointer, it can be set to anything that can handle that appropriately (the constructor for this object likely takes in that type of HANDLE). His class probably just makes sure a file is properly closed without requiring you to explicitly close the file (open file handles can be an issue). http://en.wikipedia.org/wiki/Assignment_operator_(C%2B%2B)[^]

          D Offline
          D Offline
          digitalspace xjtu
          wrote on last edited by
          #4

          thinks , got it

          1 Reply Last reply
          0
          • C Chris Losinger

            digitalspace.xjtu wrote:

            why the HANDLE created by "CreateFile" can be assigned to object hFileSrc .

            because the class has an overloaded '=' operator. i don't have the source, but i imagine it looks like this:

            TYPE operator=(TYPE t) {
            Cleanup();
            m_t = (UINT_PTR) t;
            return(*this);
            }

            i do stuff like this all the time. i have dozen of little classes designed to take a handle of some kind in the constructor and then call the appropriate release/delete in the destructor.

            image processing toolkits | batch image processing

            D Offline
            D Offline
            digitalspace xjtu
            wrote on last edited by
            #5

            Oh, it is operator overload , thank you very much

            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