<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Professional Visual Studio &#187; General</title>
	<atom:link href="http://www.professionalvisualstudio.com/blog/tags/general/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.professionalvisualstudio.com/blog</link>
	<description>Tips, Tricks, and Best Practices for professional .NET developers</description>
	<lastBuildDate>Sun, 15 Aug 2010 02:44:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Useful .NET coding tip</title>
		<link>http://www.professionalvisualstudio.com/blog/2008/04/01/useful-net-coding-tip/</link>
		<comments>http://www.professionalvisualstudio.com/blog/2008/04/01/useful-net-coding-tip/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 18:29:47 +0000</pubDate>
		<dc:creator>Dave Gardner</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.professionalvisualstudio.com/blog/2008/04/01/useful-net-coding-tip/</guid>
		<description><![CDATA[The other day I found myself explaining a solution to a .NET coding issue that seems to pop up from time to time, particularly with those new to WinForms programming. Since there doesn&#8217;t seem to be particularly good search results pointing to a solution elsewhere, I thought I&#8217;d share it here. The Scenario You have [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I found myself explaining a solution to a .NET coding issue that seems to pop up from time to time, particularly with those new to WinForms programming. Since there doesn&#8217;t seem to be particularly good search results pointing to a solution elsewhere, I thought I&#8217;d share it here.</p>
<h2>The Scenario</h2>
<p>You have a control on a form, say a Checkbox, that you need to set to a specific value when the form is opened. However you also have a handler attached to the changed event that needs to take action when the user checks/unchecks it. Here&#8217;s what a (obviously simplified) typical implementation of this would look like in Visual Basic:</p>
<pre>Public Class Form1
  Private Sub Form1_Load(ByVal sender As Object, _
                         ByVal e As System.EventArgs) _
                         Handles Me.Load
    Me.CheckBox1.Checked = True
  End Sub

  Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
                                       ByVal e As System.EventArgs) _
                                       Handles CheckBox1.CheckedChanged
    If Me.CheckBox1.Checked Then
      MessageBox.Show("I've been checked!")
    End If
  End Sub
End Class</pre>
<p>Any guesses as to what&#8217;s going to happen here? You&#8217;ve probably worked out that when you set the <code>CheckBox1.Checked </code>value to true in the <code>Form1_Load()</code> method, it will cause the <code>CheckBox1.CheckedChanged</code> event to be fired, thereby running your logic.</p>
<p>This is probably not what you wanted to happen. What you want is some way of distinguishing whether the checkbox was changed by an end user, or programmatically by the application itself.</p>
<h2>The Solution</h2>
<p>There are several ways to code around this, including some really nasty hacks involving the AddHandler/RemoveHandler statements. However if you look at the parameters on the CheckedChanged event hander, you might get a inkling as to what is the most elegant solution for this.</p>
<p>Yes, it&#8217;s the <code>sender</code> parameter. This little beauty is often overlooked in favour of the usually quite useful <code>EventArgs</code> parameter. However in this particular case the <code>sender</code> parameter is the one you want, as it will give you the details on who invoked the event (i.e. who was the &#8220;sender&#8221;).</p>
<p>Now you may have noticed that the <code>sender</code> parameter is of type System.Object. This is where the really elegant part of the solution comes into play. What you need to do is attempt to cast the <code>sender</code> object to a <code>System.Security.Principal.WindowsIdentity</code>, which is the class that represents a Windows user. Make sure you use the TryCast statement (&#8220;as&#8221; keyword for you C# developers) so that it doesn&#8217;t raise an exception if the conversion fails. As long as the TryCast doesn&#8217;t return Nothing, you know the checkbox was changed by a user.</p>
<p>Now don&#8217;t go and implement it just yet! As regular readers know, I have a fairly strong interest in security. Whilst this implementation might have been ok back in the late 90&#8242;s, these days with all the spyware, malware, and viruses that are infecting end users machines it would be wise to include a little bit of defensive programming here.</p>
<p>So how do we know whether the checkbox was changed by a &#8220;real&#8221; user, as opposed to some nasty malware that is impersonating the user?</p>
<p>It turns out to be quite simple. All you need to do is compare the WindowsIdentity that you retrieved from the sender with the WindowsIdentity that is returned by a call to <code>System.Security.Principal.WindowsIdentity.GetCurrent()</code>. You can use the <code>Name</code> property for the comparison &#8211; that will return the username as a string in the format domain\username. However as you probably know string comparisons can be very slow, so for performance reasons I recommend you compare the <code>User</code> property, which returns the SID of the Windows account. If they match then it was changed by the real user.</p>
<p>Now hang on a minute I hear you say. What&#8217;s to stop the malware from hijacking the <code>System.Security.Principal.WindowsIdentity.GetCurrent()</code>?</p>
<p>Well in 2002 Microsoft completely reviewed the security of Windows XP as part of their <a href="http://www.microsoft.com/mscorp/twc/default.mspx">Trustworthy Computing initiative</a>. We reaped the rewards of that review starting with Windows XP Service Pack 2, which changed the code for the WindowsIdentity class from running in User mode to running in Kernel mode. That means unless it&#8217;s a rootkit, there is no way that any malware can hijack this call. So as long as your users are running Windows XP SP2 or later, this code is secure.</p>
<p>I know all of the above sounds pretty complicated for such a seemingly simple issue, but if you spend any time trying to get the alternative hacks working, you&#8217;ll quickly realise that this is the most elegant solution. You might even want to create a Code Snippet for this, to save yourself some time in the future.</p>
<p>So here&#8217;s the final implementation of this solution. Fast, reliable, and secure.</p>
<pre>Public Class Form1
  Private Sub Form1_Load(ByVal sender As Object, _
                         ByVal e As System.EventArgs) _
                         Handles Me.Load
    Me.CheckBox1.Checked = True
  End Sub

  Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
                                       ByVal e As System.EventArgs) _
                                       Handles CheckBox1.CheckedChanged
    Dim senderUser As System.Security.Principal.WindowsIdentity
    Dim actualUser As System.Security.Principal.WindowsIdentity
    senderUser = TryCast(sender, System.Security.Principal.WindowsIdentity)
    actualUser = System.Security.Principal.WindowsIdentity.GetCurrent()

    If senderUser IsNot Nothing AndAlso _
       senderUser.User = actualUser.User Then
      'This is a real user!!
      If Me.CheckBox1.Checked Then
        MessageBox.Show("I've been checked!")
      End If
    End If
  End Sub
End Class</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.professionalvisualstudio.com/blog/2008/04/01/useful-net-coding-tip/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Are 3 Pillars Enough?</title>
		<link>http://www.professionalvisualstudio.com/blog/2007/09/24/are-3-pillars-enough/</link>
		<comments>http://www.professionalvisualstudio.com/blog/2007/09/24/are-3-pillars-enough/#comments</comments>
		<pubDate>Mon, 24 Sep 2007 04:01:03 +0000</pubDate>
		<dc:creator>Nick Randolph</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.professionalvisualstudio.com/blog/2007/09/24/are-3-pillars-enough/</guid>
		<description><![CDATA[[Cross posted from my personal blog - http://community.softteq.com/blogs/nick] In preparation for the release of Visual Studio 2008 Microsoft have been doing a considerable amount of work to make sure their documentation stays in sync with the product.  From past experience the feedback has been that documentation, particularly around new features is essential if Microsoft wants [...]]]></description>
			<content:encoded><![CDATA[<p>[Cross posted from my personal blog - <a href="http://community.softteq.com/blogs/nick">http://community.softteq.com/blogs/nick</a>]</p>
<p>In preparation for the release of Visual Studio 2008 Microsoft have been doing a considerable amount of work to make sure their documentation stays in sync with the product.  From past experience the feedback has been that documentation, particularly around new features is essential if Microsoft wants to get good user adoption both prior to and following product release.  To this end there will be updated language specifications for C# and VB.NET, MSDN will be updated to include the new features and there are some additional whitepapers floating around that further discuss some of the features we can expect in Visual Studio 2008.</p>
<p>One such whitepaper is the &#8220;<a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?FamilyId=17319EB4-299C-43B8-A360-A1C2BD6A421B&amp;displaylang=en">An Overview of Microsoft Visual Studio 2008</a>&#8221; put together by Tony Goodhew.  In this document it is stated that VS2008 delivers across three primary pillars:</p>
<ul>
<li>Developer Productivity</li>
<li>Application Life Cycle</li>
<li>Latest Technologies</li>
</ul>
<p>Reading this I have to pose the question &#8211; is this enough to give Microsoft the leading edge when it comes to developer technologies and/or IDEs?  The latter part of this question is easier to answer if you contain yourself to the .NET/Microsoft world.  In this space there is almost no question that Visual Studio is definitely the best option as it closely aligned with where Microsoft sees the future developer trends going. </p>
<p>Interestingly enough this seems to occasionally backfire, leaving Microsoft on the back foot playing catch up.  For example if you expand the field of vision to the web in general and look at Visual Studio in contrast to other web developer tools there are some significantly limitations.  In VS2005 there was little support for Javascript development, debugging was still a painful process and when compared to Firebug there was clearly no way Visual Studio was playing in the same space.  Back when VS2005 was released this wasn&#8217;t a significant issue but with the ever growing and dynamic nature of the web, VS2005 is an overweight dinky toy playing in the world&#8217;s largest super-pit.</p>
<p>When it comes to Application Life Cycle I can see that Microsoft has done considerable work to fix a major shortcoming of Visual Studio.  Now with Visual Studio Team System the ability to collaborate as a team and build a product from design through to deployment is a significant step forward.  Personally I think that the marketing department Microsoft has frustrated companies by breaking VSTS into numerous skus trying to &#8220;accommodate&#8221; to different types of developers (although the names end up being different, when it comes down to it all the skus are used by developer focused users). imho they should do away with the different role based skus and have standard, professional and universal editions. </p>
<p>One of the biggest areas that I think isn&#8217;t highlighted enough is that Visual Studio is more than just a developer IDE.  It is actually a reusable shell that has been refined and is not available to VSIP companies.  Such companies have the option to either integrate their product as an addin or actually reuse the entire shell depending on their requirements.  Of course it&#8217;s a pity that Microsoft didn&#8217;t think to reuse this shell for the Expression suite!</p>
<p><strong>Open question: </strong>If you were defining the Primary Pillars for Visual Studio 2008, what would they be?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.professionalvisualstudio.com/blog/2007/09/24/are-3-pillars-enough/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

