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