{"id":103368,"date":"2020-01-27T07:00:00","date_gmt":"2020-01-27T15:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103368"},"modified":"2020-01-26T21:23:34","modified_gmt":"2020-01-27T05:23:34","slug":"20200127-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200127-00\/?p=103368","title":{"rendered":"Is there a difference between creating a null pen with <CODE>Create&shy;Pen<\/CODE> and just using the stock null pen?"},"content":{"rendered":"<p>A null pen draws nothing. It&#8217;s a pen with no ink.<\/p>\n<p>To get a null pen, you can use the stock null pen by calling <code>Get\u00adStock\u00adObject(NULL_<\/code><code>PEN)<\/code>, or you can create your own null pen from scratch by calling <code>Create\u00adPen(PS_NULL, something, something)<\/code>.<\/p>\n<p>Is there a difference? Why would you choose one over the other?<\/p>\n<p>All null pens are functionally equivalent. They all do nothing. The difference is in the rules for handling them.<\/p>\n<p>The stock null pen is always available, and you don&#8217;t need to destroy it. (Indeed, you <i>can&#8217;t<\/i> destroy it.) On the other hand, if you create your own custom null pen, then it&#8217;s on you to destroy it.<\/p>\n<p>Now I get to talk about implementation details: Remember that implementation details are not contractual and may change at any time.<\/p>\n<p>Starting in the Windows NT series, the <code>Create\u00adPen<\/code> function (and its moral equivalents such as <code>Create\u00adPen\u00adIndirect<\/code>) checks whether you&#8217;re creating a null pen. If so, then it just gives you the stock null pen instead of creating a custom one.<\/p>\n<p>The contract is still the same: Pens created with <code>Create\u00adPen<\/code> still need to be destroyed (eventually) with <code>Delete\u00adObject<\/code>. (But everything works out because your attempt to destroy the stock null pen is simply ignored.)<\/p>\n<p>Aside from checking the numeric value of the handle and comparing it to the numeric value of the stock null pen, there&#8217;s another way to detect that your null pen has been optimized out: See if your custom width and color are still there!<\/p>\n<pre>HPEN hpen = CreatePen(PS_NULL, 42, RGB(12,34,56));\r\nLOGPEN lpen;\r\nGetObject(hpen, sizeof(lpen), &amp;lpen);\r\nif (lpen.lopnWidth == 42)\r\n{\r\n  \/\/ CreatePen gave me a custom pen!\r\n}\r\n<\/pre>\n<p>Fortunately, we didn&#8217;t find any apps that secretly passed information by hiding it inside the the width and color of null pens.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>All null pens are functionally equivalent, so why have more than one? Are some pens more null than others?<\/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-103368","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>All null pens are functionally equivalent, so why have more than one? Are some pens more null than others?<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103368","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=103368"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103368\/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=103368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}