{"id":37403,"date":"2004-11-03T06:57:00","date_gmt":"2004-11-03T06:57:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/11\/03\/why-do-i-sometimes-see-redundant-casts-before-casting-to-lparam\/"},"modified":"2004-11-03T06:57:00","modified_gmt":"2004-11-03T06:57:00","slug":"why-do-i-sometimes-see-redundant-casts-before-casting-to-lparam","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20041103-00\/?p=37403","title":{"rendered":"Why do I sometimes see redundant casts before casting to LPARAM?"},"content":{"rendered":"<p><P>\nIf you read through old code, you will often find casts\nthat seem redundant.\n<\/P>\n<PRE>\nSendMessage(hwndListBox, LB_ADDSTRING, 0, (LPARAM)(LPSTR)&#8221;string&#8221;);\n<\/PRE>\n<P>\nWhy was <CODE>&#8220;string&#8221;<\/CODE> cast to <CODE>LPSTR<\/CODE>?\nIt&#8217;s already an <CODE>LPSTR<\/CODE>!\n<\/P>\n<P>\nThese are leftovers from 16-bit Windows.\nRecall that in 16-bit Windows, pointers were near by default.\nConsequently, <CODE>&#8220;string&#8221;<\/CODE> was a near pointer to a string.\nIf the code had been written as\n<PRE>\nSendMessage(hwndListBox, LB_ADDSTRING, 0, (LPARAM)&#8221;string&#8221;);\n<\/PRE>\n<P>\nthen it would have taken the near pointer and cast it to a <CODE>long<\/CODE>.\nSince a near pointer is a 16-bit value, the pointer would have been \nzero-extended to the 32-bit size of a <CODE>long<\/CODE>.\n<\/P>\n<P>\nHowever, all pointers in window messages must be far pointers\nbecause the window procedure for the window might very well be implemented\nin a different module from the sender.\nRecall that near pointers are interpreted relative to the default\nselector, and the default selector for each module is different.\nSending a near pointer to another module will result in the\npointer being interpreted relative to the <STRONG>recipient&#8217;s<\/STRONG>\ndefault selector, which is not the same as the <STRONG>sender&#8217;s<\/STRONG>\ndefault selector.\n<\/P>\n<P>\nThe intermediate cast to <CODE>LPSTR<\/CODE> converts the near\npointer to a far pointer, <CODE>LP<\/CODE> being the Hungarian\nprefix for far pointers (also known as &#8220;long pointers&#8221;).\nCasting a near pointer to a far pointer inserts the previously-implied\ndefault selector, so that the cast to <CODE>LPARAM<\/CODE> captures\nthe full 16:16 far pointer.\n<\/P>\n<P>\nAren&#8217;t you glad you don&#8217;t have to worry about this any more?\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you read through old code, you will often find casts that seem redundant. SendMessage(hwndListBox, LB_ADDSTRING, 0, (LPARAM)(LPSTR)&#8221;string&#8221;); Why was &#8220;string&#8221; cast to LPSTR? It&#8217;s already an LPSTR! These are leftovers from 16-bit Windows. Recall that in 16-bit Windows, pointers were near by default. Consequently, &#8220;string&#8221; was a near pointer to a string. If the [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-37403","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>If you read through old code, you will often find casts that seem redundant. SendMessage(hwndListBox, LB_ADDSTRING, 0, (LPARAM)(LPSTR)&#8221;string&#8221;); Why was &#8220;string&#8221; cast to LPSTR? It&#8217;s already an LPSTR! These are leftovers from 16-bit Windows. Recall that in 16-bit Windows, pointers were near by default. Consequently, &#8220;string&#8221; was a near pointer to a string. If the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/37403","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=37403"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/37403\/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=37403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=37403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=37403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}