3

I want to write a program to monitor windows clipboard using C#. I found some post about this topic. According thread How to monitor clipboard content changes in C#? and Finding the handle to a WPF window, I write a demo using WPF. In all samples code I found, all of them are WinForm or WPF apps, and win32 api they interop with need window handle as parameters. Such as api function SetClipboardViewer(HWND hWndNewViewer)

But in my scenario, I need my program run background as a service to monitor and collect clipboard content. How to monitor clipboard without window UI?

Could you give me some suggestions? Thanks in advance.


According user1795804's suggestion, I write following test code

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    public static class User32
    {


  [DllImport("User32.dll")]
    public static extern IntPtr OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("User32.dll")]
    public static extern IntPtr GetClipboardData(uint uFormat);
}
class Program
{
    static void Main(string[] args)
    {
        int result = (int)User32.OpenClipboard(new IntPtr(0));
        if (result == 0)
        {
            Console.WriteLine("error");
        }
        else
        {
            Console.WriteLine("success");
        }

        int returnHandle = (int)User32.GetClipboardData(1); //CF_TEXT 1
        if (returnHandle == 0)
        {
            Console.WriteLine("can't get text data");
        }

        Console.ReadKey();
    }
    }
}

The result is I can open clipboard and seem to get a handle to date object. But now I have two issue.

1. Although I have a handle to data object in clipboard, how can I get this data using handle? I can't find related function.

2. I need pass a proc function as callback so that it can receive message when system event raise. But I can't find counterpart in non-window app.

Community
  • 1
  • 1
KyL
  • 987
  • 12
  • 24

1 Answers1

1

According to Microsoft, "There are three ways of monitoring changes to the clipboard. The oldest method is to create a clipboard viewer window. Windows 2000 added the ability to query the clipboard sequence number, and Windows Vista added support for clipboard format listeners. Clipboard viewer windows are supported for backward compatibility with earlier versions of Windows. New programs should use clipboard format listeners or the clipboard sequence number."

This GetClipboardSequenceNumber does not take any arguments and according to Microsoft, "The system keeps a serial number for the clipboard for each window station. This number is incremented whenever the contents of the clipboard change or the clipboard is emptied. You can track this value to determine whether the clipboard contents have changed and optimize creating DataObjects. If clipboard rendering is delayed, the sequence number is not incremented until the changes are rendered."

This would fulfill your requirement of "I want to write a program to monitor windows clipboard using C#".

  • It also seems like, though I haven't tried it, that you could use HANDLE WINAPI GetClipboardData() to get the text if the above indicates a change. Hope this helps!! –  Jul 04 '13 at 03:42
  • Before using function [GetClipboardData](http://msdn.microsoft.com/en-us/library/windows/desktop/ms649039(v=vs.85).aspx), "the clipboard must have been opened previously" using function [GetClipboardData(HWND hWndNewOwner)](http://msdn.microsoft.com/en-us/library/windows/desktop/ms649048(v=vs.85).aspx), it need handle as parameter. It seems that we must find other ways. – KyL Jul 04 '13 at 04:05
  • Forgive me, but I don't see where it says it must be opened using GetClipboardData(). If It can be opened using OpenClipboard, maybe you an use it. Notice, it says that if null is passed to it, a clipboard is associated with the current task. BOOL WINAPI OpenClipboard( _In_opt_ HWND hWndNewOwner ); Parameters: A handle to the window to be associated with the open clipboard. If this parameter is NULL, the open clipboard is associated with the current task. –  Jul 04 '13 at 04:16
  • I'm not sure what that means "the current task", but it might be something to explore. –  Jul 04 '13 at 04:20
  • Sorry, I typed wrong. My meaning is "using OpenClipboard(HWND hWndNewOwner). I will try it and hope it works. – KyL Jul 04 '13 at 05:03