-2

In my application I want to lock the clipboard to prevent other application from changing the clipboard. How can I achieve this using Delphi 2007?

Kromster
  • 7,181
  • 7
  • 63
  • 111
Abhishek
  • 174
  • 2
  • 9
  • How do you imagine the use-case for such a feature? For as long as your app runs clipboard is disabled? Add more details. This looks like XY problem. – Kromster Apr 05 '16 at 12:44
  • 4
    Translated: *I want my users to perform destruction testing of my uninstall program.* – David Heffernan Apr 05 '16 at 13:49
  • 3
    [What if two programs did this?](https://blogs.msdn.microsoft.com/oldnewthing/20050607-00/?p=35413) – Gerry Coll Apr 05 '16 at 21:45
  • Whilst this can be done, it shouldn't. This sounds to me like a question driven by the belief that it would provide the solution to some other problem. It may be helpful to explain what that original problem is. There is almost certainly a better solution. I speculate that your underlying problem might be something along the lines of using the clipboard to save and transfer some data between two operations separated by time or perhaps in two different programs and so needing to prevent *other* programs from "interfering" with the data transfer. – Deltics Apr 05 '16 at 22:52
  • May be I was not clear enough in asking the question. I have tried to add the actual problem in next question. http://stackoverflow.com/questions/36448709/how-to-send-text-to-adobe-flash-player-exe-from-delphi-application – Abhishek Apr 06 '16 at 10:44
  • I expect that your motivation for this is to enforce DRM or privacy, such as making it difficult to make unauthorized copies of your protected content. This is NOT going to be a good way to do that. You are talking about interfering with a shared resource, and introducing unexpected behavior. This can include crashing programs, locked remote desktop sessions, unexpected error messages, lost data (whatever was on the clipboard before you destroyed it), angry users, and angry third-parties. – Chris Thornton Apr 06 '16 at 13:37
  • @Chris: Looking at the poster's [follow up question](http://stackoverflow.com/questions/36448709/how-to-send-text-to-adobe-flash-player-exe-from-delphi-application), it may be the other way around; this poster may not be trying to enforce anything. – Ken White Apr 07 '16 at 01:59
  • @KenWhite Looking at follow up question I don't think OP has to even worry about clipboard at all as it is possible that the flash application that he is talking about simply doesn't handle pasting of the text at all. – SilverWarior Apr 07 '16 at 03:12
  • @SilverWarior the flash application is _always clearing the clipboard._ I have checked this behavior. My purpose is to find a way in which I can add my text in that application. Thanks. – Abhishek Apr 07 '16 at 08:50
  • @Abhishek In tha case such application should be considered as potentionally dangerous as it is interfeering with normal operations of every other application on your system. Therefore I strongly recomend you against using of such application in the first place. As for possible way to combat this check my answer in your follow up question. – SilverWarior Apr 07 '16 at 13:33

2 Answers2

4

There is no facility for that. The user is the ultimate owner of the clipboard. When the user wants something else on the clipboard, the user will cut or copy something new. You, as the application developer, don't get a vote. (Users who discover programs trying to assert votes they don't have are likely to uninstall those programs and give them poor reviews.)

You can monitor the clipboard to discover when it changes with wm_ClipboardUpdate, but by the time you receive the notification, there's already something new there.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • 4
    "*There is no facility for that*" - that is not entirely true. The app can call `OpenClipboard()` and leave it open: "*Opens the clipboard for examination **and prevents other applications from modifying the clipboard content**... OpenClipboard fails if another window has the clipboard open.*" I would not advise doing this to the clipboard, for the reasons already stated, but it is *technically* doable. – Remy Lebeau Apr 05 '16 at 15:47
  • Presumably the user wants to read the clipboard from another app. I believe that would not be possible if it is left open. – Michael Apr 06 '16 at 12:32
  • As @RemyLebeau stated, leaving the clipboard open will block other apps from using it. I want to point out that this can cause unexpected/undesired behavior in those apps, including crashing them. – Chris Thornton Apr 06 '16 at 13:34
3

The purpose of the clipboard is to make data stored in it to be available to any program at any time and therefore provide easy way from transferring such data between different applications.

Because of that there is no official mechanism which would allow blocking access to the clipboard.

Any way if you are perhaps thinking of trying to block other application of accessing the clipboard in order to avoid them to be able to intercept some data from your application that has been stored there for copying purposes (which is the only reason I can think of why you would want to do this) there is another even better way.

Instead of using Windows default clipboard for string parts of data for copy and paste operations go and implement your own custom clipboard that will only be available from your own application similar as Microsoft does with its Office Clipboard (https://support.office.com/en-us/article/Copy-and-paste-multiple-items-by-using-the-Office-Clipboard-714a72af-1ad4-450f-8708-c2931e73ec8a).

In order to do this you only need to design a storing mechanism and then override default Cut, Copy and Paste shortcuts to use your mechanism instead of the default Windows Clipboard.

SilverWarior
  • 7,372
  • 2
  • 16
  • 22