<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Angry Richard's WebLog : Useless rants</title><link>http://blogs.msdn.com/angryrichard/archive/tags/Useless+rants/default.aspx</link><description>Tags: Useless rants</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>How to shoot yourself in the foot with const (or the lack thereof)</title><link>http://blogs.msdn.com/angryrichard/archive/2006/05/24/606225.aspx</link><pubDate>Wed, 24 May 2006 21:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:606225</guid><dc:creator>AngryRichard</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/angryrichard/comments/606225.aspx</comments><wfw:commentRss>http://blogs.msdn.com/angryrichard/commentrss.aspx?PostID=606225</wfw:commentRss><description>&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;It has long puzzled me why various Win32 functions take non-const string parameters, when clearly they have no business manipulating the string.&amp;nbsp; Take, for instance, GetNamedSecurityInfo:&lt;/FONT&gt;&lt;/P&gt;&lt;FONT face=Tahoma size=2&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;PRE class=syntax xml:space="preserve"&gt;&lt;B&gt;DWORD&lt;/B&gt; &lt;B&gt;GetNamedSecurityInfo(&lt;/B&gt;
  &lt;B&gt;LPTSTR&lt;/B&gt; &lt;A class=synParam onclick=showTip(this) href=""&gt;&lt;EM&gt;&lt;FONT color=#000000&gt;pObjectName&lt;/FONT&gt;&lt;/EM&gt;&lt;/A&gt;&lt;B&gt;&lt;/B&gt;&lt;B&gt;&lt;EM&gt;,&lt;/EM&gt;
