Beispiel #1
0
        public override unsafe void SetClipboardText(string value)
        {
            UIntPtr dstSize = (UIntPtr)((value.Length + 1) * Marshal.SystemDefaultCharSize);

            // retry up to 10 times
            for (int i = 0; i < 10; i++)
            {
                if (!API.OpenClipboard(WinHandle))
                {
                    Thread.Sleep(100);
                    continue;
                }

                IntPtr hGlobal = API.GlobalAlloc(GMEM_MOVEABLE, dstSize);
                if (hGlobal == IntPtr.Zero)
                {
                    API.CloseClipboard(); return;
                }

                IntPtr dst = API.GlobalLock(hGlobal);
                fixed(char *src = value)
                {
                    CopyString_Unicode((IntPtr)src, dst, value.Length);
                }

                API.GlobalUnlock(hGlobal);

                API.EmptyClipboard();
                API.SetClipboardData(CF_UNICODETEXT, hGlobal);
                API.CloseClipboard();
                return;
            }
        }
Beispiel #2
0
        public override unsafe string GetClipboardText()
        {
            // retry up to 10 times
            for (int i = 0; i < 10; i++)
            {
                if (!API.OpenClipboard(WinHandle))
                {
                    Thread.Sleep(100);
                    continue;
                }

                bool   isUnicode = true;
                IntPtr hGlobal   = API.GetClipboardData(CF_UNICODETEXT);
                if (hGlobal == IntPtr.Zero)
                {
                    hGlobal   = API.GetClipboardData(CF_TEXT);
                    isUnicode = false;
                }
                if (hGlobal == IntPtr.Zero)
                {
                    API.CloseClipboard(); return("");
                }

                IntPtr src   = API.GlobalLock(hGlobal);
                string value = isUnicode ? new String((char *)src) : new String((sbyte *)src);
                API.GlobalUnlock(hGlobal);

                API.CloseClipboard();
                return(value);
            }
            return("");
        }