Search code examples
c++winapi

The call to SetClipboardData after EmptyClipboard


Is call to SetClipboardData needed after the call to EmptyClipboard when the clipboard is opened using a nullptr argument? When clearing the clipboard content, can the call to SetClipboardData be omitted?

if (!OpenClipboard(nullptr)) {
    return false;
}
EmptyClipboard();
SetClipboardData(CF_TEXT, nullptr); // is this needed as-is
CloseClipboard();

The code is used to clear the clipboard content. No clipboard owner is needed. Is it safe to omit the SetClipboardData(CF_TEXT, nullptr); call?


Solution

  • If you just want to clear the clipboard, it doesn't make sense to call SetClipboardData() to put a data item into the clipboard. Specifying a null handle for the item sets it up for delayed rendering.

    So yes, omit the call to SetClipboardData() in this example.