1

My project is about text processing software and using InputSimulator library.

When my program simulate Ctrl+C and get text from clipboard, It gave an exception like this.

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in     PresentationCore.dll

Additional information: OpenClipboard Failed (Exception from HRESULT:0x800401D0 (CLIPBRD_E_CANT_OPEN))

here's my project and here's my code

        System.Threading.Thread.Sleep(3000); // wait for opening notepad and type any text to test
        while (true)
        {
            string oldclipboard = Clipboard.GetText();
            InputSimulator input = new InputSimulator();
            input.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_A); // select text

            input.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
            string highlightedtext = Clipboard.GetText();
            //do sth.(text processing)
            input.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_V);
        }

I'm sure that it is about conflict between the library and clipboard. but I don't know what is the problem.

So, Why it is throwing an exception? and Is there any alternative way to to this?

Thanks

P.S. I've also try "Sendkeys.Sendwait". It didn't give any exception but it is unstable.

doliolarzz
  • 45
  • 1
  • 8
  • Please copy your exception in your answer instead of linking externally. – SaeX Dec 04 '15 at 08:58
  • Please show code and error inline as text in the post. – Alexei Levenkov Dec 04 '15 at 08:58
  • There is a solution very close to what you need here: http://www.codeproject.com/Questions/205820/How-can-I-use-Csharp-Clipboard-to-get-selected-tex – Murray Foxcroft Dec 04 '15 at 09:23
  • I don't want to use sendkeys because sometimes it won't send a keystroke and unstable even I've try this. – doliolarzz Dec 04 '15 at 09:26
  • I've tried your example and it looks like you're calling clipboard functions to often. If I add `System.Threading.Thread.Sleep(5000);` before end of `while(true)` statement, error does not occur. You should probably catch exception and retry if it fails. – Jure Dec 04 '15 at 09:49
  • Could be that your library or another program is blocking the clipboard. See here: http://stackoverflow.com/questions/930219/how-to-handle-blocked-clipboard-and-other-oddities – Ketobomb Dec 04 '15 at 09:56
  • @bidy I found that Notepad is blocking clipboard. So I've try "while (GetOpenClipboardWindow() != IntPtr.Zero)" It won't exit the loop and my program was freeze. I've also try Closeclipboard win32api but result is the same. – doliolarzz Dec 04 '15 at 13:09
  • @Jure It's real time program so it's can't be wait. When I use sendkeys, I don't have to delay to do things with clipboard. – doliolarzz Dec 04 '15 at 13:17

0 Answers0