Jump to content

Trouble accessing the clipboard.


HiPhiSch

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.

Siggi.gif
Link to comment
Share on other sites

Try doing so from either the dialog or OnSetRenderInfo() - i've had problems before accessing the clipboard while another thread in the process is running.

KaHuc.png
Link to comment
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().

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

forumSig_bmwE60.jpg

Link to comment
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.

×
×
  • Create New...