How to check which application has the clipboard hold?

The Clipboard API dates from Windows 3.0 (or before?) and is badly designed. Unfortunately, instead of having get/set primitives, it uses open/close, which makes it possible for applications to hold its access for far too long. Some improvement was brought by Vista to the handling of the viewers chain, but no new API.

With the existing API, it is possible to identify the owner of the clipboard only if that owner also has at least one open window. If the owner has no windows, then one is out of luck.

In the thread Why has my clipboard stopped working?, Jay Parzych has contributed the following vbs code where the GetClipboardLocker function returns the file-name of the process holding the clipboard :

<DllImport("user32.dll")> _
Public Function GetOpenClipboardWindow() As IntPtr
   End Function
 <DllImport("user32.dll", SetLastError:=True)> _
   Public Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer
   End Function
Public Function GetClipboardLocker() As String
       Dim hwnd As IntPtr = GetOpenClipboardWindow()
       If hwnd <> IntPtr.Zero Then
           Dim processId As Integer
           GetWindowThreadProcessId(hwnd, processId)
           Dim p As Process = Process.GetProcessById(processId)
           GetClipboardLocker = p.Modules(0).FileName
       Else
           GetClipboardLocker = String.Empty
       End If
   End Function

A similar C# function can be found in the post Get Clipboard owners Title/Caption.