Page 1 of 1

Clipboard.GetText() not working with Agent?

Posted: Mon Mar 04, 2019 5:57 pm
by semate
Hi there!

Is it me or is Clipboard.GetText() not working when the test application is executed remotely via Agent. Works locally.
Some more details: To get Putty output I use the Copy All To Clipboard Menu function. If I try to access it later with Clipboard.GetText() it comes up with an Error Message - but only if I run it via the Agent - locally it works just fine.

Re: Clipboard.GetText() not working with Agent?

Posted: Mon Mar 04, 2019 9:55 pm
by Support Team
Hi semate,

I did a quick test and was not able to duplicate this behavior. The below code ran perfectly on both my local and agent machines. All runs pulled the different clipboard text for each machine properly. Is something else affecting the clipboard text on the remote machine? What is the error message you are receiving?

Code: Select all

Report.Info(Clipboard.GetText());
Cheers,
Ned

Re: Clipboard.GetText() not working with Agent?

Posted: Tue Mar 05, 2019 9:10 am
by Stub
That happens to me all the time. We're still running Ranorex v8.2.1 (simply because I have not had the time to try to update things) and our tests run on a dedicated physical machine. Clipboard.GetText hurls exceptions so often that what I ended up doing was surrounding the whole clipboard operation in a 'try a few times' loop. If it fails the first time, it appears to succeed on the subsequent attempt.

We also have a lot of problems with Clipboard.SetText throwing, "Requested Clipboard operation did not succeed." This is more down to something locking the clipboard. I've started detecting this issue and reporting on who's locking the clipboard. In our case it's usually TightVNC which I use to connect to the remote machine running our Ranorex tests.

Code: Select all

        public static void HandleClipboardException (Exception exc)
        {
        	Process process_locking_clipboard = GetProcessLockingClipboard();
        	Report.Log(ReportLevel.Error, "Clipboard Error", exc.Message);
        	Report.Log(ReportLevel.Error, "Clipboard Error", $"The process apparently locking the clipboard is: {process_locking_clipboard.ProcessName} ({process_locking_clipboard.Id})");
        }
        
        [DllImport("user32.dll", SetLastError = true)]
	static extern IntPtr GetOpenClipboardWindow();

	[DllImport("user32.dll", SetLastError = true)]
	static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

	private static Process GetProcessLockingClipboard()
	{
    		int processId;
    		GetWindowThreadProcessId(GetOpenClipboardWindow(), out processId);

		return Process.GetProcessById(processId);
	}

Re: Clipboard.GetText() not working with Agent?

Posted: Tue Mar 05, 2019 11:35 am
by ahoisl
Just as a note: The Clipboard class is part of the default .NET Framework, i.e. it is not something Ranorex itself provides.

Anyway, interacting with the clipboard is prone to throwing exceptions, because it is a shared resource for the whole system. The .NET Framework itself already tries to get data from it in a loop; I suggest creating a utility method and doing the same thing again with e.g. 100ms wait time between tries.

Regards,
Alex
Ranorex Team