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. Win32 Structures

Win32 Structures

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++json
7 Posts 3 Posters 3 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.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    My knowledge of the low level implementation of C++ structures and classes is limited. But I want to ask someone who might know, is it theoretically possible to derive a class from a Win32 structure and add methods and still be able to pass the new "structure" to a Win32 API? I do know that adding class data members would be out of the question, because that would change the size of the structure. But is adding methods to a structure something I could do and still use it in the windows api?

    The difficult we do right away... ...the impossible takes slightly longer.

    Mircea NeacsuM 1 2 Replies Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      My knowledge of the low level implementation of C++ structures and classes is limited. But I want to ask someone who might know, is it theoretically possible to derive a class from a Win32 structure and add methods and still be able to pass the new "structure" to a Win32 API? I do know that adding class data members would be out of the question, because that would change the size of the structure. But is adding methods to a structure something I could do and still use it in the windows api?

      The difficult we do right away... ...the impossible takes slightly longer.

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      Yes, it is very much possible. You can write something like this:

      class MyPen : public LOGPEN {
      public:
      MyPen (/*args*/);
      void use_pen ();
      private:
      int stuff;
      }

      MyPen the_pen;
      HPEN hp = CreatePenIndirect ((LOGPEN*)&the_pen);

      Note that you can also extend your object with additional data members. They will be placed after the base class (Windows structure) and they will do no harm.

      Mircea

      Richard Andrew x64R 1 Reply Last reply
      0
      • Mircea NeacsuM Mircea Neacsu

        Yes, it is very much possible. You can write something like this:

        class MyPen : public LOGPEN {
        public:
        MyPen (/*args*/);
        void use_pen ();
        private:
        int stuff;
        }

        MyPen the_pen;
        HPEN hp = CreatePenIndirect ((LOGPEN*)&the_pen);

        Note that you can also extend your object with additional data members. They will be placed after the base class (Windows structure) and they will do no harm.

        Mircea

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        Thanks for your reply. I'm excited to try this. Your example is great. But if I wanted to use an array of MyPen's then that would surely cause problems because of the larger size, no?

        The difficult we do right away... ...the impossible takes slightly longer.

        Mircea NeacsuM 1 Reply Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          Thanks for your reply. I'm excited to try this. Your example is great. But if I wanted to use an array of MyPen's then that would surely cause problems because of the larger size, no?

          The difficult we do right away... ...the impossible takes slightly longer.

          Mircea NeacsuM Offline
          Mircea NeacsuM Offline
          Mircea Neacsu
          wrote on last edited by
          #4

          Correct but only if the Windows API call takes an array of (the smaller) structures as parameters. Of top of my head, I cannot think of one that does.

          Mircea

          1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            My knowledge of the low level implementation of C++ structures and classes is limited. But I want to ask someone who might know, is it theoretically possible to derive a class from a Win32 structure and add methods and still be able to pass the new "structure" to a Win32 API? I do know that adding class data members would be out of the question, because that would change the size of the structure. But is adding methods to a structure something I could do and still use it in the windows api?

            The difficult we do right away... ...the impossible takes slightly longer.

            1 Offline
            1 Offline
            11917640 Member
            wrote on last edited by
            #5

            This is the way some MFC classes are implemented:

            typedef struct tagPOINT
            {
            LONG x;
            LONG y;
            } POINT, *PPOINT, NEAR *NPPOINT, FAR *LPPOINT;

            class CPoint :
            public tagPOINT
            {
            ...
            };

            No additional members and virtual functions to get binary compatibility with underlying structure.

            Richard Andrew x64R 1 Reply Last reply
            0
            • 1 11917640 Member

              This is the way some MFC classes are implemented:

              typedef struct tagPOINT
              {
              LONG x;
              LONG y;
              } POINT, *PPOINT, NEAR *NPPOINT, FAR *LPPOINT;

              class CPoint :
              public tagPOINT
              {
              ...
              };

              No additional members and virtual functions to get binary compatibility with underlying structure.

              Richard Andrew x64R Offline
              Richard Andrew x64R Offline
              Richard Andrew x64
              wrote on last edited by
              #6

              Hi, thanks very much for your post. Do I understand correctly that non-virtual functions would ruin binary compatibility?

              The difficult we do right away... ...the impossible takes slightly longer.

              1 1 Reply Last reply
              0
              • Richard Andrew x64R Richard Andrew x64

                Hi, thanks very much for your post. Do I understand correctly that non-virtual functions would ruin binary compatibility?

                The difficult we do right away... ...the impossible takes slightly longer.

                1 Offline
                1 Offline
                11917640 Member
                wrote on last edited by
                #7

                Non-virtual functions don't change a class instance size. You can use this technique, as in existing Microsoft classes like CPoint, CRect, adding instance non-virtual functions and static functions, if necessary.

                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