-1

I am trying to save the clipboard current value and setting it back later on.

I am using the GetClipboardData() in order to get whatever is on the clipboard. However, I need to know which data type there is currently in the clipboard in order to call this function.

  • Is there a general data type I can use?
  • Once I get the data from the clipboard, how would I set it using the SetClipboardData() function?
  • 2
    You are assuming the clipboard contains data in only one format. It can contain data in all formats at the same time. Which you can [enumerate](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumclipboardformats). – GSerg Oct 29 '21 at 16:24
  • Ok, but how do I delete the last value added to the clipboard and return it to the state it was before I changed it? – Omer Friedlander Oct 29 '21 at 17:34
  • 3
    In general, you can't. You would have to make a complete copy of everything on the clipboard, then wipe the entire clipboard, and then re-add everything again. And that is not easy to do, with all the various possible formats, including custom formats, delay rendering, OLE objects, etc. What you are asking for is actually a very complicated task that the clipboard is not really designed for. What are you *really* trying to accomplish? – Remy Lebeau Oct 29 '21 at 18:32
  • My program is using the clipboard in my computer and I don't want it to replace what I currently have in my clipboard. Would it be too complicated to just save the top value in the clipboard and restore it once my program isdone with the clipboard so my last Ctrl+c will be saved? – Omer Friedlander Oct 29 '21 at 21:44
  • https://stackoverflow.com/questions/2578900/how-do-i-backup-and-restore-the-system-clipboard-in-c/2579846#2579846 – Vlad Feinstein Oct 29 '21 at 22:37
  • This is in c#, I am trying to use WINAPI in cpp – Omer Friedlander Oct 29 '21 at 22:58
  • 2
    That's not relevant. The answer is *"you can't"*, and that's true irrespective of the programming language used. – IInspectable Oct 30 '21 at 06:13
  • 1
    @OmerFriedlander "*My program is using the clipboard*" - for what purpose exactly? "*I don't want it to replace what I currently have in my clipboard*" - then using the clipboard is not the right solution for your problem, whatever that is (which you haven't explained yet)." *Would it be too complicated to just save the top value in the clipboard and restore it once my program isdone with the clipboard*" - yes, actually, it can be quite complicated, and in some cases, even impossible, to do that. – Remy Lebeau Oct 30 '21 at 19:59

1 Answers1

0

Clipboard is a shared resource. Besides it being almost impossible to restore it to its previous state, it may interfere with the other apps.

Much better alternative is to use WM_COPYDATA to copy data within your own program.

Windows 10 supports clipboard history, but I don't know of a method to remove last entry from it.

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
  • Wouldn't it be possible to use `GetClipboardData()` to get the latest value in the clipboard, then use `EmptyClipboard()` to empty it and lastly use `SetClipboardData()` to set the value we saved earlier by using the Get function? – Omer Friedlander Oct 30 '21 at 01:04
  • @OmerFriedlander No. As other comments pointed out, the owner of the clipboard can offer multiple formats, with delay rendering. You would have to get `ALL` of them, and the corresponding data. This may not be possible if the data changes at the time of `paste`, you would save/restore different data set. – Vlad Feinstein Oct 30 '21 at 03:43
  • Ok, got it. thank you all for helping me out! – Omer Friedlander Oct 30 '21 at 08:54