I have a .NET 4.0 console application that generates SQL and stores it in a string variable. I want this string to be copied directly to the clipboard.
So far, all m
The Marshal.StringToHGlobalUni
function actually allocates memory in a fashion unsuitable for SetClipboardData (using LocalAlloc with LMEM_FIXED)
, which can cause crashes (you wouldn't expect it given the method name, but stepping into the code e.g. using ReSharper reveals this).
SetClipboardData
requires GlobalAlloc
with GMEM_MOVABLE
according to the documentation: SetClipboardData on MSDN.
Here's an MIT licensed System.Windows.Forms alternative, tested and complete with error handling: Clippy
(The clipboard pushing code itself is to be found here: Clippy.cs.)