{"id":22183,"date":"2008-05-27T10:00:00","date_gmt":"2008-05-27T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/05\/27\/you-cant-give-away-something-that-isnt-yours\/"},"modified":"2008-05-27T10:00:00","modified_gmt":"2008-05-27T10:00:00","slug":"you-cant-give-away-something-that-isnt-yours","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20080527-00\/?p=22183","title":{"rendered":"You can&#039;t give away something that isn&#039;t yours"},"content":{"rendered":"<p>\nThis rule of real life applies to code as well.\nIf something doesn&#8217;t belong to you, then you can&#8217;t give it away.\n<\/p>\n<p>\nFor example, functions like\n<code>SetCliboardData<\/code> and\n<code>SetWindowRgn<\/code>\ntake ownership of the item that you pass it.\nIn <code>SetClipboardData<\/cODE>'s case, the memory block you pass\nas the <code>hMem<\/code> parameter becomes the property of the clipboard.\nFor <code>SetWindowRgn<\/code> it's the <code>hRgn<\/code> that becomes\nthe property of the window manager.\nIn both cases, you are giving control of the item to another component,\nbut in order to do that legitimately, it must be yours to give away.\n<\/p>\n<p>\nThis statement may be obvious, but it's surprising how many people fail\nto grasp its consequences. For example, you can't do this:\n<\/p>\n<pre>\n\/\/ error checkin removed to improve readability\nSetClipboardData(CF_TEXT, hMem);\n<i>\/\/ Code in italics is wrong\nSetClipboardData(RegisterClipboardData(CFSTR_SHELLURL), hMem);\n<\/i><\/pre>\n<p>\nOnce you call <code>SetClipboardData<\/code> the first time,\nthe memory now belongs to the clipboard;\nit's not yours again.\nWhen you call <code>SetClipboardData<\/code> a second time,\nyou're giving it away again,\nbut the second time, it's not your memory any more.\n<\/p>\n<p>\nIn other words:\nAfter the first call to <code>SetClipboardData<\/code>,\nthe memory belongs to the clipboard.\nThe second call is trying to give away something that you no longer own.\n<\/p>\n<p>\nIt reminds me of a news story I read some time ago.\nIn the United States,\nthe winner of a major lottery often gets a choice between a\nseries of payments over several years\nor a lump sum.\nSomebody won the lottery and agreed to sell the annuity to a company\nin exchange for a lump sum.\n(Presumably, this company offered a lump sum larger than the one\noffered by the lottery.)\nAnd then when the lottery asked whether to disburse the winnings\nin the form of an annuity or a lump sum,\n<i>the winner opted for the lump sum<\/i>.\nDude, you can't take the lump sum.\nYou already sold the revenue stream to that other company.\nThat's not your money any more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This rule of real life applies to code as well. If something doesn&#8217;t belong to you, then you can&#8217;t give it away. For example, functions like SetCliboardData and SetWindowRgn take ownership of the item that you pass it. In SetClipboardData&#8217;s case, the memory block you pass as the hMem parameter becomes the property of 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":[25],"class_list":["post-22183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>This rule of real life applies to code as well. If something doesn&#8217;t belong to you, then you can&#8217;t give it away. For example, functions like SetCliboardData and SetWindowRgn take ownership of the item that you pass it. In SetClipboardData&#8217;s case, the memory block you pass as the hMem parameter becomes the property of the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/22183","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=22183"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/22183\/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=22183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=22183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=22183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}