Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read raw (CF_HTML) clipboard data in a webpage or C#?

If I drag and drop a selection of a webpage from Firefox to HTML-Kit, HTML-Kit asks me whether I want to paste as text or HTML. If I select "text," I get this:

Version:0.9
StartHTML:00000147
EndHTML:00000516
StartFragment:00000181
EndFragment:00000480
SourceURL:http://en.wikipedia.org/wiki/Herodotus
<html><body>
<!--StartFragment-->Additional details have been garnered from the <i><a href="http://en.wikipedia.org/wiki/Suda" title="Suda">Suda</a></i>, an 11th-century encyclopaedia of the <a href="http://en.wikipedia.org/wiki/Byzantium" title="Byzantium">Byzantium</a> which likely took its information from traditional accounts.<!--EndFragment-->
</body>
</html>

According to MSDN, this is "CF_HTML" formatted clipboard data. Is it the same on OS X and Linux systems?

Is there any way to access this kind of detailed information (as opposed to just the plain clip fragment) in a webpage-to-webpage drag and drop operation? What about to a C# WinForms desktop application?

like image 859
ine Avatar asked Oct 25 '25 22:10

ine


1 Answers

For completeness, here is the P/Invoke Win32 API code to get RAW clipboard data

using System;
using System.Runtime.InteropServices;
using System.Text;

//--------------------------------------------------------------------------------
http://metadataconsulting.blogspot.com/2019/06/How-to-get-HTML-from-the-Windows-system-clipboard-directly-using-PInvoke-Win32-Native-methods-avoiding-bad-funny-characters.html
//--------------------------------------------------------------------------------

public class ClipboardHelper
{
    #region Win32 Native PInvoke

    [DllImport("User32.dll", SetLastError = true)]
    private static extern uint RegisterClipboardFormat(string lpszFormat);
    //or specifically - private static extern uint RegisterClipboardFormatA(string lpszFormat);

    [DllImport("User32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool IsClipboardFormatAvailable(uint format);

    [DllImport("User32.dll", SetLastError = true)]
    private static extern IntPtr GetClipboardData(uint uFormat);

    [DllImport("User32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("User32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool CloseClipboard();

    [DllImport("Kernel32.dll", SetLastError = true)]
    private static extern IntPtr GlobalLock(IntPtr hMem);

    [DllImport("Kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GlobalUnlock(IntPtr hMem);

    [DllImport("Kernel32.dll", SetLastError = true)]
    private static extern int GlobalSize(IntPtr hMem);

    #endregion

    public static string GetHTMLWin32Native()
    {

        string strHTMLUTF8 = string.Empty; 
        uint CF_HTML = RegisterClipboardFormatA("HTML Format");
        if (CF_HTML != null || CF_HTML == 0)
                return null;

        if (!IsClipboardFormatAvailable(CF_HTML))
            return null;

        try
        {
            if (!OpenClipboard(IntPtr.Zero))
                return null;

            IntPtr handle = GetClipboardData(CF_HTML);
            if (handle == IntPtr.Zero)
                return null;

            IntPtr pointer = IntPtr.Zero;

            try
            {
                pointer = GlobalLock(handle);
                if (pointer == IntPtr.Zero)
                    return null;

                uint size = GlobalSize(handle);
                byte[] buff = new byte[size];

                Marshal.Copy(pointer, buff, 0, (int)size);

            strHTMLUTF8 = System.Text.Encoding.UTF8.GetString(buff);
            }
            finally
            {
                if (pointer != IntPtr.Zero)
                    GlobalUnlock(handle);
            }
        }
        finally
        {
            CloseClipboard();
        }

        return strHTMLUTF8; 
    }
}
like image 177
Markus Avatar answered Oct 29 '25 03:10

Markus