For Programmers: Free Programming Magazines  


Home > Archive > C# > June 2006 > Reading clipboard data crashes Explorer?!









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Reading clipboard data crashes Explorer?!
Shell

2006-06-28, 4:00 am

Hi all,

I'm in the middle of writing a namespace extension in C#. I'm trying to
process cut/copy/paste from Explorer onto my extension. To figure out
whether the user was trying to cut or copy a file, I need to see the
"Preferred DropEffect" datta that Explorer puts onto the clipboard.
Fair enough, so this is what I do:

dataObject = Clipboard.GetDataObject();
object o = dataObject.GetData("Preferred DropEffect");

if (o is MemoryStream)
{
MemoryStream stream = (MemoryStream)o;
effects = (DragDropEffects)stream.ReadByte();
stream.Position = 0;
}

Strange things happen in these seemingly simple lines...

When I first get the stream, I take a look at its properties through
QuickWatch and can see that its capacity and length are 1. This is
expected -- Explorer is supposed to put a single byte in there that
tells me what the drop effect is supposed to be.

So I read the byte. If I don't position the stream back to the origin,
Explorer will die because apparently, it references it again once the
drop is complete. I figure that happens because reading the memory
stream actually moves the pointer forward. So I position the stream
back to 0. Here's the weird thing -- if I take a look at the stream's
properties now, it says that the length and capacity are 4!
stream.S() does the same thing. Is this right? Doesn't make much
sense to me.

Anyway, this code runs through just fine for a few iterations. After
that, Explorer just dies. I know for sure that this piece of code is
the culprit because without it, all's well. Uncomment these lines and
it's a pandora's box of errors.

Why is rewinding the stream not working as expected? Is there any other
reliable way for me to get the kind of drop that the user is trying to
perform?

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com