Skip to content

Commit

Permalink
Hopefully fixed #355
Browse files Browse the repository at this point in the history
  • Loading branch information
dahall committed Dec 30, 2022
1 parent 04cb2ed commit 7ca924e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions UnitTests/Windows.Shell/ClipboardTests.cs
Expand Up @@ -28,6 +28,8 @@ public void CtorTest()
Assert.That(cb.GetText(TextDataFormat.UnicodeText), Is.EqualTo(txt));
using (var cb = new Clipboard(true, User32.GetActiveWindow()))
cb.SetText(txt, txt, txt);
using (var cb = new Clipboard(false, User32.GetActiveWindow()))
Assert.That(cb.GetText(TextDataFormat.UnicodeText), Is.EqualTo(txt));
using (var cb = new Clipboard())
Assert.That(cb.GetText(TextDataFormat.UnicodeText), Is.EqualTo(txt));
}
Expand Down
20 changes: 18 additions & 2 deletions Windows.Shell.Common/NativeClipboard.cs
Expand Up @@ -71,8 +71,7 @@ public NativeClipboard(bool empty = false, HWND hWndNewOwner = default)
}
if (hWndNewOwner == default)
hWndNewOwner = GetDesktopWindow();
if (!OpenClipboard(hWndNewOwner))
Win32Error.ThrowLastError();
Win32Error.ThrowLastErrorIfFalse(OpenClipboard(hWndNewOwner));
open = true;
if (empty)
Empty();
Expand Down Expand Up @@ -127,6 +126,19 @@ public static IComDataObject DataObject
set => OleSetClipboard(value).ThrowIfFailed();
}

/// <summary>Retrieves the clipboard sequence number for the current window station.</summary>
/// <returns>
/// The clipboard sequence number. If you do not have <c>WINSTA_ACCESSCLIPBOARD</c> access to the window station, the function
/// returns zero.
/// </returns>
/// <remarks>
/// The system keeps a serial number for the clipboard for each window station. This number is incremented whenever the contents of
/// the clipboard change or the clipboard is emptied. You can track this value to determine whether the clipboard contents have
/// changed and optimize creating DataObjects. If clipboard rendering is delayed, the sequence number is not incremented until the
/// changes are rendered.
/// </remarks>
public static uint SequenceNumber => GetClipboardSequenceNumber();

/// <summary>Carries out the clipboard shutdown sequence. It also releases any IDataObject instances that were placed on the clipboard.</summary>
public static void Flush() => OleFlushClipboard().ThrowIfFailed();

Expand Down Expand Up @@ -391,6 +403,7 @@ public void SetBinaryData(uint formatId, byte[] data)
{
using var pMem = new SafeMoveableHGlobalHandle(data);
Win32Error.ThrowLastErrorIfInvalid(pMem);
pMem.Unlock();
Win32Error.ThrowLastErrorIfNull(SetClipboardData(formatId, pMem.DangerousGetHandle()));
}

Expand All @@ -401,6 +414,7 @@ public void SetData<T>(uint formatId, T data)
{
using var pMem = SafeMoveableHGlobalHandle.CreateFromStructure(data);
Win32Error.ThrowLastErrorIfInvalid(pMem);
pMem.Unlock();
Win32Error.ThrowLastErrorIfNull(SetClipboardData(formatId, pMem.DangerousGetHandle()));
}

Expand All @@ -411,6 +425,7 @@ public void SetData<T>(uint formatId, T data)
{
using var pMem = SafeMoveableHGlobalHandle.CreateFromList(values);
Win32Error.ThrowLastErrorIfInvalid(pMem);
pMem.Unlock();
Win32Error.ThrowLastErrorIfNull(SetClipboardData(formatId, pMem.DangerousGetHandle()));
}

Expand All @@ -423,6 +438,7 @@ public void SetData(uint formatId, IEnumerable<string> values, StringListPackMet
{
using var pMem = SafeMoveableHGlobalHandle.CreateFromStringList(values, packing, charSet);
Win32Error.ThrowLastErrorIfInvalid(pMem);
pMem.Unlock();
Win32Error.ThrowLastErrorIfNull(SetClipboardData(formatId, pMem.DangerousGetHandle()));
}

Expand Down

0 comments on commit 7ca924e

Please sign in to comment.