Created
November 10, 2025 18:39
-
-
Save kevingosse/f7be4bf971dc8632bb1b2400e1bf02b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Diagnostics; | |
| using System.Runtime.InteropServices; | |
| using System.Text; | |
| namespace InspectClipboard; | |
| internal class Program | |
| { | |
| [STAThread] | |
| static void Main(string[] args) | |
| { | |
| if (!OpenClipboard(IntPtr.Zero)) | |
| { | |
| Console.WriteLine("Failed to open clipboard"); | |
| return; | |
| } | |
| try | |
| { | |
| uint fmt = 0; | |
| while ((fmt = EnumClipboardFormats(fmt)) != 0) | |
| { | |
| string name = GetFormatName(fmt); | |
| IntPtr h = GetClipboardData(fmt); | |
| if (h == IntPtr.Zero) | |
| { | |
| Console.WriteLine($"Format: {name} ({fmt}), GetClipboardData returned NULL"); | |
| continue; | |
| } | |
| var sz = GlobalSize(h); | |
| var size = (int)sz; | |
| IntPtr p = GlobalLock(h); | |
| if (p == IntPtr.Zero) | |
| { | |
| Console.WriteLine($"Format: {name} ({fmt}), GlobalLock returned NULL"); | |
| continue; | |
| } | |
| try | |
| { | |
| byte[] data = new byte[size]; | |
| Marshal.Copy(p, data, 0, size); | |
| Console.WriteLine($"Format: {name} ({fmt}), Size: {size}"); | |
| if (data[^1] != 0) | |
| { | |
| Console.WriteLine($"**** Data does not end with null terminator ****"); | |
| } | |
| } | |
| finally | |
| { | |
| GlobalUnlock(h); | |
| } | |
| } | |
| var errror = Marshal.GetLastWin32Error(); | |
| if (errror != 0) | |
| { | |
| Debug.WriteLine($"EnumClipboardFormats error: {errror}"); | |
| } | |
| } | |
| finally | |
| { | |
| CloseClipboard(); | |
| } | |
| } | |
| private static string GetFormatName(uint fmt) | |
| { | |
| StringBuilder sb = new(128); | |
| if (fmt >= 0xC000 && GetClipboardFormatName(fmt, sb, sb.Capacity) > 0) | |
| { | |
| return sb.ToString(); | |
| } | |
| return fmt.ToString(); | |
| } | |
| [DllImport("user32.dll", SetLastError = true)] | |
| static extern bool OpenClipboard(IntPtr hWndNewOwner); | |
| [DllImport("user32.dll", SetLastError = true)] | |
| static extern bool CloseClipboard(); | |
| [DllImport("user32.dll", SetLastError = true)] | |
| static extern uint EnumClipboardFormats(uint format); | |
| [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] | |
| static extern uint GetClipboardFormatName(uint format, StringBuilder lpszFormatName, int cchMaxCount); | |
| [DllImport("user32.dll", SetLastError = true)] | |
| static extern IntPtr GetClipboardData(uint uFormat); | |
| [DllImport("kernel32.dll", SetLastError = true)] | |
| static extern IntPtr GlobalLock(IntPtr hMem); | |
| [DllImport("kernel32.dll", SetLastError = true)] | |
| static extern bool GlobalUnlock(IntPtr hMem); | |
| [DllImport("kernel32.dll", SetLastError = true)] | |
| static extern uint GlobalSize(IntPtr hMem); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment