Rosencrantz and Guildenstern, two friends that Hamlet knows from his youth, are employed by King Claudius to try and find out what is the matter with him. In Act 3, scene 1, they report to Claudius, and also Queen Gertrude, that they have not been able to discover what the problem is with Hamlet. All they really say is that Hamlet certainly appears distracted, but that he is not at all forthcoming:
Nor do we find him forward to be sounded,
But, with a crafty madness, keeps aloof,
When we would bring him on to some confession
Of his true state.
Hamlet, in other words, has not allowed himself to be 'sounded', or examined, by his old friends; he still is cunning enough to thwart all their inquiries; this is what is meant by his 'crafty madness'.
The Queen then asks whether the two men were able to cheer Hamlet up at all, at which they reveal that Hamlet appeared much interested in the arrival of a troupe of actors. The King then tells Rosencrantz and Guildernstern to encourage Hamlet in to watch the play that the actors are going to put on, presumably in a bid to lift his spirits out of his depression.
However, just a couple of scenes later, we see the King plotting with Rosencrantz and Guildernstern to get rid of Hamlet by sending him to England along with them, ultimately to have him killed there. The King is by this time much alarmed at Hamlet's state, which nothing seems to alleviate, and can see no other way to eliminate the problem. However, in the event, Hamlet turns the tables on his one-time friends, and arranges to have them killed instead. Therefore, they fail quite dismally in their job as King Claudius's spies.
They are both escorting Hamlet to see the King and when Hamlet views the letter he automatically assumes that his friends from his childhood plan to kill him to. In his madness, he changes the name from his to his friends so he would be spared from death and makes his friends suffer his fate.