hm… one solution is to build yourself two new coordinate systems in both objects (one coordinate system in the origin object and one in the target object). Then, apply a transformation that transforms from the origin coordinate system to the destiny coordinate system.
First you build the two coordinate systems of the objects someway. You will align the coordinate system of the origin object to the coordinate system of the destiny object. When you align these coordinate systems, you will have your objects aligned too.
For both objects, the coordinate systems are composed by three orthogonal normalized vectors (x, y and z vectors) and a point in world coordinates. Like a regular coordinate system, just customized by yourself with some position/rotation for each of your objects (I recomend you to build them with cross product and normalization to ensure they are ortho-normal axis - and it will be nice if you draw them to ensure you are having the right results).
Then, you will build two Matrix4x4, that are matrices that transforms an object from the world coordinate system to the coordinate system you built for each object.
For each coordinate system you built, the Matrix4x4 that transforms from world coordinate system to your custom coordinate system will be the matrix:
M =
[ V1.x V2.x V3.x P.x ]
[ V1.y V2.y V3.y P.y ]
[ V1.z V2.z V3.z P.z ]
[ 0 0 0 1 ]
where V1, V2 and V3 are the three orthogonal normalized vectors that define your custom coordinate system and P is the origin of this custom coordinate system.
So, let’s say that you built the two matrices, M1 that transforms from the coordinate system of the world to the coordinate system of the origin object, and M2 that transforms from the coordinate system of the world to the coordinate system of the destiny object.
Then, the transformation T that transforms from the coordinate system of the origin object to the coordinate system of the target object will be a Matrix4x4 defined as:
T = M2 * M1^(-1)
That is the inverse of M1 (to transform it to the origin of the world) multiplied with M2 to transform it to the destiny coordinate system.
Not sure if that’s what you really need, but it may be helpful.