Casting struct to different struct type

Is there a way to cast from one struct type to another? The CRuntime.cast functions are only for casting to/from VoidPtr and OpaquePointer types, and thus don’t work with normal Ptr types. I’m trying to pass a sockaddr_in to SCNetworkReachabilityCreateWithAddress, which requires a sockaddr, and I can’t seem to figure out how to get natj to do this, since all the constructors which take Pointer objects are protected, so I can’t just create a weak pointer to the address and pass it to sockaddr’s constructor or something of the sort.

Hi Kevin,

We are going to add a cast feature to 1.3, but until then here is a code fragment that might be useful as a temporary workaround:

    sockaddr_in addr_in = ...;
    Pointer peer = addr_in.getPeer();
    Constructor<sockaddr> constr = null;
    sockaddr addr = null;
    try {
        constr =sockaddr.class.getDeclaredConstructor(Pointer.class);
        constr.setAccessible(true);
        addr = constr.newInstance(peer);
    } catch (Exception e) {
        e.printStackTrace();
        ...
    }

I see, thanks a lot for the workaround. I wasn’t sure if there was some non-reflection solution that I wasn’t seeing. Good to know this’ll eventually be fixed as well, thanks.