Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NativeClipboard.cs
Go to the documentation of this file.
1using System;
3using System.Text;
4
5namespace ReLogic.OS.Windows;
6
7internal static class NativeClipboard
8{
9 private const uint CF_UNICODETEXT = 13u;
10
11 [DllImport("User32.dll", SetLastError = true)]
12 [return: MarshalAs(UnmanagedType.Bool)]
13 private static extern bool IsClipboardFormatAvailable(uint format);
14
15 [DllImport("user32.dll", SetLastError = true)]
16 private static extern bool OpenClipboard(IntPtr hWndNewOwner);
17
18 [DllImport("user32.dll", SetLastError = true)]
19 private static extern bool CloseClipboard();
20
21 [DllImport("user32.dll")]
22 private static extern bool SetClipboardData(uint uFormat, IntPtr data);
23
24 [DllImport("user32.dll", SetLastError = true)]
25 private static extern IntPtr GetClipboardData(uint uFormat);
26
27 [DllImport("Kernel32.dll", SetLastError = true)]
28 private static extern IntPtr GlobalLock(IntPtr hMem);
29
30 [DllImport("Kernel32.dll", SetLastError = true)]
31 [return: MarshalAs(UnmanagedType.Bool)]
32 private static extern bool GlobalUnlock(IntPtr hMem);
33
34 [DllImport("Kernel32.dll", SetLastError = true)]
35 private static extern int GlobalSize(IntPtr hMem);
36
37 public static void SetText(string text)
38 {
41 SetClipboardData(13u, ptr);
43 }
44
45 public static bool TryGetText(out string text)
46 {
47 text = null;
49 {
50 return false;
51 }
52 try
53 {
55 {
56 return false;
57 }
59 if (handle == IntPtr.Zero)
60 {
61 return false;
62 }
64 try
65 {
67 if (pointer == IntPtr.Zero)
68 {
69 return false;
70 }
71 int size = GlobalSize(handle);
72 byte[] buff = new byte[size];
73 Marshal.Copy(pointer, buff, 0, size);
74 text = Encoding.Unicode.GetString(buff).TrimEnd('\0');
75 return !string.IsNullOrEmpty(text);
76 }
77 finally
78 {
79 if (pointer != IntPtr.Zero)
80 {
82 }
83 }
84 }
85 finally
86 {
88 }
89 }
90}
static bool SetClipboardData(uint uFormat, IntPtr data)
static bool OpenClipboard(IntPtr hWndNewOwner)
static bool TryGetText(out string text)
static bool IsClipboardFormatAvailable(uint format)
static IntPtr GetClipboardData(uint uFormat)
static IntPtr GlobalLock(IntPtr hMem)
static bool GlobalUnlock(IntPtr hMem)
static int GlobalSize(IntPtr hMem)
static void SetText(string text)
static void Copy(int[] source, int startIndex, IntPtr destination, int length)
Definition Marshal.cs:800
static unsafe IntPtr StringToHGlobalUni(string? s)
Definition Marshal.cs:1310
static Encoding Unicode
Definition Encoding.cs:519
static readonly IntPtr Zero
Definition IntPtr.cs:18