Sign in to follow this  
HiPhiSch

Trouble accessing the clipboard.

Recommended Posts

Hi all!

I tried to access the Clipboard in the OnRender section with this:

IDataObject idat;
lock(this) {
idat = Clipboard.GetDataObject();
}
// idat == null <- this happens every time

I used a try block before, the simplified code is just for testing. There is no exception thrown, but I always just get the null, no matter what is stored inside of the clipboard. I also included a demand for UIPermission including AllClipboard. This does not help. I am using VS2008 pro if that matters.

Is it somehow possible to read content from the clipboard?

I hope you can help me out.

Share this post


Link to post
Share on other sites

You can only access the clipboard from an STA thread. I ran into this with some refactoring of the regular Edit->Paste command, in fact.

IDataObject idat = null;
Exception threadEx = null;
Thread staThread = new Thread(
   delegate
   {
       try
       {
           idat = Clipboard.GetDataObject();
       }

       catch (Exception ex) 
       {
           threadEx = ex;            
       }
   });
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join();
// at this point either you have clipboard data or an exception

You probably also have to query the existence of clipboard data within that STA thread, btw.

And, also, don't do this from OnRender(). Do it within OnSetRenderInfo().

Share this post


Link to post
Share on other sites

Man! I've been wondering about this for a while now... :(

Good info. Thanks!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this