public static byte[] GetClipboardData(string format) { uint formatHandle = User32.RegisterClipboardFormat(format); if (formatHandle != 0) { User32.OpenClipboard(IntPtr.Zero); //Get pointer to clipboard data in the selected format var clipboardDataPointer = User32.GetClipboardData(formatHandle); //Do a bunch of crap necessary to copy the data from the memory //the above pointer points at to a place we can access it. var length = Kernel32.GlobalSize(clipboardDataPointer); var gLock = Kernel32.GlobalLock(clipboardDataPointer); //Init a buffer which will contain the clipboard data var buffer = new byte[(int)length]; //Copy clipboard data to buffer Marshal.Copy(gLock, buffer, 0, (int)length); User32.CloseClipboard(); return(buffer); } return(new byte[0]); }
public static void SetText(ReadOnlySpan <char> value) { var memoryLength = (value.Length + 1) * sizeof(char); // SetClipboardData requires GMEM_MOVEABLE. // https://docs.microsoft.com/en-us/windows/desktop/api/Winuser/nf-winuser-setclipboarddata#parameters using (var memory = Kernel32.GlobalAlloc(Kernel32.GMEM.MOVEABLE, (UIntPtr)memoryLength)) { unsafe { var pointer = Kernel32.GlobalLock(memory); if (pointer is null) { throw new Win32Exception(); } try { fixed(char *valuePointer = value) Buffer.MemoryCopy(valuePointer, pointer, memoryLength, memoryLength); *((char *)pointer + value.Length) = '\0'; } finally { if (!Kernel32.GlobalUnlock(memory)) { var error = Marshal.GetLastWin32Error(); if (error != 0) { throw new Win32Exception(error); } } } } if (!User32.OpenClipboard(hWndNewOwner: IntPtr.Zero)) { throw new Win32Exception(); } try { if (User32.SetClipboardData(User32.CF.UNICODETEXT, memory) == IntPtr.Zero) { throw new Win32Exception(); } // If SetClipboardData succeeds, we have transferred ownership of the memory. // https://docs.microsoft.com/en-us/windows/desktop/api/Winuser/nf-winuser-setclipboarddata#parameters memory.SetHandleAsInvalid(); } finally { if (!User32.CloseClipboard()) { throw new Win32Exception(); } } } }