2

Mysterious clipboard issue:

I have an application under development that launches an external application (as a System.Diagnostics.Process object) which is expected to copy some data (text) to the clipboard. When the external app closes, the client application retrieves the text from the clipboard.

The problem is that although once the external application has copied the text to the clipboard I can paste it into, say, notepad, the client app is getting an empty string from the clipboard.

external app code:

    private void btn1_Click(object sender, EventArgs e)
    {
        //copy text pane to clipboard
        DataObject obj = new DataObject();
        obj.SetData(tbText.Text);
        System.Windows.Forms.Clipboard.SetDataObject(obj);
    }

Client app code:

        string returnedValues = string.Empty;

        System.Windows.Forms.IDataObject data = System.Windows.Forms.Clipboard.GetDataObject();
        if (data != null && data.GetDataPresent(System.Windows.Forms.DataFormats.Text) == true)
        {
            returnedValues = (string) data.GetData(System.Windows.Forms.DataFormats.Text, true));
        }

The data object is always null, even though the clipboard has the text in it and I can paste it into other applications.

Can anyone point me at the flaw in the client app code? Why is 'data' always null, even though there is data on the clipboard?

haughtonomous
  • 4,602
  • 11
  • 34
  • 52
  • I have tried to run your code and it works just fine and gets any text on the clipboard. However, an application that copies data to the clipboard may decide to clear the clipboard when it exits but I assume that is not the source of your problem because you are able to paste the text into Notepad. – Martin Liversage Dec 10 '12 at 13:47
  • The copying application I am using at the moment is a very simple Windows Forms test application that simply does a Clipboard.SetText(tbTextBox.text) operation. It doesn't explicitly clear the clipboard anywhere, ans as you point out, I can paste into notepad after the test app has been closed, but my client app retrieves an empty string! – haughtonomous Dec 10 '12 at 14:49
  • Use DataFormats.UnicodeText instead. Fall in the pit of success by using the convenience methods, Clipboard.SetText() and GetText(). – Hans Passant Dec 10 '12 at 15:38
  • It's the data object which is null - there is nothing in it to apply DataFormats.UnicodeText to. – haughtonomous Dec 10 '12 at 16:38

2 Answers2

1

When all your clipboard has is an string copied as text, you have to retrieve it as

Clipboard.GetText()

and to retrieve other types of objects you can use GetData()

Jorge Alvarado
  • 2,664
  • 22
  • 33
  • Changing to SetText and GetText makes no difference. Clipboard.GetText() returns an empty string even though I can paste from clipboard to notepad. Could this be some cross-application boundary issue? – haughtonomous Dec 10 '12 at 14:39
  • no, I don't think so, clipboard is one of the most basic functions ever. Are you sure you are saving the content of GetText into variable of type String and not to an Object? – Jorge Alvarado Dec 10 '12 at 15:05
  • Yes: string returnedValues=""; if (System.Windows.Forms.Clipboard.ContainsText()) returnedValues = System.Windows.Forms.Clipboard.GetText(); – haughtonomous Dec 10 '12 at 16:36
  • Can I be clear about something? It is the DataObject object that is null, and that is why I am not able to retrieve anything from the clipboard. The question is why is this object null? The code is: ` string returnedValues = string.Empty; System.Windows.Forms.IDataObject data = System.Windows.Forms.Clipboard.GetDataObject(); if (data != null && data.GetDataPresent(System.Windows.Forms.DataFormats.Text) == true) { returnedValues = (string) data.GetData(System.Windows.Forms.DataFormats.Text, true)); }` – haughtonomous Dec 11 '12 at 09:06
  • Using SetText() and GetText() makes no difference - there is nothing there (although I can still paste into Notepad!) – haughtonomous Dec 11 '12 at 09:12
  • More information: even if I copy to the clipboard from say Notepad, my app cannot retrieve text from the clipboard, whether I use Clipboard.GetText() with or without arguments, or Clipboard.GetDataObject(). That means I cannot meet a primary objective, so is there an alternative approach I could fall back on using C# .Net3.5? What factors could be blocking access to the clipboard? (My app runs in a STA thread - I've checked). – haughtonomous Dec 12 '12 at 09:33
0

For text, you really should use SetText() and GetText(). And make sure that overwriting the current content of the clipboard is what the user expects in your scenario. If not, you should use interprocess communication instead, see e.g. What is the simplest method of inter-process communication between 2 C# processes?

Community
  • 1
  • 1
Sebastian Negraszus
  • 11,915
  • 7
  • 43
  • 70