Say I receive a WinDef.POINT
using User32.INSTANCE.GetCursorPos
. How can pass this WinDef.POINT
to a function that requires a WinDef.POINT.ByValue
?
The WinDef.POINT.ByValue
class includes a Pointer
constructor.
You can retrieve the Pointer
to the POINT
structure you received using the Structure
class method getPointer()
.
So a type safe way of doing it that I'd probably do:
WinDef.POINT thePoint = new WinDef.POINT();
User32.INSTANCE.GetCursorPos(thePoint);
WinDef.POINT.ByValue thePointByVal = new WinDef.POINT.ByValue(thePoint.getPointer());
passThePointToTheOtherFunction(thePointByVal);
Knowing the inner workings of the class, I can see the nested ByValue
class extends the POINT
class without changing anything about its fields, so simple typecasting would work here, although may be considered a code smell:
WinDef.POINT thePoint = new WinDef.POINT();
User32.INSTANCE.GetCursorPos(thePoint);
passThePointToTheOtherFunction((WinDef.POINT.ByValue) thePoint);
Upcasting would work as well is probably "safer" but since we're still intentionally causing different behavior it's relying on implementation details and may also be a code smell:
WinDef.POINT.ByValue thePoint = new WinDef.POINT.ByValue();
// Explicitly upcast to trigger default ByReference behavior
User32.INSTANCE.GetCursorPos((WinDef.POINT) thePoint);
passThePointToTheOtherFunction(thePoint);