&lt;/B&gt;  &lt;B&gt;SE_OBJECT_TYPE&lt;/B&gt; &lt;I&gt;&lt;A class=synParam onclick=showTip(this) href=""&gt;&lt;FONT color=#000000&gt;ObjectType&lt;/FONT&gt;&lt;/A&gt;&lt;/I&gt;&lt;B&gt;&lt;/B&gt;&lt;FONT color=#000000&gt;&lt;B&gt;,
&lt;/B&gt;  &lt;B&gt;SECURITY_INFORMATION&lt;/B&gt; &lt;I&gt;&lt;A class=synParam onclick=showTip(this) href=""&gt;&lt;FONT color=#000000&gt;SecurityInfo&lt;/FONT&gt;&lt;/A&gt;&lt;/I&gt;&lt;B&gt;&lt;/B&gt;&lt;/FONT&gt;&lt;FONT color=#000000&gt;&lt;B&gt;,
&lt;/B&gt;  &lt;B&gt;PSID*&lt;/B&gt; &lt;I&gt;&lt;A class=synParam onclick=showTip(this) href=""&gt;&lt;FONT color=#000000&gt;ppsidOwner&lt;/FONT&gt;&lt;/A&gt;&lt;/I&gt;&lt;B&gt;&lt;/B&gt;&lt;/FONT&gt;&lt;FONT color=#000000&gt;&lt;B&gt;,
&lt;/B&gt;  &lt;B&gt;PSID*&lt;/B&gt; &lt;I&gt;&lt;A class=synParam onclick=showTip(this) href=""&gt;&lt;FONT color=#000000&gt;ppsidGroup&lt;/FONT&gt;&lt;/A&gt;&lt;/I&gt;&lt;B&gt;&lt;/B&gt;&lt;/FONT&gt;&lt;FONT color=#000000&gt;&lt;B&gt;,
&lt;/B&gt;  &lt;B&gt;PACL*&lt;/B&gt; &lt;I&gt;&lt;A class=synParam onclick=showTip(this) href=""&gt;&lt;FONT color=#000000&gt;ppDacl&lt;/FONT&gt;&lt;/A&gt;&lt;/I&gt;&lt;B&gt;&lt;/B&gt;&lt;/FONT&gt;&lt;FONT color=#000000&gt;&lt;B&gt;,
&lt;/B&gt;  &lt;B&gt;PACL*&lt;/B&gt; &lt;I&gt;&lt;A class=synParam onclick=showTip(this) href=""&gt;&lt;FONT color=#000000&gt;ppSacl&lt;/FONT&gt;&lt;/A&gt;&lt;/I&gt;&lt;B&gt;&lt;/B&gt;&lt;/FONT&gt;&lt;FONT color=#000000&gt;&lt;B&gt;,
&lt;/B&gt;  &lt;B&gt;PSECURITY_DESCRIPTOR*&lt;/B&gt; &lt;I&gt;&lt;A class=synParam onclick=showTip(this) href=""&gt;&lt;FONT color=#000000&gt;ppSecurityDescriptor&lt;/FONT&gt;&lt;/A&gt;&lt;/I&gt;&lt;B&gt;&lt;/B&gt;&lt;/FONT&gt;&lt;B&gt;
&lt;/B&gt;&lt;B&gt;);&lt;/B&gt;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;I can't imagine why this function should ever need to write to pObjectName.&amp;nbsp; In fact, the SDK documentation explicitly lists it as an [in] parameter.&amp;nbsp; Also note that pObjectName is an LP&lt;STRONG&gt;T&lt;/STRONG&gt;STR.&amp;nbsp; This is important to the shooting of the foot.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;A tidy, const-aware&amp;nbsp;developer might want to do something like this:&lt;/FONT&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=1&gt;const LPWSTR SomeFile = L"\\somefile.txt";&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=1&gt;[...]&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=1&gt;GetNamedSecurityInfo(Somefile, ...)&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Of course, this will fail to compile, because SomeFile is const.&amp;nbsp; But casting solves this problem, right?&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;&lt;FONT face="Courier New" size=1&gt;GetNamedSecurityInfo((LPTSTR)Somefile, ...)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Yay, this compiles.&amp;nbsp; Why, it even appears to return the correct data.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Fast forward a year or two, when suddenly the code stops working on Vista.&amp;nbsp; What could be wrong?&amp;nbsp; Why, in the process of casting away const, we've also turned an LP&lt;STRONG&gt;W&lt;/STRONG&gt;STR into an LP&lt;STRONG&gt;T&lt;/STRONG&gt;STR.&amp;nbsp; In this case, TCHAR resolved to char, so the code above was actually returning a security descriptor for "\\".&amp;nbsp; Well, that is until Vista came along.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;So while I hang my head in shame for being lame enough to cast between incompatible types, I will also caution the rest of you to consider whether your string parameters really need to be non-const, and avoid making your clients play casting games to use your API.&amp;nbsp; And yes, using &lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/express_75.asp"&gt;const_cast&amp;lt;&amp;gt;&lt;/A&gt; might have prevented this issue, but I didn't know about it at the time.&amp;nbsp; You, having read this far, have no excuse.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=606225" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/angryrichard/archive/tags/Useless+rants/default.aspx">Useless rants</category></item><item><title>C'mon, all your Friends are Doing it</title><link>http://blogs.msdn.com/angryrichard/archive/2004/05/27/143376.aspx</link><pubDate>Thu, 27 May 2004 22:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:143376</guid><dc:creator>AngryRichard</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/angryrichard/comments/143376.aspx</comments><wfw:commentRss>http://blogs.msdn.com/angryrichard/commentrss.aspx?PostID=143376</wfw:commentRss><description>&lt;P&gt;A bunch of the guys on the &lt;A href="http://blogs.msdn.com/profiler"&gt;team&lt;/A&gt; I work for have been starting up blogs.&amp;nbsp; I started feeling left out, &lt;BR&gt;which made me very angry.&lt;/P&gt;
&lt;P&gt;It appears all blogs start with "Hi, I'm a developer who does X and I'm going to talk about Y and maybe Z."&lt;BR&gt;It's all part of Microsoft's new image -- we're transparent now.&lt;/P&gt;
&lt;P&gt;Transparency's good, right?&lt;/P&gt;
&lt;P&gt;Go down to your local German car dealer, or, if you own a newer German car, go out to your driveway.&amp;nbsp; Shiny, pretty, isn't it?&amp;nbsp; Open the hood.&amp;nbsp; Look at that -- a big, fat, intake pipe that goes into a big box that says "BMW" in a 4" Century Gothic font.&amp;nbsp; Cool.&amp;nbsp; Now pop that plastic thing off the top.&amp;nbsp; Go ahead, I dare you.&amp;nbsp; Not so pretty now.&amp;nbsp; Look at all those wires and tubes.&amp;nbsp; Look at all that stuff you could cut yourself on.&amp;nbsp; That's why that plastic thing is there; it makes owning all that power a little less scary.&lt;/P&gt;
&lt;P&gt;Some of us spend all our time under the shiny plastic thing, hands full of wires and tubes and spark plugs.&amp;nbsp; That'd be me and some of my less 'transparent' friends, working on the instrumentation and data collection engine in the profiler.&lt;/P&gt;
&lt;P&gt;Of course, with great power, comes great potential for disaster.&amp;nbsp; Some day you'll have your turn with the profiler.&amp;nbsp; Trust me, something's always too slow.&amp;nbsp; If it works, great.&amp;nbsp; If you find yourself upside down in a ditch, tell us, we want to know where we need to cover up the sharp edges.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=143376" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/angryrichard/archive/tags/Profiling/default.aspx">Profiling</category><category domain="http://blogs.msdn.com/angryrichard/archive/tags/Useless+rants/default.aspx">Useless rants</category></item></channel></rss>