{"id":14093,"date":"2010-05-10T07:00:00","date_gmt":"2010-05-10T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/05\/10\/why-can-programs-empty-the-clipboard-when-they-start-up\/"},"modified":"2010-05-10T07:00:00","modified_gmt":"2010-05-10T07:00:00","slug":"why-can-programs-empty-the-clipboard-when-they-start-up","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20100510-00\/?p=14093","title":{"rendered":"Why can programs empty the clipboard when they start up?"},"content":{"rendered":"<p>\nVia the Suggestion Box,\nJohan Alm&#233;n\nasks,\n&#8220;<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/pages\/407234.aspx#1434973\">What was the rationale behind the decision to\nlet Excel empty the clipboard when launched?<\/a>&#8221;\n<\/p>\n<p>\nWhy can an application empty the clipboard?\nBecause it&#8217;s there.\n<\/p>\n<p>\nAfter all, the point of the clipboard is to hold information\ntemporarily.\nPrograms are permitted to empty the clipboard, add data to the clipboard,\nor retrieve data from the clipboard.\nThat&#8217;s why it&#8217;s there.\n<\/p>\n<p>\n(I&#8217;m assuming that the naming of the program Excel was just\nan example of a program, and that the question wasn&#8217;t\n&#8220;Why doesn&#8217;t Windows have a specific check for the program\n<code>EXCEL.EXE<\/code> and block its clipboard access while\nstill allowing clipboard access to everybody else.&#8221;)\n<\/p>\n<p>\nOkay, maybe the question wasn&#8217;t so much\n&#8220;Why are programs allowed to empty the clipboard&#8221;\nas it was\n&#8220;Why are programs allowed to empty the clipboard when they launch?&#8221;\nWell, because that might have been the whole point of the program!\nSomebody might write a program called <code>empty&shy;clip<\/code> whose\nsole purpose in life is to empty the clipboard.\nYou run the program, it empties the clipboard,\nand then it exits.\nShort and sweet.\nIf Windows didn&#8217;t allow programs to empty the clipboard when\nthey started up, then this program would not be able to get its\nwork done.\n<\/p>\n<p>\nYou might not consider that particularly useful,\nbut there are actually quite a few programs which empty the\nclipboard when they start up.\nFor example, the <code>clip<\/code> program that comes\nwith Windows takes its standard input and places it on the clipboard.\nImplied in that functional description is that it erases what used\nto be on the clipboard.\nEverything the program does is in its startup.\n<\/p>\n<pre>\necho I'm on the clipboard! And I erased what use to be there.| clip\n<\/pre>\n<p>\nMany scripting languages provide access to the clipboard,\nif not natively, then through an extension.\nSince these are typically\nnot GUI programs, as far as the window manager can tell,\nthese programs as perpetually stuck in their startup code:\nThey never go input idle because they never pump messages.\nProhibiting programs from accessing the clipboard during\nstartup means that console programs are effectively banned\nfrom modifying the clipboard at all.\n<\/p>\n<p>\nOkay, maybe the question wasn&#8217;t\n&#8220;Why are programs allowed to empty the clipboard when they launch?&#8221;\nso much as it was\n&#8220;Why are programs allowed to empty the clipboard outside\nof an explicit user action (like a click or a hotkey)?&#8221;\nWell, we still have the problem of programs whose design is to\nempty the clipboard without any user interaction,\nlike all those console scripts.\nBut you also remove many GUI usage patterns, such as pushing\nwork to a background thread\nso that the program can remain responsive.\nAnd it would also prevent you from writing a program that\nmodified the clipboard in response to a drop operation.\nI can imagine a program called <code>file&shy;contents&shy;to&shy;clip<\/code>\nwhich just sits there and waits for you to drag\/drop a file\nonto its window.\nWhen you do that, it opens the file and places the file&#8217;s\ncontents onto the clipboard.\nSince the drag\/drop operation is handled by the drag source,\nthe drop target receives no input and (according to the\nrule of &#8220;no clipboard access without user input&#8221;) is denied\npermission to erase the old clipboard contents.\n<\/p>\n<p>\nIn order for these sorts of interaction models to work,\nthere would have to be some sort of <code>Allow&shy;Clipboard&shy;Access<\/code>\nfunction (akin to <code>Allow&shy;Set&shy;Foreground&shy;Window<\/code>)\nso that one process can temporarily\ntransfer clipboard access permission\nto another process.\nIt could be done, but it would make writing applications more\ncomplicated,\nbecause you would have to anticipate what operations\nmight result in another application wanting to access the\nclipboard and scattering calls to <code>Allow&shy;Clipboard&shy;Access<\/code>\nin various places in your program.\nIf you miss a spot, you&#8217;ll get some bug filed against your\nprogram that says,\n&#8220;When I click the <i>Preview<\/i> button,\nand I&#8217;ve set my custom previewer to program&nbsp;X\nand configure program&nbsp;X to say &#8216;always copy image\nto clipboard when previewing&#8217;, the feature doesn&#8217;t work.&#8221;\n<\/p>\n<p>\nThe clipboard was part of Windows&nbsp;1.0,\nand back in those days, you didn&#8217;t have a lot of\nmemory available.\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/08\/09\/693280.aspx\">\nYou had to get a lot done with very little<\/a>.\nProgrammers were trusted to use their great power\nwith great responsibility.\nAnd besides, as we saw with programs like\n<code>clip<\/code> (and hypothetical programs like\n<code>empty&shy;clip<\/code> and\n<code>file&shy;contents&shy;to&shy;clip<\/code>),\nallowing programs to empty the clipboard at startup\nmade it possible to write some interesting and\nuseful tools.\nWindows historically didn&#8217;t stop programmers from doing stupid things\nbecause that would also prevent them from doing\nclever things.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Via the Suggestion Box, Johan Alm&#233;n asks, &#8220;What was the rationale behind the decision to let Excel empty the clipboard when launched?&#8221; Why can an application empty the clipboard? Because it&#8217;s there. After all, the point of the clipboard is to hold information temporarily. Programs are permitted to empty the clipboard, add data to the [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[26],"class_list":["post-14093","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-other"],"acf":[],"blog_post_summary":"<p>Via the Suggestion Box, Johan Alm&#233;n asks, &#8220;What was the rationale behind the decision to let Excel empty the clipboard when launched?&#8221; Why can an application empty the clipboard? Because it&#8217;s there. After all, the point of the clipboard is to hold information temporarily. Programs are permitted to empty the clipboard, add data to the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/14093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=14093"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/14093\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=14093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=14093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=14093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}