Clipboard SetData / GetData troubles with VPC and TS

24 באוקטובר 2007

תגיות: ,
2 תגובות

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/clipboard-setdata-getdata-troubles-with-vpc-and-ts/]


This one of those posts you can categorize is WTF (see me WPF presentation to know what is it) it is not working. However, you, probably will face with the problem now or after.

Symptoms: You are trying to set and get clipboard data from Virtual PC or remote computer by using Terminal Services and nothing happens. When you are using .NET, you're getting Unhandled exception. If you'll dig deeper, you'll find 0x800401D0 (CLIPBRD_E_CANT_OPEN) exception.

Reason: Passing data through TS or VPC pipes bug

Solution: Just retry a couple of times.

Actually, in unmanaged and COM world, you'll never face this problem, because of the way we are handling OleSetClipboard() and OleGetClipboard() methods (which wrapped by .NET Clipboard.GetData/Clipboard.SetData Set/Get Text etc. methods).  So, something like following functions will solve your problems.

public static IDataObject GetRemoteClipboardDataObject()
        {
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    return Clipboard.GetDataObject();
                }
                catch { }
                System.Threading.Thread.Sleep(100);
            }
        }

public static void SetRemoteClipboardDataObject(object data)
        {
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    Clipboard.SetDataObject(data);
                    return;
                }
                catch { }
                System.Threading.Thread.Sleep(100);
            }
        }

Now, to be honest with myself, take a look into one of Clipboard.SetDataObject method signatures in .NET framework 2.0

//Attempts to place data on the system Clipboard the specified number of times and with the specified delay between attempts, optionally leaving the data on the Clipboard after the application exits. 
public static void SetDataObject (
    Object data,
    bool copy,
    int retryTimes,
    int retryDelay
)
Don't it looks familiar? :) Have a nice day and be honest with yourselves.
הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *

2 תגובות

  1. Chris23 ביוני 2008 ב 18:30

    That is pretty nasty one. Shame we need to use a hack as the solution.

    הגב
  2. glymnDync1 בדצמבר 2008 ב 5:08

    Hello.
    Can anyone utter me how I would get a bother onto my mobby?
    The vade-mecum is useless…

    הגב