Advertisement
apieceoffruit

Clipboard

Jul 15th, 2022
1,282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1.   static class WindowsClipboard
  2.     {
  3.         public static void SetText(string text)
  4.         {
  5.             OpenClipboard();
  6.  
  7.             EmptyClipboard();
  8.             IntPtr hGlobal = default;
  9.             try
  10.             {
  11.                 var bytes = (text.Length + 1) * 2;
  12.                 hGlobal = Marshal.AllocHGlobal(bytes);
  13.  
  14.                 if (hGlobal == default)
  15.                 {
  16.                     ThrowWin32();
  17.                 }
  18.  
  19.                 var target = GlobalLock(hGlobal);
  20.  
  21.                 if (target == default)
  22.                 {
  23.                     ThrowWin32();
  24.                 }
  25.  
  26.                 try
  27.                 {
  28.                     Marshal.Copy(text.ToCharArray(), 0, target, text.Length);
  29.                 }
  30.                 finally
  31.                 {
  32.                     GlobalUnlock(target);
  33.                 }
  34.  
  35.                 if (SetClipboardData(cfUnicodeText, hGlobal) == default)
  36.                 {
  37.                     ThrowWin32();
  38.                 }
  39.  
  40.                 hGlobal = default;
  41.             }
  42.             finally
  43.             {
  44.                 if (hGlobal != default)
  45.                 {
  46.                     Marshal.FreeHGlobal(hGlobal);
  47.                 }
  48.  
  49.                 CloseClipboard();
  50.             }
  51.         }
  52.  
  53.         public static void OpenClipboard()
  54.         {
  55.             var num = 10;
  56.             while (true)
  57.             {
  58.                 if (OpenClipboard(default))
  59.                 {
  60.                     break;
  61.                 }
  62.  
  63.                 if (--num == 0)
  64.                 {
  65.                     ThrowWin32();
  66.                 }
  67.  
  68.                 Thread.Sleep(100);
  69.             }
  70.         }
  71.  
  72.         const uint cfUnicodeText = 13;
  73.  
  74.         static void ThrowWin32()
  75.         {
  76.             throw new Win32Exception(Marshal.GetLastWin32Error());
  77.         }
  78.  
  79.         [DllImport("kernel32.dll", SetLastError = true)]
  80.         static extern IntPtr GlobalLock(IntPtr hMem);
  81.  
  82.         [DllImport("kernel32.dll", SetLastError = true)]
  83.         [return: MarshalAs(UnmanagedType.Bool)]
  84.         static extern bool GlobalUnlock(IntPtr hMem);
  85.  
  86.         [DllImport("user32.dll", SetLastError = true)]
  87.         [return: MarshalAs(UnmanagedType.Bool)]
  88.         static extern bool OpenClipboard(IntPtr hWndNewOwner);
  89.  
  90.         [DllImport("user32.dll", SetLastError = true)]
  91.         [return: MarshalAs(UnmanagedType.Bool)]
  92.         static extern bool CloseClipboard();
  93.  
  94.         [DllImport("user32.dll", SetLastError = true)]
  95.         static extern IntPtr SetClipboardData(uint uFormat, IntPtr data);
  96.  
  97.         [DllImport("user32.dll")]
  98.         static extern bool EmptyClipboard();
  99.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement