<?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>Nothing ventured, nothing gained</title><link>http://blogs.msdn.com/jonwis/default.aspx</link><description>[insert pretentious in-joke here]</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Super-sharpening(?) the image</title><link>http://blogs.msdn.com/jonwis/archive/2009/07/24/super-sharpening-the-image.aspx</link><pubDate>Sat, 25 Jul 2009 09:15:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9848171</guid><dc:creator>jonwis</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jonwis/comments/9848171.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jonwis/commentrss.aspx?PostID=9848171</wfw:commentRss><description>&lt;p&gt;I may have forgotten to mention it, but my expertise is &lt;em&gt;not&lt;/em&gt; in image processing.&amp;#160; The people who know better will likely tsk mightily at this and upcoming posts. My apologies. =)&lt;/p&gt;  &lt;p&gt;Last time got me a b&amp;amp;w-converted version of a picture, but didn’t do much with it.&amp;#160; Next up, I need a sequence of integer values from each row in the image for more processing.&amp;#160; Eventually I want to convert those numbers to simple on-and-off values for processing.&amp;#160; For starters, it seems like I should be able to convert each RGB pixel value into its 0-255 greyscale equivalent.&amp;#160; Once I’ve got a byte, my simple theory is that squaring that 8-bit grayscale will produce a 16-bit ushort to process.&lt;/p&gt;  &lt;p&gt;Thus:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Access the bits of the image &lt;/li&gt;    &lt;li&gt;Convert each pixel to greyscale &lt;/li&gt;    &lt;li&gt;Push “light” pixels towards some high-value, push “dark” pixels down towards some low-value &lt;/li&gt;    &lt;li&gt;Wrap that up in some sort of enumerator.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;With a little yield fun, I ended up with an enumerator that produces ushort[] from each row in the image.&amp;#160; &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Why ushorts? 255*255 = 65025, which is less than 65535.&amp;#160; &lt;/li&gt;    &lt;li&gt;Why not put the RowData function into the enumerator with the yield?&amp;#160; Because C# doesn’t allow unsafe code blocks in iterators.&amp;#160; Wierd.&lt;/li&gt;    &lt;li&gt;I’ll need to put the Bitmap acquisition and the LockData outsidethe enumerator, but that’s OK.&lt;/li&gt; &lt;/ul&gt;  &lt;pre&gt;public class ImageRowEnumerator : IEnumerable&amp;lt;ushort[]&amp;gt; {
    BitmapData _bmpData;
    int _rowSkip;

    public ImageRowEnumerator(BitmapData b, int skip) {
        _bmpData = b;
        _rowSkip = skip;
    }

    public IEnumerator&amp;lt;ushort[]&amp;gt; GetEnumerator() {
        for (int row = 0; row &amp;lt; _bmpData.Height; row += _rowSkip)
            yield return RowData(_bmpData, row);
    }

    IEnumerator IEnumerable.GetEnumerator() {
        return GetEnumerator();
    }

    unsafe ushort[] RowData(BitmapData bmpData, int row) {
        ushort[] row_data = new ushort[_bmpData.Width];
        byte* pData = (byte*)bmpData.Scan0 + (bmpData.Stride * row);
        for (int col = 0; col &amp;lt; _bmpData.Width; col++, pData += 3) {
            ushort color = (ushort)((pData[0] * .3f) + (pData[1] * .59f) + (pData[2] * .11f));
            row_data[col] = (ushort)(color * color);
        }
        return row_data;
    }
}&lt;/pre&gt;

&lt;p&gt;Next time, converting from the ushort values to on/off.&amp;#160; It’s not just checking for 50%.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9848171" width="1" height="1"&gt;</description></item><item><title>Converting an Image (well, Bitmap really) to Grayscale</title><link>http://blogs.msdn.com/jonwis/archive/2009/07/18/converting-an-image-well-bitmap-really-to-grayscale.aspx</link><pubDate>Sat, 18 Jul 2009 23:59:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9838976</guid><dc:creator>jonwis</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jonwis/comments/9838976.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jonwis/commentrss.aspx?PostID=9838976</wfw:commentRss><description>&lt;p&gt;First thought – convert the picture to greyscale and increase the contrast to help any kind of edge-detection work.&amp;#160; Some searching found me &lt;a href="http://bobpowell.net/image_contrast.htm"&gt;Bob Powell's handy guide to increasing contrast&lt;/a&gt; and &lt;a href="http://bobpowell.net/grayscale.htm"&gt;converting to grayscale&lt;/a&gt; via ColorMatrix.&amp;#160; With Wikipedia’s explanation of “naive” matrix multiplication in hand I cobbled up a converter that takes a bitmap and returns a grayscaled version.&lt;/p&gt;  &lt;pre&gt;    public class GreyscaleBitmap
    {
        // Magic conversion to greyscale matrix
        static float[][] _grayScaleMatrix = new float[][] {
            new float[] {.3f, .3f, .3f, 0, 0},
            new float[] {.59f, .59f, .59f, 0, 0},
            new float[] {.11f, .11f, .11f, 0, 0},
            new float[] {0, 0, 0, 1, 0},
            new float[] {0, 0, 0, 0, 1}
        };

        // Doubles contrast
        static float[][] _increaseContrast = new float[][] {
            new float[] {2, 0, 0, 0, 0},
            new float[] {0, 2, 0, 0, 0},
            new float[] {0, 0, 2, 0, 0},
            new float[] {0, 0, 0, 1, 0},
            new float[] {0, 0, 0, 0, 1}
        };

        // Dumb and slow.
        static float[][] MatrixMult2(float[][] first, float[][] second)
        {
            float[][] result = new float[5][];

            for (int i = 0; i &amp;lt; 5; i++)
                result[i] = new float[5];

            for (int j = 0; j &amp;lt; 5; j++)
                for (int k = 0; k &amp;lt; 5; k++)
                    for (int i = 0; i &amp;lt; 5; i++)
                        result[i][j] += first[i][k] * second[k][j];

            return result;
        }

        // Only bother making one of these
        static ColorMatrix _matrix = new ColorMatrix(MatrixMult2(_grayScaleMatrix, _increaseContrast));

        // Cache this as well?
        static ImageAttributes GetAttributes()
        {
            ImageAttributes result = new ImageAttributes();
            result.SetColorMatrix(_matrix);
            return result;
        }

        public static Bitmap Convert(Bitmap bmp)
        {
            Bitmap newBitmap = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format24bppRgb);

            using (Graphics g = Graphics.FromImage(newBitmap))
            {
                Rectangle r = new Rectangle(0, 0, newBitmap.Width, newBitmap.Height);
                g.DrawImage(bmp, r, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, GetAttributes());
            }

            return newBitmap;
        }
    }&lt;/pre&gt;

&lt;p&gt;Now, in another world the output image format would be 16bpp grayscale directly, instead of being re-encoded in 24bpp RGB.&amp;#160; As a result, my eventual scanner will have to convert back from RGB to a grayscale value &lt;em&gt;again&lt;/em&gt;.&amp;#160; Not a great start, but hey – conversion to grayscale!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9838976" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jonwis/archive/tags/grayscale/default.aspx">grayscale</category><category domain="http://blogs.msdn.com/jonwis/archive/tags/upc/default.aspx">upc</category></item><item><title>Voyage of the S.S. UPC Scanner</title><link>http://blogs.msdn.com/jonwis/archive/2009/07/18/voyage-of-the-s-s-upc-scanner.aspx</link><pubDate>Sat, 18 Jul 2009 23:24:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9838943</guid><dc:creator>jonwis</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jonwis/comments/9838943.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jonwis/commentrss.aspx?PostID=9838943</wfw:commentRss><description>&lt;p&gt;I got this silly idea in my head last week that I’d take my brand-new high-def handheld video camera and use it to produce a catalog of books, DVDs, etc. that are lying around the house.&amp;#160; Simple premise – turn on the camera and wave games, DVDs, etc. at it.&amp;#160; Every Nth frame in the video stream, search for UPC codes.&amp;#160; Produce the unique set of codes and look them up in any one of the existing online databases. Poof – a complete record of whatever I’ve got sitting around.&lt;/p&gt;  &lt;p&gt;And along the way learn WPF, image manipulation, video processing, LINQ, etc. etc.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9838943" width="1" height="1"&gt;</description></item><item><title>What's winsxs\manifestcache\(something)_blobs.bin?</title><link>http://blogs.msdn.com/jonwis/archive/2009/01/13/what-s-winsxs-manifestcache-lt-something-gt-blobs-bin.aspx</link><pubDate>Wed, 14 Jan 2009 01:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9317304</guid><dc:creator>jonwis</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jonwis/comments/9317304.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jonwis/commentrss.aspx?PostID=9317304</wfw:commentRss><description>&lt;P&gt;Glenn writes:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;"What is the c:\windows\winsxs\manifestcache\6.0.6001.18000_001c50b5_blobs.bin file and why is it taking up 156Mb on my hard drive? Do I really need a file this big?"&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That file is used by the Windows Update mechanisms in Vista.&amp;nbsp; It acts like a cache and is capped at a certain size, but it's safe to delete.&amp;nbsp; The next time you turn on or off Windows optional features or take updates from Windows Update it'll reappear.&amp;nbsp;The file will be recreated as necessary to help the servicing system do its work.&amp;nbsp; Maybe in the future it'll be added to the disk-cleaup wizard, but it noticably increases the performance of servicing operations while it's there.&lt;/P&gt;
&lt;P&gt;Of course, actually deleting the file is left as an excercise to the reader.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9317304" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jonwis/archive/tags/servicing/default.aspx">servicing</category><category domain="http://blogs.msdn.com/jonwis/archive/tags/blobs.bin/default.aspx">blobs.bin</category><category domain="http://blogs.msdn.com/jonwis/archive/tags/update/default.aspx">update</category><category domain="http://blogs.msdn.com/jonwis/archive/tags/manifestcache/default.aspx">manifestcache</category></item><item><title>Shipping default values in the registry</title><link>http://blogs.msdn.com/jonwis/archive/2008/09/03/shipping-default-values-in-the-registry.aspx</link><pubDate>Wed, 03 Sep 2008 23:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8922662</guid><dc:creator>jonwis</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jonwis/comments/8922662.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jonwis/commentrss.aspx?PostID=8922662</wfw:commentRss><description>&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Recently, someone asked about how their code should work if a certain configuration setting was missing from the registry. The responses were mixed.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;One suggested checking the value of RegQueryValue. Another suggested that the missing value should cause the app to fail. The right answer is "it should use the default value."&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;"But hark!" you say - "the question was about how to deal with the missing default value!"&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;And the answer doesn't change - have a fallback strategy that can still pull a default value out of your ear at runtime.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Ok then, let's suppose you've done this and now have code that looks like "For this configuration widget, read the current value; if there is no current value, read the default; if there is no default, use &amp;lt;mystery value&amp;gt; instead."&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;That's three levels of decision making, three independent code paths for your test team to validate.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;What if you could just cut out that middle piece and go back to two - a default in-code and a possible per-user override?&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;This leads me to the following broad statement - &lt;B style="mso-bidi-font-weight: normal"&gt;Defaults should be shipped in code, user configurations saved on the device in stable storage. &lt;/B&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;Three reasons:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 0.5in; mso-list: l0 level1 lfo1" class=MsoListParagraphCxSpFirst&gt;&lt;SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT size=3 face=Calibri&gt;1.&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;Setting a value during setup is a point of failure.&lt;/B&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Yes it almost never happens.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;But it happens.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;The best setup you can imagine is one done by (logical) xcopy.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Imagine your app being run from a network share.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;The vast majority of applications - web apps, for instance - have no "install" step.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;They just start up and use default settings until overridden by a user-initiated configuration step.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 0.5in; mso-list: l0 level1 lfo1" class=MsoListParagraphCxSpMiddle&gt;&lt;SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT size=3 face=Calibri&gt;2.&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;"Reset to known good state" should be "delete all overridden settings and start again."&lt;/B&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Apps that have "repair" fail mightily when they try to distinguish important things to keep from unimportant things to keep.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;If the app is able to start up and run correctly from a completely clean state "repair" can be "delete this key, restart the app." &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 10pt 0.5in; mso-list: l0 level1 lfo1" class=MsoListParagraphCxSpLast&gt;&lt;SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT size=3 face=Calibri&gt;3.&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;Servicing of user-defaults is impossible without an override model&lt;/B&gt;.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Imagine a default color value like "blue".&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;The user changes her mind and picks "orange" instead.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Later they decide they like "blue" again.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Now imagine an update coming down for your application that changes the default to "white".&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;A naïve updater might look at the old default (blue) and the current setting (blue) and say "aha! They're still on the default, change to white!" which is not what the customer intended.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;They consciously chose "blue".&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Granted, this means your settings model needs a "use default" button, but that's a small price to pay for customer sat.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Every interesting app has some form of configuration the user or the environment can supply it.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Well-designed apps work correctly even when there are no configurations provided at all.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Rude Q&amp;amp;A:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 0.5in; mso-list: l1 level1 lfo2" class=MsoListParagraphCxSpFirst&gt;&lt;SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT size=3 face=Calibri&gt;1.&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT size=3 face=Calibri&gt;"But I don't want to recompile to change my defaults!"&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Have your default values in a text file in your app deployment unit.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Managed code provides an app configuration model based on yourexe.exe.config, which is reasonably OK.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Others use a .txt file structured like an INF. &lt;/FONT&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 0.5in; mso-list: l1 level1 lfo2" class=MsoListParagraphCxSpMiddle&gt;&lt;SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT size=3 face=Calibri&gt;2.&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT size=3 face=Calibri&gt;"But I can't possibly change all my code to use an override system!"&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Have you checked?&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;I bet there's one or two interesting functions in your code that read those settings that you could change.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;If there are many functions that all do configuration checks, refactor them into a single point of access where you can do this.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 10pt 0.5in; mso-list: l1 level1 lfo2" class=MsoListParagraphCxSpLast&gt;&lt;SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT size=3 face=Calibri&gt;3.&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;"But having defaults in code means image bloat!" A dword in code is maximally 4 bytes in the const data section, less-so if the optimizer inlines uses of it.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;A reg_dword is those four bytes plus the size of the registry value, the key that references it, the parent key of that key, and so on.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;You're trading a small fixed overhead for a larger unknown overhead.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;Note: this is orthogonal to registering with the system (such as creating services, registering COM goo, etc.).&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Apps that integrate with the system do need an "announce" step (call it 'install' if you prefer).&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;The announcement-management should be owned, debugged, and tested by a central team.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Windows, CE, and others all do this wrong today for legacy reasons.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;MSI tries to make this better by requiring you to register COM objects through their tables, but it's a stopgap because they still allow calling into DllRegisterServer.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;One can imagine a system where a data-driven “announcement” mechanism is the only way to get integrated with the system, and the announcing application gets to run no actual code of its own.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8922662" width="1" height="1"&gt;</description></item><item><title>Deleting from the WinSxS directory</title><link>http://blogs.msdn.com/jonwis/archive/2007/01/02/deleting-from-the-winsxs-directory.aspx</link><pubDate>Tue, 02 Jan 2007 23:31:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1400105</guid><dc:creator>jonwis</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jonwis/comments/1400105.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jonwis/commentrss.aspx?PostID=1400105</wfw:commentRss><description>&lt;p&gt;In Windows Vista, the directory %windir%\WinSxS has much stronger protection on it than it did in Windows XP and Server 2003.&amp;nbsp; The owner/group is now a SID named "Trusted Installer", a service SID used to start the TrustedInstaller service.&amp;nbsp; Users other than the trusted installer are granted only generic-read/generic-execute by default.&amp;nbsp; This increased protection ensures that only the trusted installer service is allowed to modify the servicing-related metadata and files.&amp;nbsp; If a limited user could modify a file in the directory, for example, they could convince the servicing stack to overwrite one binary with another when the next administrator comes along to enable the Games for Windows package.&lt;/p&gt; &lt;p&gt;Content is added to this directory in response to installing applications, enabling packages in the add-remove-programs UI, and installing Windows Out-of-Band releases.&amp;nbsp; Content is removed from this directory as a result of uninstall + scavenging - a topic for another time.&amp;nbsp; One important note - uninstalling your application or Windows app will not &lt;em&gt;necessarily&lt;/em&gt; remove the physical bits from the system.&amp;nbsp; The servicing stack marks the bits as unusable and prevents their use through "normal" means.&amp;nbsp; Files and directories will be removed over time as the servicing system cleans up after itself.&amp;nbsp; Administrators should not, for any reason, take it upon themselves to clean out the directory - &lt;strong&gt;doing so may prevent Windows Update and MSI from functioning properly afterwards&lt;/strong&gt;.&amp;nbsp; Preventing accidental deletion from the directory is accomplished by putting a strong security descriptor on the directory that inherits to its children.&lt;/p&gt; &lt;p&gt;The directory itself is marked with the &lt;a href="http://msdn2.microsoft.com/en-us/library/aa379567.aspx"&gt;SDDL&lt;/a&gt;:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;O:S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464&lt;/li&gt; &lt;li&gt;G:S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464&lt;/li&gt; &lt;li&gt;D:PAI&lt;/li&gt; &lt;ul&gt; &lt;li&gt;(A;OICI;FA;;;S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464)&lt;/li&gt; &lt;li&gt;(A;OICI;0x1200a9;;;BA)&lt;/li&gt; &lt;li&gt;(A;OICI;0x1200a9;;;SY)&lt;/li&gt; &lt;li&gt;(A;OICI;0x1200a9;;;BU)"&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt; &lt;p&gt;That long &lt;a href="http://msdn2.microsoft.com/en-us/library/aa379602.aspx"&gt;SID&lt;/a&gt; (S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464) is the service sid for Trusted Installer.&amp;nbsp; By parts, that SDDL means "the owner and group are Trusted Installer; there is a&amp;nbsp;&lt;a href="http://msdn2.microsoft.com/en-gb/library/aa379573.aspx"&gt;protected&lt;/a&gt; and auto-inherit &lt;a href="http://msdn2.microsoft.com/en-gb/library/ms721532.aspx"&gt;DACL&lt;/a&gt;; the ACEs of that DACL grant the trusted installer full access; the ACEs of the DACL also grant builtin-administrators, local system, and builtin users generic-read and generic-write access;&amp;nbsp;each&amp;nbsp;ACE is also automatically inherited by child containers and child objects."&lt;/p&gt; &lt;p&gt;So, the security descriptor on the object will prevent even administrators from creating or deleting objects underneath %windir%\winsxs, which is why even an elevated administrator cannot (by default) delete content.&lt;/p&gt; &lt;p&gt;Elevated administrators do have (by default) the &lt;a href="http://msdn2.microsoft.com/en-us/library/aa375728.aspx#privilege_constants"&gt;SeTakeOwnership privilege&lt;/a&gt; enabled.&amp;nbsp; Such a token could &lt;a href="http://msdn2.microsoft.com/en-us/library/aa379620.aspx"&gt;take ownership of anything&lt;/a&gt; under %windir%\winsxs.&amp;nbsp; Once the owner, the administrator could then reset the SDDL on the object, granting themselves full access to the&amp;nbsp;object.&amp;nbsp; Of course, the generic LUA administrator token does not allow enabling the "take ownership" privilege, so this is only possible after answering the LUA elevation prompt successfully.&lt;/p&gt; &lt;p&gt;I leave it to the intrepid user to figure out the correct combination of "takeown.exe", "icacls.exe", and "rmdir" to actually remove content.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1400105" width="1" height="1"&gt;</description></item><item><title>On-demand multhreaded critical section creation</title><link>http://blogs.msdn.com/jonwis/archive/2006/04/27/585685.aspx</link><pubDate>Fri, 28 Apr 2006 04:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:585685</guid><dc:creator>jonwis</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jonwis/comments/585685.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jonwis/commentrss.aspx?PostID=585685</wfw:commentRss><description>&lt;P&gt;A question came up on an internal newsgroup recently - "How do I do on-demand initialization of critical sections in a multithread-aware library?"&amp;nbsp; The asker didn't have an explicit Initialize function in which his critical section could be created, and instead wanted to know what the right approach was for creating one on demand. Below I provide a sample of how this could be achieved.&amp;nbsp; Next time, I'll have a "clarification" of the sample that has a better debugging profile.&lt;/P&gt;
&lt;p&gt;
&lt;pre&gt;// This structure is only a holder for a pointer for now.
typedef struct _ONETIME_CS {
  CRITICAL_SECTION volatile *Cs;
} ONETIME_CS, *PONETIME_CS;

BOOL InitOnetimeCs(PONETIME_CS pCs) {
  MemoryBarrier();
  if (pCs-&gt;Cs == NULL) {
    CRITICAL_SECTION *NewCs;
    NewCs = (CRITICAL_SECTION)HeapAlloc(GetProcessHeap(), 0, sizeof(*NewCs));
    if (NewCs == NULL) {
      SetLastError(ERROR_OUTOFMEMORY);
      return FALSE;
    }
    // InitCs and DeleteCs fail by raising SEH exceptions. When
    // those fly by, make sure to free the heap. The loser of
    // the race deletes the losing CS, and the __finally ensures
    // that it gets freed. The winner sees 'null' from the ICEP,
    // all others get what the winner put there.
    __try {
      PVOID OldCs;
      InitializeCriticalSection(NewCs);
      OldCs = InterlockedCompareExchangePointer(
        (volatile PVOID *)&amp;pCs-&gt;Cs, NewCs, NULL);
      if (OldCs == NULL) {
        NewCs = NULL;
      } else {
        DeleteCriticalSection(NewCs);
      }
    } __finally {
      if (NewCs != NULL)
        HeapFree(GetProcessHeap(), 0, (PVOID)NewCs);
    }
  }
  MemoryBarrier();
  return (Guarded-&gt;Cs != NULL);
}

BOOL EnterOnetimeCs(PONETIME_CS pCs) {
  if (pCs != NULL) {
    if (!InitOnetimeCs(pCs))
      return FALSE;
  }
  EnterCriticalSection(pCs-&gt;Cs);
  return TRUE;
}

VOID LeaveOnetimeCs(PONETIME_CS pCs) {
  LeaveCriticalSection(pCs-&gt;Cs);
}

VOID DeleteOnetimeCs(PONETIME_CS pCs) {
  PVOID OldCs;
  // Delete can be multithread-aware as well, so use IEP to
  // atomically swap out the value with NULL.  The winner gets
  // a non-null value back, losers get NULL.
  OldCs = InterlockedExchangePointer(&amp;pCs-&gt;Cs, NULL);
  if (OldCs != NULL) {
    DeleteCriticalSection((CRITICAL_SECTION*)OldCs);
    HeapFree(GetProcessHeap(), 0, OldCs);
  }
}

ONETIME_CS g_Cs = { NULL };
DWORD MyThreadProc(PVOID) {
  if (!EnterOnetimeCs(&amp;g_Cs))
    RaiseError(ERROR_INTERNAL_ERROR, 0, 0, NULL);
  // do something
  LeaveOnetimeCs(&amp;g_Cs);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;This is just another example of "racy initialization" using InterlockedCompareExchangePointer.  Some "racy init" patterns are dangerous - especially those that involve "read after write" of the data that's been initialized.  That's what the memory barrier is there for - it guarantees (expensively) that any writes InitializeCriticalSection might have performed will be written back before the following call to EnterCriticalSection.&lt;/p&gt;

&lt;p&gt;This method is cheap, but it does require extra heap and may create a few critical sections when there is contention.  Luckily, the cost in CS creation is about zero in Vista - entering the CS when there is contention is where the real cost lies.  Certain analysis methods are made harder - when the app verifier prints the address of a busted CS, "&lt;tt&gt;ln &lt;i&gt;thataddress&lt;/i&gt;&lt;/tt&gt;" won't tell you which symbol contained the critical section.&lt;/p&gt;

&lt;p&gt;Next time, a method that avoids spurious CS creation, allows for "&lt;tt&gt;ln &lt;i&gt;thataddress&lt;/i&gt;&lt;/tt&gt;" on the CS address, and has zero heap cost!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=585685" width="1" height="1"&gt;</description></item><item><title>DLLs and resource ID 2 manifests</title><link>http://blogs.msdn.com/jonwis/archive/2006/01/17/514192.aspx</link><pubDate>Wed, 18 Jan 2006 09:53:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:514192</guid><dc:creator>jonwis</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jonwis/comments/514192.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jonwis/commentrss.aspx?PostID=514192</wfw:commentRss><description>&lt;P&gt;Manifests at resource ID 2 help simplify the lives&amp;nbsp;of DLL authors who want to consume side-by-side components via static imports. Just before processing your DLL's static imports and calling its entrypoint, the loader will create and activate an activation context based on the manifest that it finds at resource ID 2. This ensures that all your static imports are processed in the correct context - if this wasn't the case, binding your DLL's static imports would be affected by whoever was calling LoadLibrary("yourdll.dll"). Not a big deal, until you find out that they &lt;EM&gt;also &lt;/EM&gt;had some dll loaded also called "zop.dll" with an export "Flarn", but whose parameter list looked completely different. The autoactivation ensures that your DLL is bound to the right "zop.dll" with the right "Flarn" export (at least subject to publisher policy.)&lt;/P&gt;
&lt;P&gt;The logic (not the implementation) looks something like the following:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;LoadLibrary("yourdll.dll") is called by the application 
&lt;LI&gt;"yourdll.dll" is found using using the current context plus the &lt;A href="http://msdn.microsoft.com/library/en-us/dllproc/base/dynamic-link_library_search_order.asp"&gt;usual DLL lookup mechanisms.&lt;/A&gt; 
&lt;LI&gt;When found, if yourdll.dll is not already loaded into this context, the DLL is mapped. 
&lt;LI&gt;&lt;A href="http://msdn.microsoft.com/library/en-us/sbscs/setup/createactctx.asp"&gt;CreateActCtx&lt;/A&gt; is called using the mapped DLL's HMODULE, passing resource ID MAKEINTRESOURCE(2) as the resource number. 
&lt;LI&gt;If that succeeded, the resulting context is activated. 
&lt;LI&gt;The static imports of "yourdll.dll" are resolved, possibly recursively executing these operations until all the statically referenced DLLs have been loaded. 
&lt;LI&gt;The DLL entrypoint for "yourdll.dll" is invoked. 
&lt;LI&gt;If a context was activated above, it's deactivated. 
&lt;LI&gt;Control returns to the application with an appropriate HMODULE &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;You can use any manifest you care to in your resource section. Visual Studio has options for embedding manifests at the right resource ID. Nikola has some tips for doing so in &lt;a href="http://blogs.msdn.com/nikolad/articles/425359.aspx"&gt;both makefiles&lt;/A&gt; and &lt;a href="http://blogs.msdn.com/nikolad/archive/2005/06/05/425360.aspx"&gt;the VS IDE&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://msdn.microsoft.com/library/en-us/sbscs/setup/publisher_configuration.asp"&gt;Publisher policy&lt;/A&gt; is of course applied, but &lt;A href="http://msdn.microsoft.com/library/en-us/sbscs/setup/per-application_configuration_on_windows_server_2003.asp"&gt;application policy (ie: foo.exe.policy)&lt;/A&gt; is not. The usual "look in the app directory for &lt;A href="http://msdn.microsoft.com/library/en-us/sbscs/setup/private_assemblies.asp"&gt;private assemblies&lt;/A&gt;" logic applies as well, so this works even if your DLL is distributed privately with an application.&amp;nbsp; See also &lt;A href="http://msdn.microsoft.com/library/en-us/sbscs/setup/using_side-by-side_assemblies_as_a_resource.asp"&gt;Using Side-By-Side Assemblies as a Resource&lt;/A&gt;, which covers when various resource values will be used during the loading and initialization of DLLs and executables.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=514192" width="1" height="1"&gt;</description></item><item><title>C++ object for activating and Deactivating Contexts</title><link>http://blogs.msdn.com/jonwis/archive/2006/01/12/512405.aspx</link><pubDate>Fri, 13 Jan 2006 10:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:512405</guid><dc:creator>jonwis</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jonwis/comments/512405.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jonwis/commentrss.aspx?PostID=512405</wfw:commentRss><description>&lt;P&gt;As explained &lt;a href="http://blogs.msdn.com/jonwis/archive/2006/01/07/510375.aspx"&gt;earlier&lt;/A&gt;, the activation context system is implemented as a per-thread (or per-fiber) stack. Activating a context pushes the context onto the stack, deactivating it pops it from the stack. To ensure that the same sequence of pushes and pops is performed, each &lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sbscs/setup/activateactctx.asp"&gt;ActivateActCtx&lt;/A&gt; call gets a cookie - an opaque value that identifies the activation request. That cookie is then passed to the &lt;A href="http://msdn.microsoft.com/library/en-us/sbscs/setup/deactivateactctx.asp"&gt;DeactivateAcCtx&lt;/A&gt; call, and the system verifies that it matches the context at the top of the stack. If the cookie you ask to deactivate is not the top of the stack, the Win32 SEH exception STATUS_SXS_EARLY_DEACTIVATION is raised; if the cookie is not in the current thread's stack at all, STATUS_SXS_INVALID_DEACTIVATION is raised. These errors are extremely hard to track down.&lt;/P&gt;
&lt;P&gt;Being a good citizen of activation contexts can be done with a simple rule - perform both the activation and deactivation in the same function. Here's a sample C++ object that does this for you. Be sure to compile asynchronous exception unwinding - the &lt;A href="http://msdn.microsoft.com/library/en-us/vccore/html/_core_.2f.EH.asp"&gt;Visual C++ compiler option is /EHa&lt;/A&gt;. If you're using some other compiler, look for an option that calls destructors as both C++ and SEH exceptions flow past your frame and forces the compiler to assume any individual instruction may cause an SEH to be raised.&lt;/P&gt;&lt;PRE&gt;class CActCtxActivator {
  HANDLE m_hContext;
  ULONG_PTR m_Cookie;
  CActCtxActivator(const CActCtxActivator &amp;amp;);
  void operator=(const CActCtxActivator &amp;amp;);
public:
  // Addref the context to avoid someone dereffing it under you
  CActCtxActivator(HANDLE hContext) {
    m_Cookie = 0;
    m_hContext = hContext;
    if (m_hContext != INVALID_HANDLE_VALUE)
      AddRefActCtx(m_hContext);
  }
  // Deactivate if it was active, deref if we had a real one
  ~CActCtxActivator() {
    Deactivate();
    if (m_hContext != INVALID_HANDLE_VALUE) {
      HANDLE hContext = m_hContext;
      m_hContext = INVALID_HANDLE_VALUE;
      ReleaseActCtx(hContext);
    }
  }
  // Activate the context. Feel free to use "throw" if you're a C++ EH kind of
  // person.  Double activation is a programmer's error - this class is not
  // reentrant
  bool Activate() {
    if (m_Cookie != 0)
      RaiseException(ERROR_INTERNAL_ERROR, 0, 0, 0);
    return ActivateActCtx(Ctx.m_hContext, &amp;amp;m_Cookie) ? true : false;
  }
  // Deactivate if activated - somewhat asymmetric behavior with Activate, as
  // it doesn't fail when the context isn't active.
  void Deactivate() {
    if (m_Cookie != 0) {
      ULONG_PTR Cookie = m_Cookie;
      m_Cookie = 0;
      DeactivateActCtx(0, Cookie);
    }
  }
};&lt;/PRE&gt;
&lt;P&gt;So now, you have the ability to control your activation and deactivation automagically with an instance and a global context handle. Assuming that context handle is called g_hActCtx, you can use this in the following way:&lt;/P&gt;&lt;PRE&gt;void Foo() {
  CActCtxActivator Activator(g_hActCtx);
  if (Activator.Activate()) {
    if (Bar()) {
      if (!Zot())
	return;
    }
  }
}&lt;/PRE&gt;
&lt;P&gt;I'll soon use the above object to deal with calling out to plugins to avoid polluting their contexts (the other direction of being a good citizen.) As with any code posted here, it's provided as-is without warranty for any purpose.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT size=2&gt;[Edit 1/17/2006: I knew this felt familiar; my memory is hazy, but I vaguely recall &lt;/FONT&gt;&lt;A href="http://msdn.microsoft.com/library/en-us/sbscs/setup/isolating_components.asp"&gt;&lt;FONT size=2&gt;writing this up&lt;/FONT&gt;&lt;/A&gt;&lt;FONT size=2&gt; before many moons ago...]&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=512405" width="1" height="1"&gt;</description></item><item><title>Fixing Activation Context Pollution</title><link>http://blogs.msdn.com/jonwis/archive/2006/01/07/510375.aspx</link><pubDate>Sat, 07 Jan 2006 12:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:510375</guid><dc:creator>jonwis</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jonwis/comments/510375.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jonwis/commentrss.aspx?PostID=510375</wfw:commentRss><description>&lt;P&gt;As the number of apps in the world that use side-by-side activation (as a result of depending on the new Visual C++ Runtime v8.0) increases, providers of callable code (libraries, control packs, whatever) may start seeing odd and potentially unexpected behavior. Typically it's hard to diagnose. Somewhere deep inside your publicly exposed surface area, a LoadLibrary(gdiplus.dll) fails, or CreateWindow(ComCtlv6WindowClass) fails. You might have fallen victim to context pollution inadvertantly caused by your caller. There is hope however, in that you can control your own destiny even in the face of your callers' context.&lt;/P&gt;
&lt;P&gt;The activation context stack is as it sounds, a per thread stack (on Windows Server 2003 it's per thread or per fiber) of contexts that have been activated. When the process is launched, an initial context may be created and pushed into the process as the process default context. When thread A creates thread B, the currently-active context for A will flow onto B as the top of B's thread context stack. Sometimes, this flow of contexts works in your favor. The windowing system captures the current stack top along with a SendMessage/PostMessage, then ensures that the context is activated before it invokes the target window procedure. Similarly, sending an APC or making a cross-apartment COM call both capture the stack top and push it onto the stack of whatever thread ends up servicing the request before calling whatever code is to be invoked. The important point is that the stack top at the target is the same as the stack top at the call site. When whatever target code was invoked returns, the top of the stack is popped so that the thread returns to the state before the call was performed.&lt;/P&gt;
&lt;P&gt;This automatic flow of contexts ensures that the call executes as if it was running in the same context as when the call was initiated. It only covers a small subset of the possible ways code can be invoked, and it certainly doesn't help the usual case - the "call" instruction generated by the compiler. Unless you (the provider of a library) take special action, your library code will execute using the current context stack top - whether that's what you wanted or not!&lt;/P&gt;
&lt;P&gt;Let's go back to our example to see what this might mean. Assume your library depends on some other side-by-side components that use registration-free COM activation - maybe RTC as an example. When control transfers into your function, the first thing you do is CoCreateInstance(CLSID_SomeRTCObject). Your tests work - you authored "testharness.exe.manifest" which contains a reference to Microsoft.Windows.Networking.RtcDll, so that the process default context created at process launch always contains component registration information for CSLID_SomeRTCObject. Your clients are writing a rendering engine on top of your functionality that uses GDI+, and complain that your function always fails with "class not registered." Doing a little debugging, it turns out that CoCreateInstance is returning that error.&lt;/P&gt;
&lt;P&gt;Your library has just fallen prety to context pollution. The rendering client executable had its own "client.exe.manifest", but it only referred to the GDI+ assembly that it knew it needed. The context it has on the stack when it calls into your code does not have the RtcDll assembly in it, so it can't have any of the COM registration information present either. One immediate fix is to demand that client executables always reference the RtcDll assembly as well, so that the activation information will be present at the time of your CoCreateInstance call. Fortunately, the client balks at this - their app has worked forever, at least until they started using your communication DLL.&lt;/P&gt;
&lt;P&gt;The way out of this mess is to be Master Of Your Own Destiny. Don't like the context you're handed? Create an activate another one! Creating an activation context is easy, if you've burned your manifest into your resource section (a topic for another day). Activating a context is easy. Deactivating a context is easy. The only "hard" part is remembering to activate &amp;amp; deactivate in the right places and at the right times.&lt;/P&gt;
&lt;P&gt;Assume that you have a finely crafted manifest that refers to the correct set of components. You've gone through the right steps, and this manifest is now baked into your DLL's resource section with type RT_MANIFEST at resource ID 1000. To create a new activation context from this manifest, use CreateActCtx:&lt;/P&gt;&lt;PRE class=CODE&gt;HANDLE hMyActCtx = INVALID_HANDLE_VALUE;
ACTCTX Request = { sizeof(Request) };
Request.Flags = ACTCTX_FLAG_HMODULE_VALID | ACTCTX_FLAG_RESOURCE_NAME_VALID;
Request.hModule = g_hMyModuleHandle;
Request.lpResourceName = MAKEINTRESOURCE(1000);
hMyActCtx = CreateActCtx(&amp;amp;Request);
&lt;/PRE&gt;
&lt;P&gt;On success, hMyActCtx will be something other than INVALID_HANDLE_VALUE. You can now use this with ActivateActCtx and DeactivateActCtx:&lt;/P&gt;&lt;PRE&gt;HRESULT MyPublicFunction(...) {
  ULONG_PTR ulpCookie = 0;
  ActivateActCtx(g_hMyActCtx, &amp;amp;ulpCookie);
  CoCreateInstance(CLSID_SomeRTCObject);
  DeactivateActCtx(0, ulpCookie);
}&lt;/PRE&gt;
&lt;P&gt;Now, when CoCreateInstance looks at the active context, it'll see the one you activated rather than the one your client had activated. Pollution solved - you now control your own destiny. Go nuts - have different manifests for different top-level entrypoints. Have one mondo context that contains everything you'll need. No matter what context your caller might have stuck you with, you have complete control over the context you decide to bind code with.&lt;/P&gt;
&lt;P&gt;Next time - making your life easier with C++ RAII around activation and deactivation; "racy initialization" of the activation context, and more.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=510375" width="1" height="1"&gt;</description></item><item><title>What's that awful directory name under Windows\WinSxS?</title><link>http://blogs.msdn.com/jonwis/archive/2005/12/28/507863.aspx</link><pubDate>Thu, 29 Dec 2005 00:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:507863</guid><dc:creator>jonwis</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jonwis/comments/507863.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jonwis/commentrss.aspx?PostID=507863</wfw:commentRss><description>&lt;P&gt;As the &lt;A href="http://www.microsoft.com/downloads/details.aspx?FamilyID=25ae0cd6-783b-4968-a841-38a2743307d9&amp;amp;DisplayLang=en"&gt;Visual C++ Runtimes version 8.0&lt;/A&gt; is now a side-by-side component, you may have seen what looks like an unreasonably complexly named path from which parts of the CRT are loaded. "Golly, what can they possibly be thinking - creating a directory whose name is full of underscores, numbers, and dots?" The good news is, we definitely were thinking something. You may or may not have ever peeked into the %windir%\winsxs directory on your system. If you haven't, now would be a good time. First thing you'll notice is that there are a &lt;EM&gt;lot&lt;/EM&gt; of those funkily-named directories. You might further notice that there seem to be several that differ only by what looks like a version number and some random-looking eight characters on the end of the name. Next you might see that some of them differ only by the second-to-last stringish thing. Lastly, note that mostly, the strings can be deciphered with a little help.&lt;/P&gt;
&lt;P&gt;In the component world, each component has what's called an &lt;EM&gt;identity&lt;/EM&gt;. This is the unique name of the component, generated by the component author and referred-to in manifests and user interfaces. No two &lt;EM&gt;components&lt;/EM&gt; have exactly the same set of properties; if they did, even if the file contents were different, they would be considered the same component. (Note: the CLR abuses this rule and often ships new bits under old identities - that's outside the scope of today's missive.) There's a whole set of rules around how identities work, which I may get to at some point. The key thing is that identities are basically property bags of string triplets - namespace, name, and value - for each attribute. Those attributes in the bag without a namespace are called &lt;EM&gt;well known&lt;/EM&gt; attributes, and there are only a few of those (only Microsoft is currently allowed to define new ones...). Further, certain well-known attributes have rules around their values - the &lt;EM&gt;version&lt;/EM&gt; attribute has to be a dotted-quad version, the &lt;EM&gt;public key token&lt;/EM&gt; attribute has to be a string of hex digits of nonzero but even length. Other well-known attributes like &lt;EM&gt;name&lt;/EM&gt; can be whatever you like - "Foo:Bar:Bas" is ok, as is just "Q".&lt;/P&gt;
&lt;P&gt;Each shared component (in the winsxs directory) gets its own directory into which its payload bits are placed. Somehow, we have to generate (mostly) unique &amp;amp; repeatable directory names for this purpose. The requirements of directory names are reasonably simple - can't overall be more than MAX_PATH (260) characters, can't contain certain characters, etc. Given the naming requirement, it was impossible to use the &lt;EM&gt;entire &lt;/EM&gt;identity as the name of the directory, as someone could name their component "foo\bar" and mess things up. With the extensibility requirement for identities themselves, we couldn't possibly use the entire identity, as the set of tuples would end up being far longer than MAX_PATH. Most importantly, we wanted the directory names to be readable to your average administrator or PSS representative. Finally, generation of the keyform from an identity had to be fast.&lt;/P&gt;
&lt;P&gt;So, &lt;a href="http://blogs.msdn.com/mgrier"&gt;Mike Grier&lt;/A&gt; came up with the idea for a &lt;EM&gt;key form&lt;/EM&gt; of identities. This &lt;EM&gt;key form&lt;/EM&gt; would be a reasonably-unique one-way noncryptographic readable representation of the major defining attributes of an identity. What he ended up with was the following:&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;&lt;EM&gt;proc-arch&lt;/EM&gt;_&lt;EM&gt;name&lt;/EM&gt;_&lt;EM&gt;public-key-token&lt;/EM&gt;_&lt;EM&gt;version&lt;/EM&gt;_&lt;EM&gt;culture&lt;/EM&gt;_&lt;EM&gt;hash&lt;/EM&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P dir=ltr&gt;The italicized strings (except for the hash) are replaced by the values from the identity for their respective properties. If the property was unset, then "none" was put in it place. In the identity model &lt;EM&gt;name, processor architecture&lt;/EM&gt;and &lt;EM&gt;culture&lt;/EM&gt; are allowed to have very laxly-validated contents, so they may contain "unfriendly" characters that have to be filtered. Characters not in the group "A-Za-z0-9.\-_" are removed from the attribute value before being written into the string. Certain attributes have upper-limits places on their values (&lt;EM&gt;name&lt;/EM&gt;is limited to 64 characters, &lt;EM&gt;processor architecture&lt;/EM&gt; to 8, &lt;EM&gt;culture&lt;/EM&gt; to 12) achieved by dropping characters from the middle of the filtered string and replacing them with "..". Finally, the whole string is lower-cased using a clone of the unicode casing table that shipped in Windows XP RTM. &lt;/P&gt;
&lt;P dir=ltr&gt;Voila! A string representing the identity that's filesystem friendly!&lt;/P&gt;
&lt;P dir=ltr&gt;But wait... what about all those characters that got dropped? Couldn't I construct an identity whose keyform matched the keyform of another? Yes, if it weren't for the _&lt;EM&gt;hash&lt;/EM&gt; value on the end of the keyform. This hash (not in the cryptographic sense) is of all the namespaces, names and values of properties in the identity. Anything that didn't appear in the keyform text would have been represented in this hash. The two identities whose names are "Foo!" and "Foo?" will generate different &lt;EM&gt;keyforms &lt;/EM&gt;- while the ! and ? were dropped from the keyform, they still appear in the &lt;EM&gt;hash&lt;/EM&gt;. A coworker did some experiments and determined that while it was possible to reset the hash generation function, it would involve a ridiculous amount of work. &lt;/P&gt;
&lt;P dir=ltr&gt;The algorithm for generating the keyform overall (especially the hash) is undocumented at this time. Not because we're trying for security through obscurity, but because the keyform is merely an implementation artifact at this point. Maybe someday we'll lose our heads entirely and store the component payloads in a database of some sort, in a compound storage document, CAB file, whatever.&amp;nbsp; Also, the algorithm has changed for Vista, and no "normal" use cases for knowing the algorithm exist. If you're trying to find files in the WinSxS directory, you should be using the &lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sbscs/setup/createactctx.asp"&gt;CreateActCtx&lt;/A&gt;/&lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sbscs/setup/activateactctx.asp"&gt;ActivateActCtx&lt;/A&gt;/SearchPath &lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sbscs/setup/using_the_activation_context_api.asp"&gt;set of APIs&lt;/A&gt;. If you're trying to write files into the WinSxS directory, you should be using MSI which &lt;A href="http://msdn.microsoft.com/library/en-us/msi/setup/installation_of_win32_assemblies.asp"&gt;knows about installing components into the right places&lt;/A&gt;. If you're writing your own binder, don't - it's really hard to get right.&amp;nbsp;It should be sufficient to say "the generation of this string is opaque and must be assumed to change."&lt;/P&gt;
&lt;P dir=ltr&gt;But wait... what stops Evil Bob from creating a component with the exact same name and overwriting what Nice Jill had already shipped? Suffice it to say, that &lt;EM&gt;public key token&lt;/EM&gt; has something to do with it - I'll explain that next time, when I talk about signing catalogs for side-by-side components.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=507863" width="1" height="1"&gt;</description></item><item><title>Making an MSM from whole cloth – Series Intro</title><link>http://blogs.msdn.com/jonwis/archive/2005/09/08/462794.aspx</link><pubDate>Fri, 09 Sep 2005 08:56:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:462794</guid><dc:creator>jonwis</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jonwis/comments/462794.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jonwis/commentrss.aspx?PostID=462794</wfw:commentRss><description>&lt;p&gt;Not too long ago, I was asked to provide a set of merge modules for an external team.  The restriction was that they could accept an exe whose output was the merge module in question, and it had to generate stable – based on the content – component IDs.  I’m being cagy because the team in question hasn’t hit RTM yet, I don’t want to steal their thunder.&lt;/p&gt;

&lt;p&gt;My first inkling was to use the excellent WiX tools.  I’d have my tool cobble up a .wxs file and CreateProcess on both "light" and "candle", creating the "all in one" tool experience the team wanted.  Here I was, coding along, when the team in question came back and said "Sorry, we don’t think we can support using WiX in our build process either directly or indirectly."  Enter roadblock number one.  I’d never used the MSI APIs to generate modules, let alone interact with them!  Several hours and hundreds of links followed on MSDN later I was headed down a path of hand-creating the entire merge module from scratch – cabs and all.&lt;/p&gt;

&lt;p&gt;The most frequent criticism I have about our documentation is that while it provides small use cases for individual APIs, it’s rare (outside the Crypto docs, which are excellent) to find an end-to-end all-encompassing demo for complex use cases.  All the items I found were disjoint micro-samples about how to read and query MSI databases and what - at a broad level – a merge module should contain.  So, almost nine months later, four intensive test cycles, three frustrated PMs, and two major design changes later, it’s done.  I plan to share my long strange odyssey through MSI-land in a series of posts over the next month – going from "what tables do I need?" to "what do you mean you want to merge the cabs?"&lt;/p&gt;

&lt;p&gt;And yes, this time the entries will get written and posted, as I’ve scheduled myself an hour a day this week and next to write installments.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=462794" width="1" height="1"&gt;</description></item><item><title>Desiging Datastructures for Longevity</title><link>http://blogs.msdn.com/jonwis/archive/2004/08/16/214995.aspx</link><pubDate>Mon, 16 Aug 2004 07:23:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:214995</guid><dc:creator>jonwis</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jonwis/comments/214995.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jonwis/commentrss.aspx?PostID=214995</wfw:commentRss><description>&lt;p&gt;One of the more problematic areas of long-lived software is the versioning and updating of shared structures. As improvements come to a package, it's inevitable that you'll have to add more fields to a structure. Your structure today may contain two pointers, but tomorrow needs to contain three. If you ship all the bits at the same time, then there's never any problem - everything just gets recompiled and the right sizes are reserved on various stacks, and accesses to those fields don't accidentally overlap existing slots. If you don't ship all at the same time, or you persist data structures out to disk, you're eventually going to run into a problem. Here's an example: &lt;/p&gt; &lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: courier"&gt;struct Foo {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;void *pvSomething;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;void *pvSomethingElse;&lt;br /&gt;}&lt;br /&gt;void Bar(const Foo *pcFoo);&lt;br /&gt;void Zot() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Foo f = { NULL, NULL };&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Bar(&amp;amp;f);&lt;br /&gt;}&lt;br /&gt;&lt;/div&gt; &lt;p&gt;In this v0 of the structure layout, the compiler reseved two pointer-sized things on the stack, and initialized them both to zero. Let's further say that Bar and Zot live in different modules. Now, assume that something comes along and you decided to add two more pointer-sized things to your structure. After rebuilding the module containing Bar, you try testing and notice "random" access violations when reading that new field. What's wrong? With a little debugging of Bar, you notice that the new field is a random value when coming from Zot .. You've just been bitten by structure versioning! &lt;/p&gt; &lt;p&gt;The problem here is that since you didn't recompile Zot with the new structure header, the compiler's baked-in two-pointers structure is smaller than the expected three-pointers structure in the new Bar. That third pointer, when read from Bar, points to the next local past "f" in Zot, often times the return address. How do you solve this problem? What can be done to avoid it in the future?&lt;/p&gt; &lt;p&gt;First off, it's important to recognize that this problem happens with &lt;i&gt;any&lt;/i&gt; layout-dependent object, from classes to structures to vtables. If you change the layout of an object in a way that changes its behavior for existing producers/consumers, you &lt;i&gt;must&lt;/i&gt; provide a versioning mechanism. In the world of vtables, the typical answer is that new functions are added to the end of a vtable. Existing callers continue to call vtable[x], new callers call vtable[y], and as long as &lt;tt&gt;x &amp;lt; y&lt;/tt&gt; all is good. Structures, on the other hand, don't do as well here. Often, structures are embedded in persisted storage, in an array, or other such places that are hard to version. The case of being passed on the stack (or from heap!) is a simplification of this problem.&lt;/p&gt; &lt;h3&gt;Solution 1 - Add a &lt;tt&gt;Version&lt;/tt&gt; field&lt;/h3&gt; &lt;p&gt;This is the simplest, but possibly least useful answer over time. In each structure, have the first field be called &lt;tt&gt;Version&lt;/tt&gt;, or something similar. In your shared headers, create &lt;tt&gt;#define&lt;/tt&gt; values starting at 1 and moving forward as you revision the structure. Generators of structure instances specify the version number that they are generating, and consumers of the structure change how they interpret the structure based on the version found.&lt;/p&gt; &lt;p&gt;As an upside, as long as this version field is consistently maintained, you may completely shift the layout of the structure over time - remove some fields, add some fields, retype some, whatever strikes your fancy. The version number makes sure that it's correctly interpreted by the callees of your functions.&lt;/p&gt; &lt;p&gt;The downside to this approach is that sometime, somewhere, you're going to forget to update the version number. Additionally, you'll have to maintain all those old structure definitions you created over time so that consumers can know what "version 3" really looked like. This approach is attactive because of its simplicity, but the fact that it requires manual intervention to stay correct suggests that there's more to be done.&lt;/p&gt; &lt;h3&gt;Solution 2 - Add a &lt;tt&gt;Size&lt;/tt&gt; field&lt;/h3&gt; &lt;p&gt;Aside from explicit versioning of the structure, you can definitely get a hand in this problem from the compiler itself. To do this, add a field called &lt;tt&gt;Size&lt;/tt&gt;to the top of all structures whose size may shift over time. Initializations of these structures must also change somewhat, as they must also specify the size. You can do this as such:&lt;/p&gt; &lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: courier"&gt;struct Foo {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;unsigned long Size;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;void *pvSomething;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;void *pvSomethingElse;&lt;br /&gt;}; &lt;br /&gt;void Zot() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Foo f = { sizeof(f) };&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;This has the lovely side-effect of zero-initializing the remaining members of the Foo instance. The callee changes as such, using the included handy macros:&lt;/p&gt; &lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: courier"&gt;#define FIELD_OFFSET(TType, Field) ((size_t)(&amp;amp;(((TType*)NULL)-&amp;gt;Field)))&lt;br /&gt;#define FIELD_SIZE(TType, Field) (sizeof(((TType*)NULL)-&amp;gt;Field)) &lt;br /&gt;#define SIZE_THROUGH_FIELD(TType, Field) (FIELD_OFFSET(TType, Field) + FIELD_SIZE(TType, Field)) &lt;br /&gt;void Bar(Foo *pf) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (pf-&amp;gt;Size &amp;gt;= SIZE_THROUGH_FIELD(Foo, pvSomething))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Use pf-&amp;gt;pvSomething */ &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (pf-&amp;gt;Size &amp;gt;= SIZE_THROUGH_FIELD(Foo, pvSomethingElse)) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/* Use pf-&amp;gt;pvSomethingElse */ &lt;br /&gt;} &lt;/div&gt; &lt;p&gt;So when the &lt;tt&gt;Foo&lt;/tt&gt; structure gets another two pointers, the "old" code in &lt;tt&gt;Zot&lt;/tt&gt; continues to set the size to what it knew about (which on 32-bit machines would have been 12), and the "new" code in &lt;tt&gt;Bar&lt;/tt&gt; (which contains extra uses of &lt;tt&gt;SIZE_THROUGH_FIELD&lt;/tt&gt;) does the Right Thing when it detects that the structure passed is only 12 bytes long rather than the 16 or 20 bytes that it might need. In this manner, &lt;tt&gt;Bar&lt;/tt&gt; is able to adjust its behavior when it finds old clients, and even newer clients could behave better if they only have to set certain members.&lt;/p&gt; &lt;h3&gt;Solution 3 - Use a &lt;tt&gt;Flags&lt;/tt&gt; field&lt;/h3&gt; &lt;p&gt;As a hybrid of #1 and #2, adding a field containing flags is another good answer, but it doesn't directly answer the problem of size and layout changes over time. As you add members to a structure, add a &lt;tt&gt;#define&lt;/tt&gt; for the member's validity. When that member is set and available, &lt;tt&gt;or&lt;/tt&gt; it into the Flags member. Consumers of the structure must look at the Flags member before inspecting other members of the structure. As "old" generators won't know about new flags, and don't set those new fields, you're guaranteed that new consumers of old structures won't behave poorly. Here's an example:&lt;/p&gt; &lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: courier"&gt;#define FOO_FLAGS_PVSOMETHING_VALID (0x00000001)&lt;br /&gt;#define FOO_FLAGS_PVSOMETHINGELSE_VALID (0x00000002) &lt;br /&gt;struct Foo { &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;unsigned long Flags;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;void *pvSomething;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;void *pvSomethingElse;&lt;br /&gt;}; &lt;br /&gt;void Zot() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Foo f = { 0 };&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f.pvSomething = /* ? */&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f.Flags |= FOO_FLAGS_PVSOMETHING_VALID;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Bar(&amp;amp;f);&lt;br /&gt;} void Bar(const Foo *pf) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (pf-&amp;gt;Flags &amp;amp; FOO_FLAGS_PVSOMETHING_VALID)&lt;br /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/* Consume pf-&amp;gt;pvSomething */&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (pf-&amp;gt;Flags &amp;amp; FOO_FLAGS_PVSOMETHINGELSE_VALID)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;/* Consume pf-&amp;gt;pvSomethingElse */ &lt;br /&gt;} &lt;/div&gt; &lt;p&gt;When you add another field to &lt;tt&gt;Foo&lt;/tt&gt;, you also create &lt;tt&gt;#define FOO_FLAGS_ANOTHERFIELD_VALID (0x00000004)&lt;/tt&gt;. Bar changes to check that flag before using the &lt;tt&gt;AnotherField&lt;/tt&gt; member. Even though &lt;tt&gt;Zot&lt;/tt&gt; didn't know about &lt;tt&gt;AnotherField&lt;/tt&gt;, since it didn't set that new flag, there's no harm done when an old-sized structure is passed to a new-sized-reading function. The downside with this approach is that you can typically only have 32 fields. And, if you steal flags values for other purposes (changing the meaning of a member when a second flag is set), you've got even less. This method says nothing about the actual layout of the data structure, and requires that new fields are also always added at the end.&lt;/p&gt; &lt;h3&gt;Conclusions&lt;/h3&gt; &lt;p&gt;Versioning structures is hard, but you &lt;i&gt;must&lt;/i&gt; do it if you plan to service independently two components that share an object (structure or otherwise). The easy answer of setting a version member in the structure makes this very easy from the client side, but makes it much harder from the consumer side where each structure definition must be kept around. Adding a "valid through" size field makes the code obviously tied to the size of the structure, but requires some help from the compiler and some macro magic on the consumer's side to interpet the results. Flags indicating which fields are set and valid is probably the easiest, but pushes that "what is valid" work onto the caller - they have to remember to set the correct bit when they want the callee to interpet a field.&lt;/p&gt; &lt;p&gt;What's the optimal solution? Take&amp;nbsp;both #2 and #3 together. Have the first field be the valid size in bytes, because you get that for free from the compiler; saying &lt;tt&gt;Foo f = { sizeof(f) };&lt;/tt&gt; will just be natural after a while. The second field contains flags, indicating which of the possibly-valid fields should be interpreted. Not all data types have a handy "not valid" value like NULL for pointers. How do you indicate that an &lt;tt&gt;int&lt;/tt&gt; is invalid and should not be read? If you use the &lt;tt&gt;Flags&lt;/tt&gt; approach, you can know this - if the flag for the field isn't set, then don't read it.&lt;/p&gt; &lt;p&gt;Future-proofing your data structures should be done as part of the initial design. Once the first set of compiled bits has walked out the door, it's too late to introduce these mechanisms. Design with servicability in mind and you won't have to worry about structure-layout skew.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=214995" width="1" height="1"&gt;</description></item><item><title>Registration-free applications and components</title><link>http://blogs.msdn.com/jonwis/archive/2004/08/07/210476.aspx</link><pubDate>Sat, 07 Aug 2004 12:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:210476</guid><dc:creator>jonwis</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/jonwis/comments/210476.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jonwis/commentrss.aspx?PostID=210476</wfw:commentRss><description>&lt;P&gt;An area of new technology in Windows XP and Windows Server 2003 that didn't get nearly enough coverage is the ability to write applications and components that take full advantage of COM without actually registering anything on the target system.&amp;nbsp; Apps developed with this registration-free mechanism don't require a call to RegSvr32 during install to get their intra-application COM objects set up - no tampering with the registry to get progids listed, no screwing around with INFs and installers - just xcopy (or &amp;#8220;net use&amp;#8221;) and go.&amp;nbsp; Isolated applications don't just do COM, however - they also do self-contained xcopy-deployed applications using resources and MUI.&amp;nbsp; There's an entire book to be written on the topic of side-by-side apps &amp;amp; components that I just don't have time to sit down and pound out given my schedule.&amp;nbsp; However, I'll be posting a series of &amp;#8220;mini chapters&amp;#8221; covering this topic - designs, implementations, strategies, etc. Bear with me, as I'm definitely &lt;EM&gt;not&lt;/EM&gt; an author by training.&amp;nbsp; The first chapter, &amp;#8220;Being Isolated&amp;#8221; should appear in this space in about a week.&lt;/P&gt;
&lt;P&gt;If you happen to be actively developing side-by-side components and applications to take advantage of registration-free COM work (hey, this works from managed code, too...), or you've heard of the topic but want to know more specifics, I'm interested in hearing what areas I should cover.&amp;nbsp; Do you want code samples?&amp;nbsp; Airy academic discussion?&amp;nbsp; Dissection of what CoCreateInstance really does? Something inbetween those three?&amp;nbsp; Let me know by posting feedback.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=210476" width="1" height="1"&gt;</description></item><item><title>Intro</title><link>http://blogs.msdn.com/jonwis/archive/2004/07/09/178946.aspx</link><pubDate>Sat, 10 Jul 2004 04:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:178946</guid><dc:creator>jonwis</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/jonwis/comments/178946.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jonwis/commentrss.aspx?PostID=178946</wfw:commentRss><description>&lt;p&gt;Howdy - I'm a software design engineer at Microsoft, in the Windows core technologies group. My work involves isolating applications, components, and the operating system from each other. I don't have much original or interesting to say, but by gum - I'll say it here.  Like many of my generation, I grew up using computers.  I won't enumerate them all, but the list starts with an Atari 800 XL and hasn' ended yet.&lt;/p&gt;

&lt;p&gt;The unfortunate state of the world today is that once an installer exits (either for an operating system or a software package), the resulting state of the world is completely unknown and undefined.  Sure, judicious use of regmon and filemon will let you &lt;i&gt;guess&lt;/i&gt; at what those installers did, and &lt;i&gt;maybe&lt;/i&gt; you'll be able to uninstall. Compound this with the large set of installations required to get a running system, differing versions of shared components, and even more fun around points of extensibility, and you end up with a morass of crap - the only hope is to flatten and reinstall the system.&lt;/p&gt;

&lt;p&gt;So what happens when we uninstall?  Well, generally, the uninstaller has a list of objects that it installed, and some sort of reference tracking to know when stuff should go away.  The installer decrements those references, and whacks the stuff that should be deleted.  Too bad the installer for FoobleSoft's Bongoblam III's installer forgot to increment the reference on the shared component ponkfoo.dll - it hit zero, so the installer does away with it. Practical upshot? Next time you run Bongoblam III, it crashes during binding static imports.  Well crap.&lt;/p&gt;

&lt;p&gt;How do you help this?  You could move to a single installer (MSI), so that only that installer knows how to do the right thing. You could move to a world where the system deeply understands the requirements between DLLs and EXEs (or images in general) and refuses to allow deletion of images if any references on them are alive. Both of thse, when done correctly, fix the problem of shared components accidentally disappearing out from under you.  That's a good step forward...&lt;/p&gt;

&lt;p&gt;But, it's not sufficient.  That package you uninstalled - call it Barbaszot - and Bongoblam both wanted to be the .XYZ shell extension handler.  The installer's simple logic noticed that Barbaszot had written to &lt;tt&gt;HKCU\.xyz&lt;/tt&gt;, so it deletes everything that was updated.  Now when the shell wants to activate, it &lt;i&gt;could&lt;/i&gt; find Bongoblam III, but it can't!  The .xyz handler metadata is gone, and the shell pops up a "what do you want to do with this file" dialog.&lt;/p&gt;

&lt;p&gt;Smarter installers - like MSI - can help with this as well, by noticing the state of the world before values are written or updated in the registry.  Maybe when you uninstall Barbaszot, it knows to just rewrite the old values, and suddenly Bongoblam III is now the handler again.  That's groovy.  But wait - what if Bongoblam was installed &lt;i&gt;after&lt;/i&gt; Barbaszot, but Barbaszot stole back the extension handler (this horrible practice started about five minutes after the first alternate handler for .whatever files was created...)?  The installer in all its logic says "well there was no value before" and helpfully deletes the keys.  Now Bongoblam III is out in the cold again.&lt;/p&gt;

&lt;p&gt;What can we do about all this?  My work involves doing completely self-described simple and nondestructive operations to a running system.  In my world, when the shell was looking for an .xyz handler but after Barbaszot had been removed, it would look through a data-driven list of applications that had declared themselves to be .xyz handlers.  Maybe it would have prompted the user, maybe it would have picked the last-used, maybe it would have picked the first random one. In any case, removal of Barbaszot would not have destroyed the list of possible choices&lt;/p&gt;

&lt;p&gt;It would appear I'm rambling again.  I'll have more later - less ranting on how bad software is now, and more things I've discovered while working here.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=178946" width="1" height="1"&gt;</description></item></channel></rss>