Here is a very simple example of how to use MathLink for this sort of communication. I will use normal code blocks for kernel A and quoted code blocks for kernel B. You must evaluate these in the order shown.
link = LinkCreate["a uniquely named link"];
link = LinkConnect["a uniquely named link"];
LinkActivate[link];
MathLink`AddSharingLink[link];
LinkActivate[link];
SetAttributes[remoteEvaluate, HoldAllComplete];
remoteEvaluate[expr_] := (
LinkWrite[link, Unevaluated@EvaluatePacket[expr]]; LinkFlush[link];
First@Cases[
While[Sow[#, Head[#]] &[LinkRead[link]]; LinkReadyQ[link]] ~Reap~ ReturnPacket,
ReturnPacket[result_] :> result, {3}
]
);
The idea is that the result of evaluating expr on kernel B comes back to kernel A as the body of a ReturnPacket. Other types of packets may also be produced (for example, ExpressionPackets are generated when something is Printed), but we ignore these for present purposes.
Now:
var = Range[5];
var
(* -> var *)
remoteEvaluate[var]
(* -> {1, 2, 3, 4, 5} *)
If we care to check, we will also find that var did not suffer the indignity of unpacking and was quite unmolested in general by this process.
One important point to note is that it is not legal to call MathLink`AddSharingLink on the same link from two different kernels, as this causes a deadlock. Therefore, if you want to share data in a symmetric fasion, you will have to set up a link from A to B and another link from B to A. Although links are in fact bidirectional, this sort of communication requires matching reads and writes to be placed on the link in the appropriate sequence. Unless A and B are both being controlled by C, it will be easier in many cases to defer to the main loop as shown above, because although it renders each link effectively unidirectional, it also enables single-ended communication.
CurrentValue[$FrontEndSession,{TaggingRules, ...}]– Mike Honeychurch Nov 5 '12 at 21:58