<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-10455146818084740</id><updated>2011-07-07T16:10:26.459-07:00</updated><category term='C#'/><category term='C++'/><category term='Inter-Process Communications'/><category term='Useful Tools'/><category term='Tips/Tricks'/><category term='Communication Protocols'/><category term='Website Optimizations'/><category term='C'/><category term='OOP'/><category term='AJAX'/><category term='DLL'/><category term='Windows Services'/><category term='MS SQL'/><category term='JavaScript'/><category term='Multithreading'/><category term='Registry'/><category term='Design Patterns'/><category term='ASP.NET'/><title type='text'>Programming Features</title><subtitle type='html'>Tips tricks and features from the real world programming.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>68</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-1055632140896404554</id><published>2009-07-30T00:22:00.000-07:00</published><updated>2009-07-30T00:25:36.086-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>ArrayList to object[] and versa</title><content type='html'>A simple way to perform the conversion:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;object&lt;/span&gt;[] tmp = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt;[3]{1,2,3};&lt;/pre&gt;&lt;br /&gt;&lt;pre style="margin: 0px;"&gt; &lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color:green;"&gt;// to ArrayList&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArrayList&lt;/span&gt; arrayList = &lt;span style="color: rgb(43, 145, 175);"&gt;ArrayList&lt;/span&gt;.Adapter(tmp);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color:green;"&gt;// to array&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;object&lt;/span&gt;[] tmp1 = arrayList.ToArray();&lt;/pre&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-1055632140896404554?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/1055632140896404554/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/07/arraylist-to-object-and-versa.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/1055632140896404554'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/1055632140896404554'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/07/arraylist-to-object-and-versa.html' title='ArrayList to object[] and versa'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-7131520559303335307</id><published>2009-07-24T07:28:00.000-07:00</published><updated>2009-07-24T07:35:47.761-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Useful Tools'/><category scheme='http://www.blogger.com/atom/ns#' term='Website Optimizations'/><title type='text'>The CSS Sprites Creator</title><content type='html'>In order to decrease the traffic and website loading time you can use CSS sprites.&lt;br /&gt;The &lt;a style="color: rgb(51, 51, 255);" href="http://www.floweringmind.com/sprite-creator/"&gt;Sprite Creator 1.0&lt;/a&gt; is a nice tool for upload an image and select areas of the image to create the css for that sprite.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-7131520559303335307?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/7131520559303335307/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/07/css-sprites-creator.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7131520559303335307'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7131520559303335307'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/07/css-sprites-creator.html' title='The CSS Sprites Creator'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-286444556369215108</id><published>2009-06-28T10:54:00.000-07:00</published><updated>2009-06-30T00:21:01.104-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>The Singleton pattern</title><content type='html'>The&lt;a href="http://en.wikipedia.org/wiki/Singleton_pattern"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;span style="font-weight: bold;"&gt;singleton&lt;/span&gt;&lt;/span&gt;&lt;/a&gt; pattern is a design pattern that is used to restrict instantiation of a class to one object.&lt;br /&gt;This is useful when exactly one object is needed to coordinate actions across the system.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_bgdRGHjn5sE/Ske-n37e7DI/AAAAAAAAAEU/IRjYHkILzwA/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 222px; height: 227px;" src="http://3.bp.blogspot.com/_bgdRGHjn5sE/Ske-n37e7DI/AAAAAAAAAEU/IRjYHkILzwA/s320/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5352456274390740018" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;C++ implementations:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Static singleton:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;"iostream"&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;template&lt;/span&gt; &amp;lt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; T&amp;gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; Singleton&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;static&lt;/span&gt; T _instance;   &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;   ~Singleton(){ }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    Singleton(){ }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    T&amp;amp; GetInstance() {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; _instance;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    Singleton(Singleton&amp;amp;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    Singleton&amp;amp; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;=(Singleton&amp;amp;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;template&lt;/span&gt; &amp;lt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; T&amp;gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;T Singleton&amp;lt;T&amp;gt;::_instance;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; MyData&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;void&lt;/span&gt; Execute(){std::cout&amp;lt;&amp;lt;&lt;span style="color: rgb(163, 21, 21);"&gt;"Execute..."&lt;/span&gt;&amp;lt;&amp;lt;std::endl;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    MyData(){std::cout&amp;lt;&amp;lt;&lt;span style="color: rgb(163, 21, 21);"&gt;"Constructor..."&lt;/span&gt;&amp;lt;&amp;lt;std::endl;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;void&lt;/span&gt; main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    Singleton&amp;lt;MyData&amp;gt; singleton1;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    singleton1.GetInstance().Execute();&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    Singleton&amp;lt;MyData&amp;gt; singleton2;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    singleton2.GetInstance().Execute();&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    Singleton&amp;lt;&lt;span style="color:blue;"&gt;int&lt;/span&gt;&amp;gt; singleton3;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    singleton3.GetInstance() = 5;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    std::cout&amp;lt;&amp;lt;singleton3.GetInstance()&amp;lt;&amp;lt;std::endl;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Static function singleton:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;template&lt;/span&gt; &amp;lt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; T&amp;gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; Singleton&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;   ~Singleton(){ }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    Singleton(){ }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    T&amp;amp; GetInstance() {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;static&lt;/span&gt; T instance;   &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; instance;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    Singleton(Singleton&amp;amp;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    Singleton&amp;amp; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;=(Singleton&amp;amp;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Dynamic singleton:&lt;br /&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;   Unlike the static singletons, the dynamic is not thread safe.&lt;br /&gt;You can use critical section as synchronization mechanism.&lt;/span&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;template&lt;/span&gt; &amp;lt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; T&amp;gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; Singleton&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;static&lt;/span&gt; T* _instance;   &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;   ~Singleton(){ }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    Singleton(){ }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    T&amp;amp; GetInstance() {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;if&lt;/span&gt;(!_instance)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            _instance = &lt;span style="color:blue;"&gt;new&lt;/span&gt; T();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; *_instance;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    Singleton(Singleton&amp;amp;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    Singleton&amp;amp; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;=(Singleton&amp;amp;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;template&lt;/span&gt; &amp;lt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; T&amp;gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;T* Singleton&amp;lt;T&amp;gt;::_instance=0;&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;C# implementation:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Singleton&lt;/span&gt;&amp;lt;T&amp;gt; &lt;span style="color:blue;"&gt;where&lt;/span&gt; T : &lt;span style="color:blue;"&gt;class&lt;/span&gt;, &lt;span style="color:blue;"&gt;new&lt;/span&gt;()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; T _instance = &lt;span style="color:blue;"&gt;null&lt;/span&gt;;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;static&lt;/span&gt; Singleton()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                _instance = &lt;span style="color:blue;"&gt;new&lt;/span&gt; T();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; T Instance&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;get&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    &lt;span style="color:blue;"&gt;return&lt;/span&gt; _instance;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;MyData&lt;/span&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; Execute() { &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;"Execute..."&lt;/span&gt;); }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        [&lt;span style="color: rgb(43, 145, 175);"&gt;STAThread&lt;/span&gt;]&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; Main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;Singleton&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;MyData&lt;/span&gt;&amp;gt;.Instance.Execute();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;The static constructor for a class executes at most once in a given application domain.&lt;br /&gt;It's used to initialize any static data, or to perform a particular action that needs performed once only and it is called automatically before the first instance is created or any static members are referenced.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Double-Checked Locking Singleton (lazy initialization):&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;    internal&lt;/span&gt; &lt;span style="color:blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Singleton&lt;/span&gt;&amp;lt;T&amp;gt; &lt;span style="color:blue;"&gt;where&lt;/span&gt; T : &lt;span style="color:blue;"&gt;class&lt;/span&gt;, &lt;span style="color:blue;"&gt;new&lt;/span&gt;()&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;    {&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; T _instance = &lt;span style="color:blue;"&gt;null&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; _syncObj = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt;();&lt;/pre&gt;&lt;br /&gt;&lt;pre style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; Singleton()&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;        {&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;            &lt;span style="color:green;"&gt;// your implementation&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;pre style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; T Instance&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;        {&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;get&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;            {&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;if&lt;/span&gt; (_instance == &lt;span style="color:blue;"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;                {&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;                    &lt;span style="color:blue;"&gt;lock&lt;/span&gt; (_syncObj)&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;                    {&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;                        &lt;span style="color:blue;"&gt;if&lt;/span&gt; (_instance != &lt;span style="color:blue;"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;                            &lt;span style="color:blue;"&gt;return&lt;/span&gt; _instance;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;                        _instance = &lt;span style="color:blue;"&gt;new&lt;/span&gt; T();&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;                    }&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;                }&lt;/pre&gt;&lt;br /&gt;&lt;pre style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;return&lt;/span&gt; _instance;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;            }&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;        }&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;    }&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt; &lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Program&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;    {&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; Main(&lt;span style="color:blue;"&gt;string&lt;/span&gt;[] args)&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;        {&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;Singleton&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;StringBuilder&lt;/span&gt;&amp;gt;.Instance.Append(&lt;span style="color: rgb(163, 21, 21);"&gt;"Some text"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;        }&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;    }&lt;/pre&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;The double-checked locking optimization is designed to reduce the overhead of acquiring a lock by first testing the locking criterion.&lt;br /&gt;Lazy initialization avoids initializing a value until the first time it is accessed.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-286444556369215108?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/286444556369215108/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/06/singleton-pattern.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/286444556369215108'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/286444556369215108'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/06/singleton-pattern.html' title='The Singleton pattern'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_bgdRGHjn5sE/Ske-n37e7DI/AAAAAAAAAEU/IRjYHkILzwA/s72-c/untitled.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-3244277638336654836</id><published>2009-06-20T07:24:00.000-07:00</published><updated>2009-06-20T07:39:48.419-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>The C# preprocessor directives</title><content type='html'>Unlike the C/C++ the C# compiler does not have a separate preprocessor.&lt;br /&gt;Basicaly preprocessor directives are instructions to the C# compiler to perform some action during compilation.&lt;br /&gt;&lt;br /&gt;&lt;ul type="disc"&gt; &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/4y6tbswk%28VS.71%29.aspx" id="ctl00_contentContainer_ctl01"&gt;#if&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/87a56b46%28VS.71%29.aspx" id="ctl00_contentContainer_ctl02"&gt;#else&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/88td0y52%28VS.71%29.aspx" id="ctl00_contentContainer_ctl03"&gt;#elif&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/hyx43has%28VS.71%29.aspx" id="ctl00_contentContainer_ctl04"&gt;#endif&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/yt3yck0x%28VS.71%29.aspx" id="ctl00_contentContainer_ctl05"&gt;#define&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/wkxst87d%28VS.71%29.aspx" id="ctl00_contentContainer_ctl06"&gt;#undef&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/963th5x3%28VS.71%29.aspx" id="ctl00_contentContainer_ctl07"&gt;#warning&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/x5hedts0%28VS.71%29.aspx" id="ctl00_contentContainer_ctl08"&gt;#error&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/34dk387t%28VS.71%29.aspx" id="ctl00_contentContainer_ctl09"&gt;#line&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/9a1ybwek%28VS.71%29.aspx" id="ctl00_contentContainer_ctl10"&gt;#region&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/67w7t67f%28VS.71%29.aspx" id="ctl00_contentContainer_ctl11"&gt;#endregion&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;#if, #else, #elif  and #endif&lt;/span&gt; - commonly used to evaluate conditional code sections:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;#define DEBUG&lt;br /&gt;using System;&lt;br /&gt;public class MyClass&lt;br /&gt;{&lt;br /&gt;public static void Main()&lt;br /&gt;{&lt;br /&gt; #if (DEBUG)&lt;br /&gt;    Console.WriteLine("DEBUG is defined");&lt;br /&gt; #elif (VC_V7)&lt;br /&gt;    Console.WriteLine("VC_V7 is defined");&lt;br /&gt; #else&lt;br /&gt;    Console.WriteLine("DEBUG and VC_V7 are not defined");&lt;br /&gt; #endif&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;#define&lt;/span&gt; lets you define a symbol, such that, by using the symbol as the expression passed to the #if directive, the expression will evaluate to true.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;#undef&lt;/span&gt; lets you undefine a symbol, such that, by using the symbol as the expression in a #if directive, the expression will evaluate to false.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;#warning/#error&lt;/span&gt; lets you generate a warning/error from a specific location in your code:&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;#define DEBUG&lt;br /&gt;public class MyClass&lt;br /&gt;{&lt;br /&gt;public static void Main()&lt;br /&gt;{&lt;br /&gt;   #if DEBUG&lt;br /&gt;   #warning DEBUG is defined&lt;br /&gt;   #endif&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;#line&lt;/span&gt; lets you modify the compiler's line number and (optionally) the file name output for errors and warnings.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(255, 102, 0);"&gt;#region&lt;/span&gt; lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor.&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;#region MyClass definition&lt;br /&gt;public class MyClass&lt;br /&gt;{&lt;br /&gt;  public static void Main()&lt;br /&gt;  {&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;#endregion&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-3244277638336654836?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/3244277638336654836/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/06/c-preprocessor-directives.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/3244277638336654836'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/3244277638336654836'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/06/c-preprocessor-directives.html' title='The C# preprocessor directives'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-7890868298588800005</id><published>2009-06-01T23:12:00.000-07:00</published><updated>2009-06-01T23:37:45.589-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Stopwatch – a way to measure elapsed time</title><content type='html'>If you need to measure elapsed time of your function or code section you can use Stopwatch object.&lt;br /&gt;&lt;br /&gt;According to &lt;a href="http://msdn.microsoft.com/"&gt;MSDN&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;A &lt;a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx"&gt;Stopwatch&lt;/a&gt; instance can measure elapsed time for one interval, or the total of elapsed time across multiple intervals. In a typical Stopwatch scenario, you call the Start method, then eventually call the Stop method, and then you check elapsed time using the Elapsed property.&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;pre style="margin: 0px;"&gt;     System.Diagnostics.&lt;span style="color: rgb(43, 145, 175);"&gt;Stopwatch&lt;/span&gt; watch = &lt;span style="color:blue;"&gt;new&lt;/span&gt; System.Diagnostics.&lt;span style="color: rgb(43, 145, 175);"&gt;Stopwatch&lt;/span&gt;();&lt;/pre&gt;&lt;br /&gt;&lt;pre style="margin: 0px;"&gt;     watch.Start();&lt;/pre&gt;&lt;br /&gt;&lt;pre style="margin: 0px;"&gt;     &lt;span style="color:green;"&gt;// Your Implementation&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre style="margin: 0px;"&gt;     watch.Stop();&lt;/pre&gt;&lt;br /&gt;&lt;pre style="margin: 0px;"&gt;     &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(watch.Elapsed.ToString());&lt;/pre&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-7890868298588800005?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/7890868298588800005/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/06/stopwatch-way-to-measure-elapsed-time.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7890868298588800005'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7890868298588800005'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/06/stopwatch-way-to-measure-elapsed-time.html' title='Stopwatch – a way to measure elapsed time'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-4382359266009572510</id><published>2009-05-31T09:03:00.000-07:00</published><updated>2009-05-31T09:09:43.789-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>File.ReadAllText and File.WriteAllText</title><content type='html'>If you are looking for an easy way to work with files the: File.ReadAllText and File.WriteAllText are the answer.&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms143368.aspx"&gt;&lt;br /&gt;File.ReadAllText&lt;/a&gt; Method ppens a text file, reads all lines of the file, and then closes the file.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms143375.aspx"&gt;File.WriteAllText&lt;/a&gt; Method creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-4382359266009572510?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/4382359266009572510/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/05/filereadalltext-and-filewritealltext.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/4382359266009572510'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/4382359266009572510'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/05/filereadalltext-and-filewritealltext.html' title='File.ReadAllText and File.WriteAllText'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-8301977683675127798</id><published>2009-05-09T12:31:00.000-07:00</published><updated>2009-05-09T22:33:59.077-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Inter-Process Communications'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='Multithreading'/><title type='text'>Shared Memory</title><content type='html'>&lt;a style="color: rgb(51, 51, 255); font-weight: bold;" href="http://en.wikipedia.org/wiki/Shared_memory"&gt;Shared Memory&lt;/a&gt; is an efficeint way of exchanging data between programs running at the same time. One process will create an area in RAM which other processes can access.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_bgdRGHjn5sE/SgZnH21h6jI/AAAAAAAAAEM/GLSUEefK8ZQ/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 345px;" src="http://1.bp.blogspot.com/_bgdRGHjn5sE/SgZnH21h6jI/AAAAAAAAAEM/GLSUEefK8ZQ/s400/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5334064193343711794" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;WinAPI remarks:&lt;br /&gt;After a Shared Memory object is created, the size of the file must not exceed the size of the file Shared Memory; if it does, not all of the file contents are available for sharing.&lt;br /&gt;A shared file mapping object will not be destroyed until all processes that use it close their handles to it by using the &lt;a style="font-weight: bold; color: rgb(51, 51, 255);" href="http://msdn.microsoft.com/en-us/library/ms724211%28VS.85%29.aspx"&gt;CloseHandle&lt;/a&gt; function.&lt;br /&gt;&lt;br /&gt;A simple example of Writer/Reader processes.&lt;br /&gt;First process creates a Shared Memory; the second reads messages from it.&lt;br /&gt;&lt;br /&gt;SharedMemoryHandler.h&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; SharedMemoryHandler&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    SharedMemoryHandler(&lt;span style="color:blue;"&gt;void&lt;/span&gt;* pObj, &lt;span style="color:blue;"&gt;int&lt;/span&gt; length);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ~SharedMemoryHandler(&lt;span style="color:blue;"&gt;void&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;bool&lt;/span&gt; Read();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;bool&lt;/span&gt; Write();&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;bool&lt;/span&gt; Load();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;bool&lt;/span&gt; Unload();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    SharedMemoryHandler(&lt;span style="color:blue;"&gt;void&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    SharedMemoryHandler&amp;amp; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;=(SharedMemoryHandler&amp;amp;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    PVOID        m_pObj;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;int&lt;/span&gt;          m_length;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    PVOID        m_pBuf;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    HANDLE        m_hMutex;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    HANDLE        m_hMapFile;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;SharedMemoryHandler.cpp&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;"SharedMemoryHandler.h"&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;lt;tchar.h&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:green;"&gt;//___________ create unique mutex name ___________&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;static&lt;/span&gt; LPWSTR mutexName=_T(&lt;span style="color: rgb(163, 21, 21);"&gt;"SharedMemoryHandler_Mutex"&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;static&lt;/span&gt; LPWSTR sharedMemoryName=_T(&lt;span style="color: rgb(163, 21, 21);"&gt;"SharedMemoryHandler_SharedMemory"&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;SharedMemoryHandler::SharedMemoryHandler(&lt;span style="color:blue;"&gt;void&lt;/span&gt;* pObj, &lt;span style="color:blue;"&gt;int&lt;/span&gt; length)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;:m_hMutex(0), m_hMapFile(0), m_pBuf(0), m_pObj(pObj), m_length(length)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    Load();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;SharedMemoryHandler::~SharedMemoryHandler(&lt;span style="color:blue;"&gt;void&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    Unload();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    CloseHandle( m_hMutex );&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    CloseHandle( m_hMapFile );&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    m_length = 0;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    m_hMutex = m_hMapFile = m_pBuf = m_pObj = 0;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;bool&lt;/span&gt; SharedMemoryHandler::Load()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//__________ lock with mutex ____________&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;((m_hMutex = CreateMutex(NULL, FALSE, mutexName))==0)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//_____ wait till someone unlock it ___&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WaitForSingleObject( m_hMutex, INFINITE );&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;// _____________ create shared memory __________&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    m_hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, m_length, sharedMemoryName); &lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt; (m_hMapFile == 0 || m_hMapFile == INVALID_HANDLE_VALUE) &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    { &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:green;"&gt;//_____ unlock _____&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        ReleaseMutex(m_hMutex);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//______________ load shared memory ________________&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    m_pBuf = MapViewOfFile(m_hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, m_length);   &lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt; (m_pBuf == 0) &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    { &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:green;"&gt;//_____ unlock _____&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        ReleaseMutex(m_hMutex);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//_____ unlock _____&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ReleaseMutex(m_hMutex);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;bool&lt;/span&gt; SharedMemoryHandler::Read()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//__________ lock with mutex ____________&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;((m_hMutex = CreateMutex(NULL, FALSE, mutexName))==0)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//_____ wait till someone unlock it ___&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WaitForSingleObject( m_hMutex, INFINITE );&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ZeroMemory( m_pObj, m_length );&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    CopyMemory(m_pObj, m_pBuf, m_length);&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//_____ unlock _____&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ReleaseMutex(m_hMutex);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;true;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;bool&lt;/span&gt; SharedMemoryHandler::Write()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//__________ lock with mutex ____________&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;((m_hMutex = CreateMutex(NULL, FALSE, mutexName))==0)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//_____ wait till someone unlock it ___&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WaitForSingleObject( m_hMutex, INFINITE );&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    ZeroMemory( m_pBuf, m_length );&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    CopyMemory(m_pBuf, m_pObj, m_length);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//_____ unlock _____&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ReleaseMutex(m_hMutex);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;bool&lt;/span&gt; SharedMemoryHandler::Unload()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//_______________ get mutex handler ________________&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;((m_hMutex=CreateMutex(NULL, FALSE, mutexName))==0)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//____ wait till someone unlock it __&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WaitForSingleObject( m_hMutex, INFINITE );&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;// ____ destroy shared memory ______&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!UnmapViewOfFile(m_hMapFile)) &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    { &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:green;"&gt;//_____ unlock _____&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        ReleaseMutex(m_hMutex);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//_____ unlock _____&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ReleaseMutex(m_hMutex);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;First process:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;"stdafx.h"&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;lt;conio.h&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;"SharedMemoryHandler.h"&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;struct&lt;/span&gt; Message&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;char&lt;/span&gt; msg[256];&lt;/p&gt;&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;int&lt;/span&gt; _tmain()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    Message msg = {0};&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    strcpy(msg.msg, &lt;span style="color: rgb(163, 21, 21);"&gt;"Writer process message!"&lt;/span&gt;);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    SharedMemoryHandler smh(&amp;amp;msg, &lt;span style="color:blue;"&gt;sizeof&lt;/span&gt;(msg));&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    smh.Write();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    _getch();&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; 0;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Second process:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;"stdafx.h"&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;"SharedMemoryHandler.h"&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;struct&lt;/span&gt; Message&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;char&lt;/span&gt; msg[256];&lt;/p&gt;&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;int&lt;/span&gt; _tmain()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    Message msg = {0};&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    SharedMemoryHandler smh(&amp;amp;msg, &lt;span style="color:blue;"&gt;sizeof&lt;/span&gt;(msg));&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    smh.Read();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    printf(&lt;span style="color: rgb(163, 21, 21);"&gt;"%s\n"&lt;/span&gt;, msg.msg);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; 0;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-8301977683675127798?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/8301977683675127798/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/05/shared-memory.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/8301977683675127798'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/8301977683675127798'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/05/shared-memory.html' title='Shared Memory'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_bgdRGHjn5sE/SgZnH21h6jI/AAAAAAAAAEM/GLSUEefK8ZQ/s72-c/untitled.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-7434897266095249445</id><published>2009-05-09T03:12:00.000-07:00</published><updated>2009-05-09T03:22:04.029-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='Registry'/><title type='text'>How to startup your application automatically when a Window starts</title><content type='html'>One way to execute your process each time a Windows starts is to add your application's path to the registry.&lt;br /&gt;When Windows starts it's executes all applications which registered at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run path.&lt;br /&gt;&lt;br /&gt;You can create installer for your application and create relevant registry key, or you can do this from code:&lt;br /&gt;&lt;br /&gt;Main application:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;int&lt;/span&gt; main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;wchar_t&lt;/span&gt; appKey[] = TEXT(&lt;span style="color:maroon;"&gt;"My_StartUp_Application"&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;wchar_t&lt;/span&gt; appPath[] = TEXT(&lt;span style="color:maroon;"&gt;"D:\\StartUpApplication\\debug\\StartUpApplication.exe"&lt;/span&gt;);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    StartUpApplication startUpApp;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    startUpApp.Set(appKey, appPath);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;  &lt;span style="color:blue;"&gt;return&lt;/span&gt; 0;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;StartUpApplication.h&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; StartUpApplication&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    StartUpApplication(&lt;span style="color:blue;"&gt;void&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ~StartUpApplication(&lt;span style="color:blue;"&gt;void&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;bool&lt;/span&gt; UnSet(&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;wchar_t&lt;/span&gt;* key);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;bool&lt;/span&gt; Set(&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;wchar_t&lt;/span&gt;* key, &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;wchar_t&lt;/span&gt;* appPath);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;bool&lt;/span&gt; GetAppPath(&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;wchar_t&lt;/span&gt;* key, &lt;span style="color:blue;"&gt;wchar_t&lt;/span&gt;* appPath);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    StartUpApplication(StartUpApplication&amp;amp;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    StartUpApplication&amp;amp; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;=(StartUpApplication&amp;amp;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;StartUpApplication.cpp&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color:maroon;"&gt;"StartUpApplication.h"&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color:maroon;"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;wchar_t&lt;/span&gt;* _startupPath = __TEXT(&lt;span style="color:maroon;"&gt;"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"&lt;/span&gt;);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;StartUpApplication::StartUpApplication()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt;StartUpApplication::~StartUpApplication(&lt;span style="color:blue;"&gt;void&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;bool&lt;/span&gt; StartUpApplication::Set(&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;wchar_t&lt;/span&gt;* key, &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;wchar_t&lt;/span&gt;* appPath)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;(!key || !appPath)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    HKEY hKey;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;(ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, _startupPath, 0, KEY_ALL_ACCESS, &amp;amp;hKey))&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;long&lt;/span&gt; retVal = RegSetValueEx(hKey, key, 0, REG_SZ, (BYTE*)appPath, (wcslen(appPath)+1)*&lt;span style="color:blue;"&gt;sizeof&lt;/span&gt;(TCHAR));&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; ( (ERROR_SUCCESS == retVal) &amp;amp;&amp;amp; (ERROR_SUCCESS == RegCloseKey(hKey)) );&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;bool&lt;/span&gt; StartUpApplication::UnSet(&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;wchar_t&lt;/span&gt;* key)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    HKEY hKey;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt; (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, _startupPath, 0, KEY_ALL_ACCESS, &amp;amp;hKey))&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; ( (ERROR_SUCCESS == RegDeleteValue(hKey, (LPCWSTR)key)) &amp;amp;&amp;amp; (ERROR_SUCCESS == RegCloseKey(hKey)) );&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;bool&lt;/span&gt; StartUpApplication::GetAppPath(&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;wchar_t&lt;/span&gt;* key, &lt;span style="color:blue;"&gt;wchar_t&lt;/span&gt;* appPath)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    HKEY hKey;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt; (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _startupPath, 0, KEY_READ, &amp;amp;hKey) != ERROR_SUCCESS)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    DWORD dwType = REG_SZ, dwSize = MAX_PATH;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;long&lt;/span&gt; retVal = RegQueryValueEx(hKey, (LPCWSTR)key, NULL, &amp;amp;dwType, (LPBYTE)appPath, &amp;amp;dwSize);&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; ( (ERROR_SUCCESS == retVal) &amp;amp;&amp;amp; (ERROR_SUCCESS == RegCloseKey(hKey)) );&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-7434897266095249445?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/7434897266095249445/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/05/how-to-startup-your-application.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7434897266095249445'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7434897266095249445'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/05/how-to-startup-your-application.html' title='How to startup your application automatically when a Window starts'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-6975534907566491233</id><published>2009-05-09T02:02:00.000-07:00</published><updated>2009-05-09T02:05:45.935-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>How to receive OS notifications about windows shutdown/logoff</title><content type='html'>To create a process which must detect windows shutdown or log-off actions to perform necessary clean-up operations you can use many features.&lt;br /&gt;&lt;br /&gt;One of them is to create Windows Service.&lt;br /&gt;Another way is to subscribe to the system events notifications.&lt;br /&gt;&lt;br /&gt;WinAPI provides a &lt;a href="http://msdn.microsoft.com/en-us/library/ms686016%28VS.85%29.aspx"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SetConsoleCtrlHandler &lt;/span&gt;&lt;/a&gt;function which able to add a callback for the calling process.&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color:maroon;"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;HANDLE hEvent;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;BOOL WINAPI ShutDownHandler(DWORD dwCtrlType)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;switch&lt;/span&gt;( dwCtrlType )&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;case&lt;/span&gt; CTRL_SHUTDOWN_EVENT:&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;case&lt;/span&gt; CTRL_LOGOFF_EVENT: &lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;case&lt;/span&gt; CTRL_CLOSE_EVENT: &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            SetEvent(hEvent);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;return&lt;/span&gt; TRUE;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;default&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;return&lt;/span&gt; FALSE;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;void&lt;/span&gt; main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    SetConsoleCtrlHandler(ShutDownHandler, &lt;span style="color:blue;"&gt;true&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;// your implementation&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WaitForSingleObject(hEvent,INFINITE); &lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;// your clean-up&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    CloseHandle (hEvent);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-6975534907566491233?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/6975534907566491233/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/05/how-to-receive-os-notifications-about.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6975534907566491233'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6975534907566491233'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/05/how-to-receive-os-notifications-about.html' title='How to receive OS notifications about windows shutdown/logoff'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-1633698066133927269</id><published>2009-05-08T09:37:00.000-07:00</published><updated>2009-05-09T02:06:26.755-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Force application not to appear at taskbar</title><content type='html'>&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color:maroon;"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;int&lt;/span&gt; _tmain(&lt;span style="color:blue;"&gt;int&lt;/span&gt; argc, _TCHAR* argv[])&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    HWND hwnd;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;wchar_t&lt;/span&gt; pszNewWindowTitle[MAX_PATH]={0};&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    GetConsoleTitle(pszNewWindowTitle, &lt;span style="color:blue;"&gt;sizeof&lt;/span&gt;(pszNewWindowTitle));&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    hwnd = FindWindow(NULL, pszNewWindowTitle);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;(hwnd)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        ShowWindow(hwnd, SW_HIDE);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;// Your Code..&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; 0;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-1633698066133927269?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/1633698066133927269/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/05/force-application-not-to-appear-at.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/1633698066133927269'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/1633698066133927269'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/05/force-application-not-to-appear-at.html' title='Force application not to appear at taskbar'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-8197643732073232846</id><published>2009-05-03T12:22:00.000-07:00</published><updated>2009-05-03T12:27:31.311-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='Multithreading'/><title type='text'>How to force thread to perform context switch</title><content type='html'>&lt;span style="font-weight: bold;"&gt;What is a &lt;a style="color: rgb(51, 51, 255);" href="http://en.wikipedia.org/wiki/Context_switch"&gt;context switch&lt;/a&gt;?&lt;/span&gt;&lt;br /&gt;A context switch is an action performed by operating systems to switch from one process/thread to another.&lt;br /&gt;Basically, during application execution the operating system provides a CPU time for each thread.&lt;br /&gt;While thread is executed its state is loaded into CPU registers.&lt;br /&gt;By the time comes to execute a new thread, current's thread data is stored back in process's stack and a new process's state is loaded into CPU registers.&lt;br /&gt;&lt;br /&gt;You can cause context switch by calling:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;C/C++ &lt;/span&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;ul&gt;&lt;li&gt;Sleep(0);&lt;/li&gt;&lt;/ul&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;C#&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;System.Threading.Thread.Sleep(0);&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-8197643732073232846?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/8197643732073232846/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/05/how-to-force-thread-to-perform-context.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/8197643732073232846'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/8197643732073232846'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/05/how-to-force-thread-to-perform-context.html' title='How to force thread to perform context switch'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-2058550131830966889</id><published>2009-05-02T11:03:00.000-07:00</published><updated>2009-05-02T11:19:42.600-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><title type='text'>How to prevent current event's propagation</title><content type='html'>Think about next scenario:&lt;br /&gt;&lt;br /&gt;Create a table.&lt;br /&gt;Each row shows information on click.&lt;br /&gt;Each column shows information on click.&lt;br /&gt;Each column can contain additional clickable tags.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;The implementation:&lt;/span&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;table&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;tr&lt;/span&gt; &lt;span style="color:red;"&gt;onclick&lt;/span&gt;&lt;span style="color:blue;"&gt;="alert('row');"&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;td&lt;/span&gt; &lt;span style="color:red;"&gt;onclick&lt;/span&gt;&lt;span style="color:blue;"&gt;="alert('td');"&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;span&lt;/span&gt; &lt;span style="color:red;"&gt;onclick&lt;/span&gt;&lt;span style="color:blue;"&gt;="alert('td');"&lt;/span&gt; &lt;span style="color:red;"&gt;style&lt;/span&gt;&lt;span style="color:blue;"&gt;="color:blue;"&lt;/span&gt; &lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    Click here!&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;span&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:red;"&gt;&amp;amp;nbsp;&lt;/span&gt; td section&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;td&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;tr&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;table&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;That is because when method is called by any EventListener the event will cease propagating through the tree.&lt;br /&gt;&lt;br /&gt;To prevent the propagation after “Click here!” text click use following code:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 9pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;table&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;tr&lt;/span&gt; &lt;span style="color:red;"&gt;onclick&lt;/span&gt;&lt;span style="color:blue;"&gt;="alert('row');"&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;td&lt;/span&gt; &lt;span style="color:red;"&gt;onclick&lt;/span&gt;&lt;span style="color:blue;"&gt;="alert('td');"&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;span&lt;/span&gt; &lt;span style="color:red;"&gt;onclick&lt;/span&gt;&lt;span style="color:blue;"&gt;="spanClick(event);"&lt;/span&gt; &lt;span style="color:red;"&gt;style&lt;/span&gt;&lt;span style="color:blue;"&gt;="color:blue;"&lt;/span&gt; &lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    Click here!&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;span&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:red;"&gt;&amp;amp;nbsp;&lt;/span&gt; td section&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;td&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;tr&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;table&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;function&lt;/span&gt; spanClick(e)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{    &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    alert(&lt;span style="color: rgb(163, 21, 21);"&gt;'span'&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;(!e) &lt;span style="color:blue;"&gt;var&lt;/span&gt; e = window.&lt;span style="color:blue;"&gt;event&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;try&lt;/span&gt;{e.cancelBubble = &lt;span style="color:blue;"&gt;true&lt;/span&gt;; &lt;span style="color:blue;"&gt;return&lt;/span&gt;;}&lt;span style="color:blue;"&gt;catch&lt;/span&gt;(e){} &lt;span style="color:green;"&gt;// IE&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;try&lt;/span&gt;{e.stopPropagation(); &lt;span style="color:blue;"&gt;return&lt;/span&gt;;}&lt;span style="color:blue;"&gt;catch&lt;/span&gt;(e){}   &lt;span style="color:green;"&gt;// FireFox&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-2058550131830966889?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/2058550131830966889/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/05/how-to-prevent-current-events.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/2058550131830966889'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/2058550131830966889'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/05/how-to-prevent-current-events.html' title='How to prevent current event&apos;s propagation'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-8723750120047425835</id><published>2009-05-02T01:08:00.000-07:00</published><updated>2009-05-05T12:36:59.081-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='MS SQL'/><title type='text'>Raise custom exceptions</title><content type='html'>The SQL exception handling is a very powerful feature.&lt;br /&gt;Yet, you must use it carefully.&lt;br /&gt;&lt;br /&gt;It's important to understand that the "Catch" blocks swallows exceptions and application's Data Access Layer gets no information regarding SQL errors during execution.&lt;br /&gt;Therefore it can take allot of time to understand if there was error at all!&lt;br /&gt;&lt;br /&gt;In cases when it's crucial to notify calling application about execution crushes you can use &lt;a href="http://msdn.microsoft.com/en-us/library/ms178592.aspx"&gt;&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;RAISERROR&lt;/span&gt;&lt;/a&gt; routine.&lt;br /&gt;&lt;br /&gt;&lt;a style="color: rgb(51, 51, 255);" href="http://msdn.microsoft.com/en-us/library/ms178592.aspx"&gt;RAISERROR&lt;/a&gt; – able to generate system defended as well as custom errors.&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;BEGIN &lt;/span&gt;TRY&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;RAISERROR &lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;'Error raised in TRY block.'&lt;/span&gt;, &lt;span style="color:green;"&gt;-- Message text.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;               16, &lt;span style="color:green;"&gt;-- Severity.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;               1 &lt;span style="color:green;"&gt;-- State.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;               );&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;END &lt;/span&gt;TRY&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;BEGIN &lt;/span&gt;CATCH&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;DECLARE &lt;/span&gt;@ErrorMessage &lt;span style="color:blue;"&gt;NVARCHAR&lt;/span&gt;(4000);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;DECLARE &lt;/span&gt;@ErrorSeverity &lt;span style="color:blue;"&gt;INT&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;DECLARE &lt;/span&gt;@ErrorState &lt;span style="color:blue;"&gt;INT&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;SELECT &lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        @ErrorMessage = ERROR_MESSAGE(),&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        @ErrorSeverity = ERROR_SEVERITY(),&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        @ErrorState = ERROR_STATE();&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;RAISERROR &lt;/span&gt;(@ErrorMessage, &lt;span style="color:green;"&gt;-- Message text.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;               @ErrorSeverity, &lt;span style="color:green;"&gt;-- Severity.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;               @ErrorState &lt;span style="color:green;"&gt;-- State.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;               );&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;END &lt;/span&gt;CATCH;&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-8723750120047425835?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/8723750120047425835/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/05/raise-custom-exceptions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/8723750120047425835'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/8723750120047425835'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/05/raise-custom-exceptions.html' title='Raise custom exceptions'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-4259085044497038284</id><published>2009-04-29T21:51:00.000-07:00</published><updated>2009-04-29T21:53:22.861-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><category scheme='http://www.blogger.com/atom/ns#' term='AJAX'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><title type='text'>GET vs POST methods</title><content type='html'>a very nice post &lt;a style="color: rgb(51, 51, 255);" href="http://alfredjava.wordpress.com/2008/11/06/html-form-get-vs-post/" rel="bookmark"&gt;GET vs POST.&lt;br /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-4259085044497038284?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/4259085044497038284/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/get-vs-post-methods.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/4259085044497038284'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/4259085044497038284'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/get-vs-post-methods.html' title='GET vs POST methods'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-1591906551334173493</id><published>2009-04-29T13:23:00.000-07:00</published><updated>2009-04-29T13:25:49.030-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Initializing your assembly's information</title><content type='html'>Each assembly you build has usable information such as company name, copyright, version and etc...&lt;br /&gt;The way to modify it is to right click on your assembly project, under Application section choose Assembly Information.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_bgdRGHjn5sE/Sfi3tv7MIQI/AAAAAAAAAEE/arigstJ-JWI/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 395px;" src="http://1.bp.blogspot.com/_bgdRGHjn5sE/Sfi3tv7MIQI/AAAAAAAAAEE/arigstJ-JWI/s400/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5330212155579703554" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-1591906551334173493?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/1591906551334173493/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/initializing-your-assemblys-information.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/1591906551334173493'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/1591906551334173493'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/initializing-your-assemblys-information.html' title='Initializing your assembly&apos;s information'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_bgdRGHjn5sE/Sfi3tv7MIQI/AAAAAAAAAEE/arigstJ-JWI/s72-c/untitled.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-903897398026591340</id><published>2009-04-29T10:49:00.000-07:00</published><updated>2009-04-29T11:16:37.739-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='OOP'/><title type='text'>Interfaces vs. abstract Classes</title><content type='html'>Both interfaces and abstract classes provide a set of behavior patterns for derived types.&lt;br /&gt;&lt;br /&gt;Interface able to inherit from multiple interfaces while class can inherit only from one class and many interfaces.&lt;br /&gt;Abstract class can provide implementation for non abstract methods/properties while interface can't.&lt;br /&gt;Abstract classes can to contain variables while interfaces can't.&lt;br /&gt;Interface cannot provide access modifier for methods while abstract classes can.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-903897398026591340?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/903897398026591340/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/interfaces-vs-abstract-classes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/903897398026591340'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/903897398026591340'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/interfaces-vs-abstract-classes.html' title='Interfaces vs. abstract Classes'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-2175846136767333049</id><published>2009-04-29T08:36:00.000-07:00</published><updated>2009-04-29T09:06:28.041-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AJAX'/><category scheme='http://www.blogger.com/atom/ns#' term='Website Optimizations'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><title type='text'>General AJAX from scratch</title><content type='html'>AJAX stands for Asynchronous JavaScript and XML.&lt;br /&gt;&lt;br /&gt;By using AJAX you can:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;    Make requests to the server without reloading the page&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;    Parse and work with XML documents&lt;/li&gt;&lt;/ul&gt;A lot of websites use AJAX to improve user’s experience.&lt;br /&gt;&lt;br /&gt;Here is a small AJAX object example which making requests to a WebService:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;AjaxObject.js:&lt;/span&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;function&lt;/span&gt; AjaxObject()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;this&lt;/span&gt;.responseText = &lt;span style="color: rgb(163, 21, 21);"&gt;''&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;this&lt;/span&gt;.httpRequest = &lt;span style="color:blue;"&gt;null&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;this&lt;/span&gt;.method = &lt;span style="color: rgb(163, 21, 21);"&gt;'GET'&lt;/span&gt;;    &lt;span style="color:green;"&gt;// GET (Query String) or POST&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;this&lt;/span&gt;.async = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;      &lt;span style="color:green;"&gt;// true - asynchronous, false - synchronous&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;this&lt;/span&gt;.params = {};       &lt;span style="color:green;"&gt;// request params&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;this&lt;/span&gt;.Callback = &lt;span style="color:blue;"&gt;null&lt;/span&gt;;   &lt;span style="color:green;"&gt;// callback function&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;this&lt;/span&gt;.CreateHttpRequest = &lt;span style="color:blue;"&gt;function&lt;/span&gt;() {&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;try&lt;/span&gt; { &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt; XMLHttpRequest(); }&lt;span style="color:blue;"&gt;catch&lt;/span&gt;(e){}&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;try&lt;/span&gt; { &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt; ActiveXObject(&lt;span style="color: rgb(163, 21, 21);"&gt;"Msxml2.XMLHTTP"&lt;/span&gt;); }&lt;span style="color:blue;"&gt;catch&lt;/span&gt;(e){}&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;try&lt;/span&gt; { &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt; ActiveXObject(&lt;span style="color: rgb(163, 21, 21);"&gt;"Microsoft.XMLHTTP"&lt;/span&gt;); }&lt;span style="color:blue;"&gt;catch&lt;/span&gt;(e){}&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;null&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;// initialize http request &lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;( !(&lt;span style="color:blue;"&gt;this&lt;/span&gt;.httpRequest = &lt;span style="color:blue;"&gt;this&lt;/span&gt;.CreateHttpRequest()) ) {&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        alert(&lt;span style="color: rgb(163, 21, 21);"&gt;"XMLHttpRequest not supported"&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;this&lt;/span&gt;.AddParam = &lt;span style="color:blue;"&gt;function&lt;/span&gt;(key, value) {&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;this&lt;/span&gt;.params[key]=value;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;this&lt;/span&gt;.CreateRequestParams = &lt;span style="color:blue;"&gt;function&lt;/span&gt;() {&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;var&lt;/span&gt; Params = &lt;span style="color:blue;"&gt;new&lt;/span&gt; Array();&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;for&lt;/span&gt;(&lt;span style="color:blue;"&gt;var&lt;/span&gt; item &lt;span style="color:blue;"&gt;in&lt;/span&gt; &lt;span style="color:blue;"&gt;this&lt;/span&gt;.params) {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;var&lt;/span&gt; param = item + &lt;span style="color: rgb(163, 21, 21);"&gt;"="&lt;/span&gt; + encodeURIComponent(&lt;span style="color:blue;"&gt;this&lt;/span&gt;.params[item]); &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            Params.push(param);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; Params.join(&lt;span style="color: rgb(163, 21, 21);"&gt;"&amp;amp;"&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;this&lt;/span&gt;.Execute = &lt;span style="color:blue;"&gt;function&lt;/span&gt;(url) {&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;if&lt;/span&gt;(!&lt;span style="color:blue;"&gt;this&lt;/span&gt;.httpRequest) {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            alert(&lt;span style="color: rgb(163, 21, 21);"&gt;'unable to create HTTP request!'&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;null&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;var&lt;/span&gt; self = &lt;span style="color:blue;"&gt;this&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;this&lt;/span&gt;.httpRequest.onreadystatechange = &lt;span style="color:blue;"&gt;function&lt;/span&gt;() {&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;if&lt;/span&gt;(self.httpRequest.readyState == 4 &amp;amp;&amp;amp; self.httpRequest.status == 200) {                   &lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;                self.responseText = self.httpRequest.responseText;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;if&lt;/span&gt; ( self.Callback ){ &lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    self.Callback(self.responseText); &lt;/p&gt;&lt;p style="margin: 0px;"&gt;                }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;this&lt;/span&gt;.httpRequest.onerror = &lt;span style="color:blue;"&gt;function&lt;/span&gt;() {alert(&lt;span style="color: rgb(163, 21, 21);"&gt;'http request error!'&lt;/span&gt;); &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;null&lt;/span&gt;;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;this&lt;/span&gt;.httpRequest.onload = &lt;span style="color:blue;"&gt;function&lt;/span&gt;() {}&lt;span style="color:green;"&gt;// add your implementation&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;this&lt;/span&gt;.httpRequest.open(&lt;span style="color:blue;"&gt;this&lt;/span&gt;.method, url + (&lt;span style="color:blue;"&gt;this&lt;/span&gt;.method!=&lt;span style="color: rgb(163, 21, 21);"&gt;'GET'&lt;/span&gt; ? &lt;span style="color: rgb(163, 21, 21);"&gt;''&lt;/span&gt; : &lt;span style="color: rgb(163, 21, 21);"&gt;'?'&lt;/span&gt; + &lt;span style="color:blue;"&gt;this&lt;/span&gt;.CreateRequestParams()), &lt;span style="color:blue;"&gt;this&lt;/span&gt;.async); &lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;this&lt;/span&gt;.httpRequest.setRequestHeader(&lt;span style="color: rgb(163, 21, 21);"&gt;"Accept-Encoding"&lt;/span&gt;, &lt;span style="color: rgb(163, 21, 21);"&gt;"gzip, deflate"&lt;/span&gt;);    &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;this&lt;/span&gt;.httpRequest.setRequestHeader(&lt;span style="color: rgb(163, 21, 21);"&gt;"Content-type"&lt;/span&gt;, &lt;span style="color: rgb(163, 21, 21);"&gt;"application/x-www-form-urlencoded"&lt;/span&gt;);         &lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;this&lt;/span&gt;.httpRequest.send( &lt;span style="color:blue;"&gt;this&lt;/span&gt;.method!=&lt;span style="color: rgb(163, 21, 21);"&gt;'POST'&lt;/span&gt; ? &lt;span style="color:blue;"&gt;null&lt;/span&gt; : &lt;span style="color:blue;"&gt;this&lt;/span&gt;.CreateRequestParams());&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;if&lt;/span&gt;(!&lt;span style="color:blue;"&gt;this&lt;/span&gt;.async) {&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;if&lt;/span&gt;(self.httpRequest.readyState == 4) {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;if&lt;/span&gt;(self.httpRequest.status == 200)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;this&lt;/span&gt;.httpRequest.responseText;  &lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;else&lt;/span&gt; {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    alert(&lt;span style="color: rgb(163, 21, 21);"&gt;'Http response error!'&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;null&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;By using AjaxObject declaration you can create JavaScript object for performing AJAX requests.&lt;br /&gt;AjaxObject object able to make GET/POST request, add method parameters, receive encoded response and execute the task synchronously/asynchronously.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Test.html:&lt;/span&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;html&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;head&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;title&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;title&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script&lt;/span&gt; &lt;span style="color:red;"&gt;language&lt;/span&gt;&lt;span style="color:blue;"&gt;="javascript"&lt;/span&gt; &lt;span style="color:red;"&gt;src&lt;/span&gt;&lt;span style="color:blue;"&gt;="AjaxObject.js"&lt;/span&gt; &lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;="text/javascript"&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;head&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;body&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;form&lt;/span&gt; &lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;="form1"&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;div&lt;/span&gt; &lt;span style="color:red;"&gt;style&lt;/span&gt;&lt;span style="color:blue;"&gt;=""&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;input&lt;/span&gt; &lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;="button"&lt;/span&gt; &lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;="Go"&lt;/span&gt; &lt;span style="color:red;"&gt;onclick&lt;/span&gt;&lt;span style="color:blue;"&gt;="AjaxTestCall();"/&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;textarea&lt;/span&gt; &lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;="output_area"&lt;/span&gt; &lt;span style="color:red;"&gt;style&lt;/span&gt;&lt;span style="color:blue;"&gt;="height: 200px; width: 650px;"&lt;/span&gt; &lt;span style="color:red;"&gt;cols&lt;/span&gt;&lt;span style="color:blue;"&gt;="20"&lt;/span&gt; &lt;span style="color:red;"&gt;rows&lt;/span&gt;&lt;span style="color:blue;"&gt;="2"&lt;/span&gt; &lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=""&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;textarea&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;form&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;body&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script&lt;/span&gt; &lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;="text/javascript"&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;function&lt;/span&gt; AjaxTestCall()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//__________ Web Service _____________&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;var&lt;/span&gt; url = &lt;span style="color: rgb(163, 21, 21);"&gt;"http://localhost:34965/www/WebService.asmx/HelloWorld"&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;var&lt;/span&gt; output = document.getElementById(&lt;span style="color: rgb(163, 21, 21);"&gt;'output_area'&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    output.innerHTML = &lt;span style="color: rgb(163, 21, 21);"&gt;''&lt;/span&gt;;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//____________synchronous Get request example____________________&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;var&lt;/span&gt; ajaxObj1 = &lt;span style="color:blue;"&gt;new&lt;/span&gt; AjaxObject();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ajaxObj1.async = &lt;span style="color:blue;"&gt;false&lt;/span&gt;; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;var&lt;/span&gt; result = ajaxObj1.Execute(url);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;(result)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        output.innerHTML +=&lt;span style="color: rgb(163, 21, 21);"&gt;'GET, Synchronics request: '&lt;/span&gt; + result + &lt;span style="color: rgb(163, 21, 21);"&gt;'\r\n'&lt;/span&gt;;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//____________asynchronous Get request example___________________&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;var&lt;/span&gt; ajaxObj2 = &lt;span style="color:blue;"&gt;new&lt;/span&gt; AjaxObject();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ajaxObj2.Callback = &lt;span style="color:blue;"&gt;function&lt;/span&gt;(result){ &lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;      &lt;span style="color:blue;"&gt;if&lt;/span&gt;(result)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        output.innerHTML +=&lt;span style="color: rgb(163, 21, 21);"&gt;'GET, Synchronics request: '&lt;/span&gt; + result + &lt;span style="color: rgb(163, 21, 21);"&gt;'\r\n'&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ajaxObj2.Execute(url);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    url = &lt;span style="color: rgb(163, 21, 21);"&gt;"http://localhost:34965/www/WebService.asmx/GetUserInfo"&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//____________synchronous POST request example____________________&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;    var&lt;/span&gt; ajaxObj3 = &lt;span style="color:blue;"&gt;new&lt;/span&gt; AjaxObject();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ajaxObj3.async = &lt;span style="color:blue;"&gt;false&lt;/span&gt;; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ajaxObj3.method = &lt;span style="color: rgb(163, 21, 21);"&gt;'POST'&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ajaxObj3.AddParam(&lt;span style="color: rgb(163, 21, 21);"&gt;'nameName'&lt;/span&gt;, &lt;span style="color: rgb(163, 21, 21);"&gt;'David'&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;var&lt;/span&gt; result = ajaxObj3.Execute(url);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;(result)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        output.innerHTML +=&lt;span style="color: rgb(163, 21, 21);"&gt;'GET, Synchronics request: '&lt;/span&gt; + result + &lt;span style="color: rgb(163, 21, 21);"&gt;'\r\n'&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;//____________asynchronous POST request example________________&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;var&lt;/span&gt; ajaxObj4 = &lt;span style="color:blue;"&gt;new&lt;/span&gt; AjaxObject();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ajaxObj4.method = &lt;span style="color: rgb(163, 21, 21);"&gt;'POST'&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ajaxObj4.Callback = &lt;span style="color:blue;"&gt;function&lt;/span&gt;(result){ &lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;      &lt;span style="color:blue;"&gt;if&lt;/span&gt;(result)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        output.innerHTML +=&lt;span style="color: rgb(163, 21, 21);"&gt;'POST, Synchronics request: '&lt;/span&gt; + result + &lt;span style="color: rgb(163, 21, 21);"&gt;'\r\n'&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ajaxObj4.AddParam(&lt;span style="color: rgb(163, 21, 21);"&gt;'nameName'&lt;/span&gt;, &lt;span style="color: rgb(163, 21, 21);"&gt;'David'&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ajaxObj4.Execute(url);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;html&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;A Web Service implementation WebService.cs:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Web;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Collections;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Web.Services;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Web.Services.Protocols;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;[WebService(Namespace = &lt;span style="color: rgb(163, 21, 21);"&gt;"http://localhost:34965/"&lt;/span&gt;)]&lt;/p&gt;&lt;p style="margin: 0px;"&gt;[&lt;span style="color: rgb(43, 145, 175);"&gt;WebServiceBinding&lt;/span&gt;(ConformsTo = &lt;span style="color: rgb(43, 145, 175);"&gt;WsiProfiles&lt;/span&gt;.BasicProfile1_1)]&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;WebService&lt;/span&gt; : System.Web.Services.&lt;span style="color: rgb(43, 145, 175);"&gt;WebService&lt;/span&gt; {&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; WebService ()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    [&lt;span style="color: rgb(43, 145, 175);"&gt;WebMethod&lt;/span&gt;]&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt; HelloWorld() {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;"Hello World"&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    [&lt;span style="color: rgb(43, 145, 175);"&gt;WebMethod&lt;/span&gt;]&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt; GetUserInfo(&lt;span style="color:blue;"&gt;string&lt;/span&gt; nameName)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; nameName + &lt;span style="color: rgb(163, 21, 21);"&gt;" Information: David Libinson..."&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;To allow http requests to a Web Service add following code you’re Web.config:&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;webServices&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;protocols&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;        &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;add&lt;/span&gt;&lt;span style="color:blue;"&gt; &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;HttpGet&lt;/span&gt;"&lt;span style="color:blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;        &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;add&lt;/span&gt;&lt;span style="color:blue;"&gt; &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;HttpPost&lt;/span&gt;"&lt;span style="color:blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;      &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;protocols&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;    &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;webServices&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-2175846136767333049?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/2175846136767333049/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/general-ajax-from-scratch.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/2175846136767333049'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/2175846136767333049'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/general-ajax-from-scratch.html' title='General AJAX from scratch'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-1565776724527499305</id><published>2009-04-29T01:15:00.000-07:00</published><updated>2009-04-29T01:17:38.904-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>How to causes the linker to search for static library</title><content type='html'>A static library is a file (.lib) that contains functions, classes, or resources that an external program can use to complement its functionality.&lt;br /&gt;&lt;br /&gt;The following pragma causes the linker to search for the ws2_32.lib WinSocket library while linking:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#pragma&lt;/span&gt; &lt;span style="color:blue;"&gt;comment&lt;/span&gt;(&lt;span style="color:blue;"&gt;lib&lt;/span&gt;, &lt;span style="color: rgb(163, 21, 21);"&gt;"ws2_32.lib"&lt;/span&gt;)&lt;/p&gt;&lt;/div&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;The following pragma causes the linker to search for the OpenGL libraries while linking:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#pragma&lt;/span&gt; &lt;span style="color:blue;"&gt;comment&lt;/span&gt;(&lt;span style="color:blue;"&gt;lib&lt;/span&gt;, &lt;span style="color: rgb(163, 21, 21);"&gt;"opengl32.lib"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#pragma&lt;/span&gt; &lt;span style="color:blue;"&gt;comment&lt;/span&gt;(&lt;span style="color:blue;"&gt;lib&lt;/span&gt;, &lt;span style="color: rgb(163, 21, 21);"&gt;"glut32.lib"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#pragma&lt;/span&gt; &lt;span style="color:blue;"&gt;comment&lt;/span&gt;(&lt;span style="color:blue;"&gt;lib&lt;/span&gt;, &lt;span style="color: rgb(163, 21, 21);"&gt;"glu32.lib"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#pragma&lt;/span&gt; &lt;span style="color:blue;"&gt;comment&lt;/span&gt;(&lt;span style="color:blue;"&gt;lib&lt;/span&gt;, &lt;span style="color: rgb(163, 21, 21);"&gt;"glaux.lib"&lt;/span&gt;)&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-1565776724527499305?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/1565776724527499305/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/how-to-causes-linker-to-search-for.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/1565776724527499305'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/1565776724527499305'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/how-to-causes-linker-to-search-for.html' title='How to causes the linker to search for static library'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-6660533957594926309</id><published>2009-04-29T01:01:00.001-07:00</published><updated>2009-04-29T01:02:37.770-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>How to disable compiler warnings</title><content type='html'>Use "#pragma warning" macro to disable compilation warnings:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#pragma&lt;/span&gt; &lt;span style="color:blue;"&gt;warning&lt;/span&gt;(&lt;span style="color:blue;"&gt;disable&lt;/span&gt;: 4786)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;void&lt;/span&gt; _tmain()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;...&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-6660533957594926309?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/6660533957594926309/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/how-to-disable-compiler-warnings.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6660533957594926309'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6660533957594926309'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/how-to-disable-compiler-warnings.html' title='How to disable compiler warnings'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-470362562372776008</id><published>2009-04-29T00:11:00.000-07:00</published><updated>2009-04-29T00:18:16.115-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Pointer vs. Reference</title><content type='html'>Both pointer and reference are data items that contain an address.&lt;br /&gt;They avoid the copying of objects (pass by reference to a function) and reduce stack resources.&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;int&lt;/span&gt; var = 20;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;int&lt;/span&gt;&amp;amp; varRef = var;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    varRef += 5;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;int&lt;/span&gt; var = 20;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;int&lt;/span&gt;* varPtr = &amp;amp;var;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    *varPtr += 5;&lt;/p&gt;&lt;/div&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Still there are some differences:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Reference must point a variable on creation.&lt;/li&gt;&lt;li&gt;Reference can point only to one variable during its lifetime.&lt;/li&gt;&lt;li&gt;Unlike pointers reference cannot point to NULL.&lt;/li&gt;&lt;/ul&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; AB&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;int&lt;/span&gt; a;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;char&lt;/span&gt; b;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:green;"&gt;...&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;void&lt;/span&gt; foo(AB* ab){}&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;void&lt;/span&gt; foo(AB&amp;amp; ab){}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Use reference if want to make sure that you are working with real object (not NULL).&lt;br /&gt;Prefer using pointers if you need to perform integer arithmetic on address.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-470362562372776008?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/470362562372776008/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/pointer-vs-reference.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/470362562372776008'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/470362562372776008'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/pointer-vs-reference.html' title='Pointer vs. Reference'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-4452669150535186855</id><published>2009-04-27T13:59:00.000-07:00</published><updated>2009-04-27T14:01:39.264-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='DLL'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Loading DLL at run-time</title><content type='html'>I used the previous &lt;a href="http://progfeatures.blogspot.com/2009/04/unmanaged-dll-from-scratch.html#links"&gt;&lt;span style="font-weight: bold; color: rgb(51, 51, 255);"&gt;post's&lt;/span&gt;&lt;/a&gt; DLL to execute function.&lt;br /&gt;&lt;br /&gt;Here is an example of loading DLL at run-time:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:green;"&gt;// pointer to function declaration&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;typedef&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; (*pMyFirstExportedFunction)(&lt;span style="color:blue;"&gt;void&lt;/span&gt;);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;int&lt;/span&gt; _tmain(&lt;span style="color:blue;"&gt;int&lt;/span&gt; argc, _TCHAR* argv[])&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;// map the DLL into current process memory space&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    HINSTANCE hLib = LoadLibrary(TEXT(&lt;span style="color: rgb(163, 21, 21);"&gt;"MyFirstDLL.dll"&lt;/span&gt;));&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;(!hLib) &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        printf(&lt;span style="color: rgb(163, 21, 21);"&gt;"Unable to load MyFirstDLL.dll...\n"&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; -1;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    pMyFirstExportedFunction MyFirstExportedFunction = 0;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;// get function's address&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;( !(MyFirstExportedFunction = (pMyFirstExportedFunction)GetProcAddress(hLib, &lt;span style="color: rgb(163, 21, 21);"&gt;"MyFirstExportedFunction"&lt;/span&gt;)) )&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        printf(&lt;span style="color: rgb(163, 21, 21);"&gt;"Unable to load 'MyFirstExportedFunction' function from MyFirstDLL.dll."&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; -1;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;// execute the function&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    MyFirstExportedFunction();&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;// release the library&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    FreeLibrary(hLib);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; 0;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-4452669150535186855?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/4452669150535186855/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/loading-dll-at-run-time.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/4452669150535186855'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/4452669150535186855'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/loading-dll-at-run-time.html' title='Loading DLL at run-time'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-7901671414753101067</id><published>2009-04-27T04:38:00.000-07:00</published><updated>2009-04-27T11:47:34.794-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>The most useful ANSI predefined macros</title><content type='html'>&lt;span style="font-weight: bold;"&gt;__FILE__&lt;/span&gt; - The name of the current source file.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;__LINE__&lt;/span&gt; - The line number in the current source file.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;__TIME__&lt;/span&gt; - The most recent compilation time of the current source file.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;__DATE__ &lt;/span&gt;- The compilation date of the current source file.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-7901671414753101067?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/7901671414753101067/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/most-useful-ansi-predefined-macros.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7901671414753101067'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7901671414753101067'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/most-useful-ansi-predefined-macros.html' title='The most useful ANSI predefined macros'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-5644306068288115677</id><published>2009-04-26T12:52:00.000-07:00</published><updated>2009-04-27T13:30:07.914-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='DLL'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Unmanaged DLL from scratch</title><content type='html'>&lt;span style="font-weight: bold; color: rgb(255, 102, 0);"&gt;What is a DLL?&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;Dynamic-link library&lt;/span&gt; (DLL) is a shared library which can be mapped into process memory space at run-time.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(255, 102, 0);"&gt;Why to use it?&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;/span&gt;&lt;blockquote&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;Versioning&lt;/span&gt; - it is easy to maintain the application versions by simply replacing old  modules with new ones.&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;br /&gt;Modularity&lt;/span&gt; - by using DLLs it is easy to separate project into independent units which can perform different unique roles.&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;br /&gt;Code reuse &lt;/span&gt;- one code implementation can be exploited on different processes.&lt;br /&gt;&lt;br /&gt;One of the great advantages of DLLs is that it is possible to use them in most popular programming languages such as: VB, Delphi, C\C++, .Net and etc...&lt;br /&gt;Unfortunately, DLLs are platform dependant.&lt;/blockquote&gt;&lt;br /&gt;Allot of organization prefer using DLLs for implementing their logical units.&lt;br /&gt;A GUI parts of application can be changed rapidly (MFC, WinForms, WPF) while logical implementations can remain unchanged.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Let's get started:&lt;/span&gt;&lt;br /&gt;If you are using MS Visual Studio 6, open a new project and choose "Win32 Dynamic-Link Library", Give your project a name and click "OK" button:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_bgdRGHjn5sE/SfYDZ1gKM9I/AAAAAAAAADU/9FBzW_sZtQw/s1600-h/untitled1.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 200px; height: 130px;" src="http://2.bp.blogspot.com/_bgdRGHjn5sE/SfYDZ1gKM9I/AAAAAAAAADU/9FBzW_sZtQw/s200/untitled1.JPG" alt="" id="BLOGGER_PHOTO_ID_5329450951433139154" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Choose "An empty DLL project" option and click Finish button.&lt;br /&gt;&lt;br /&gt;If you are using MS Visual Studio 2005/2008, open a new project and choose "Visual C++" under Project Types and "Win32 Console Application" under Templates, Give your project a name and click "OK" button:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_bgdRGHjn5sE/SfYDsJOEUiI/AAAAAAAAADc/0wY54rSxOiw/s1600-h/untitled2.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 200px; height: 143px;" src="http://1.bp.blogspot.com/_bgdRGHjn5sE/SfYDsJOEUiI/AAAAAAAAADc/0wY54rSxOiw/s200/untitled2.JPG" alt="" id="BLOGGER_PHOTO_ID_5329451265963610658" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Click next button, Choose "DLL" at Application type section, "Empty project" at Additional options and click "Finish" button:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_bgdRGHjn5sE/SfYD4fElWDI/AAAAAAAAADk/zHdr3m7YWLo/s1600-h/untitled3.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 200px; height: 129px;" src="http://3.bp.blogspot.com/_bgdRGHjn5sE/SfYD4fElWDI/AAAAAAAAADk/zHdr3m7YWLo/s200/untitled3.JPG" alt="" id="BLOGGER_PHOTO_ID_5329451477987842098" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Now create header and implementation files.&lt;br /&gt;Give them same name as you gave to your project:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_bgdRGHjn5sE/SfYFl4M1GpI/AAAAAAAAADs/athUWizSioU/s1600-h/untitled4.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 200px; height: 126px;" src="http://1.bp.blogspot.com/_bgdRGHjn5sE/SfYFl4M1GpI/AAAAAAAAADs/athUWizSioU/s200/untitled4.JPG" alt="" id="BLOGGER_PHOTO_ID_5329453357339056786" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Next step is to enable DLL to export functions and resources.&lt;br /&gt;&lt;br /&gt;Write the following code in MyFirstDLL.h file that you created:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#ifdef&lt;/span&gt; MYFIRSTDLL_EXPORTS&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;#define&lt;/span&gt; MYFIRSTDLL_API &lt;span style="color:blue;"&gt;__declspec&lt;/span&gt;(&lt;span style="color:blue;"&gt;dllexport&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#else&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:gray;"&gt;    #define MYFIRSTDLL_API __declspec(dllimport)&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#endif&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;__declspec&lt;/span&gt; keyword specifies that an instance of a given type is to be stored with a Microsoft-specific storage-class attribute dllexport and dllimport.&lt;br /&gt;&lt;br /&gt;The &lt;span style="color: rgb(51, 51, 255);"&gt;dllexport&lt;/span&gt; and &lt;span style="color: rgb(51, 51, 255);"&gt;dllimport&lt;/span&gt; storage-class attributes are Microsoft-specific extensions to the C and C++ languages.&lt;br /&gt;They enable you to export and import functions, data, and objects to and from a DLL.&lt;br /&gt;In most cases you'll need only to export stuff from your DLL.&lt;br /&gt;&lt;br /&gt;Write MYFIRSTDLL_API before each function that you want to be exported :&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;MYFIRSTDLL_API &lt;span style="color:blue;"&gt;void&lt;/span&gt; MyFirstExportedFunction(&lt;span style="color:blue;"&gt;void&lt;/span&gt;);&lt;/p&gt;&lt;/div&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now it is time to write an implementation:&lt;br /&gt;&lt;br /&gt;Add following code to your MyFirstDLL.cpp file:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#define&lt;/span&gt; MYFIRSTDLL_EXPORTS&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;"MyFirstDLL.h"&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;void&lt;/span&gt; MyFirstExportedFunction(&lt;span style="color:blue;"&gt;void&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    printf(&lt;span style="color: rgb(163, 21, 21);"&gt;"This is my first function exported from DLL!"&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;BOOL APIENTRY DllMain( HANDLE hModule, &lt;/p&gt;&lt;p style="margin: 0px;"&gt;                       DWORD  ul_reason_for_call, &lt;/p&gt;&lt;p style="margin: 0px;"&gt;                       LPVOID lpReserved&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                   )&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;switch&lt;/span&gt; (ul_reason_for_call)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;case&lt;/span&gt; DLL_PROCESS_ATTACH:&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;case&lt;/span&gt; DLL_THREAD_ATTACH:&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;case&lt;/span&gt; DLL_THREAD_DETACH:&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;case&lt;/span&gt; DLL_PROCESS_DETACH:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; TRUE;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;In order to export your functions it is important to define &lt;span style="color: rgb(51, 51, 255);"&gt;MYFIRSTDLL_EXPORTS&lt;/span&gt; before including MyFirstDLL.h definitions.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The &lt;span style="color: rgb(51, 51, 255);"&gt;DllMain&lt;/span&gt; function is the entry point into a dynamic-link library. The function is called when processes and threads are initialized and terminated, or upon calls to the LoadLibrary and FreeLibrary functions.&lt;br /&gt;&lt;br /&gt;ul_reason_for_call Indicates why the DLL entry-point function is being called.&lt;br /&gt;&lt;br /&gt;Make your function implementation and build the project.&lt;br /&gt;Here it is, your DLL is ready!&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_bgdRGHjn5sE/SfYP0WhkyeI/AAAAAAAAAD0/PPfurjfBiVI/s1600-h/untitled5.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 150px; height: 96px;" src="http://2.bp.blogspot.com/_bgdRGHjn5sE/SfYP0WhkyeI/AAAAAAAAAD0/PPfurjfBiVI/s320/untitled5.JPG" alt="" id="BLOGGER_PHOTO_ID_5329464601113577954" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;In order to see the exported interface I'll use the &lt;a style="color: rgb(51, 51, 255);" href="http://progfeatures.blogspot.com/2009/04/dependency-walker.html#links"&gt;Dependency Walker&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_bgdRGHjn5sE/SfYRG73uDJI/AAAAAAAAAD8/Xb1Rnehl0MM/s1600-h/untitled6.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 200px; height: 76px;" src="http://2.bp.blogspot.com/_bgdRGHjn5sE/SfYRG73uDJI/AAAAAAAAAD8/Xb1Rnehl0MM/s200/untitled6.JPG" alt="" id="BLOGGER_PHOTO_ID_5329466019887844498" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;As you can see, the &lt;span style="color: rgb(255, 102, 0);"&gt;?MyFirstExportedFunction@@YAXXZ&lt;/span&gt; function name is not the same we gave it.&lt;br /&gt;That because of Name Mangling.&lt;br /&gt;&lt;br /&gt;The name mangling is a process that makes it possible to have two functions with the same name in C++ as long as they have different arguments (overloaded functions).Since C++ is a type safe language and fairly strict about types, it implements name mangling.&lt;br /&gt;&lt;br /&gt;To prevent the mangling add extern "C" declaration:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#ifdef&lt;/span&gt; MYFIRSTDLL_EXPORTS&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;#define&lt;/span&gt; MYFIRSTDLL_API &lt;span style="color:blue;"&gt;__declspec&lt;/span&gt;(&lt;span style="color:blue;"&gt;dllexport&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#else&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:gray;"&gt;    #define MYFIRSTDLL_API __declspec(dllimport)&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#endif&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;extern&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;"C"&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    MYFIRSTDLL_API &lt;span style="color:blue;"&gt;void&lt;/span&gt; MyFirstExportedFunction(&lt;span style="color:blue;"&gt;void&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;&lt;/div&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Build the project and check the exported interface.&lt;br /&gt;That is your first DLL!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-5644306068288115677?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/5644306068288115677/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/unmanaged-dll-from-scratch.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/5644306068288115677'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/5644306068288115677'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/unmanaged-dll-from-scratch.html' title='Unmanaged DLL from scratch'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_bgdRGHjn5sE/SfYDZ1gKM9I/AAAAAAAAADU/9FBzW_sZtQw/s72-c/untitled1.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-6300710339674274489</id><published>2009-04-25T06:58:00.000-07:00</published><updated>2009-04-25T10:13:52.149-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Use initializer for static variables such arrays, structures and unions instead of memset</title><content type='html'>Please answer me this:&lt;br /&gt;How many of you use next type of action to reset your variable before use?&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;typedef&lt;/span&gt; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; _A&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;int&lt;/span&gt;        x;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;char&lt;/span&gt;       y;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;char&lt;/span&gt;*      z;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}A;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;void&lt;/span&gt; main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    A a;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    ZeroMemory(&amp;amp;a, &lt;span style="color:blue;"&gt;sizeof&lt;/span&gt;(a));&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;// Or:&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;int&lt;/span&gt; arr[50];&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    memset(arr, 0, &lt;span style="color:blue;"&gt;sizeof&lt;/span&gt;(arr));&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;The truth is that we can perform same action without using additional functions such as memset or ZeroMemory (a macro which uses memset function)!&lt;br /&gt;&lt;br /&gt;C language allows ordinary variables, structures, unions and arrays to be given initial values in their definitions.&lt;br /&gt;C++ allows the initializer for an automatic member variable of a union or structure type to be a constant or non-constant expression.&lt;br /&gt;&lt;br /&gt;The initializer is preceded by an equal sign (=).&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;void&lt;/span&gt; main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    A a={0};&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    A a1 = {20, &lt;span style="color:maroon;"&gt;'Z'&lt;/span&gt;, &lt;span style="color:maroon;"&gt;"some string"&lt;/span&gt;};&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;int&lt;/span&gt; arr[50]={50};&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_bgdRGHjn5sE/SfMZRtWvGcI/AAAAAAAAADE/MNp0Y6Fr_n8/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 338px; height: 114px;" src="http://3.bp.blogspot.com/_bgdRGHjn5sE/SfMZRtWvGcI/AAAAAAAAADE/MNp0Y6Fr_n8/s400/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5328630576132856258" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_bgdRGHjn5sE/SfMZiX7VxbI/AAAAAAAAADM/8a5DYKnpI7E/s1600-h/untitled1.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 388px; height: 106px;" src="http://4.bp.blogspot.com/_bgdRGHjn5sE/SfMZiX7VxbI/AAAAAAAAADM/8a5DYKnpI7E/s400/untitled1.JPG" alt="" id="BLOGGER_PHOTO_ID_5328630862438581682" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-6300710339674274489?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/6300710339674274489/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/use-initializer-for-static-variables.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6300710339674274489'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6300710339674274489'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/use-initializer-for-static-variables.html' title='Use initializer for static variables such arrays, structures and unions instead of memset'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_bgdRGHjn5sE/SfMZRtWvGcI/AAAAAAAAADE/MNp0Y6Fr_n8/s72-c/untitled.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-6366875331575791489</id><published>2009-04-24T15:19:00.000-07:00</published><updated>2009-04-24T16:09:21.853-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows Services'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Handling Windows Services (the unmanaged way)</title><content type='html'>The Win API provides set of &lt;a href="http://msdn.microsoft.com/en-us/library/ms685942%28VS.85%29.aspx"&gt;&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;function&lt;/span&gt;&lt;/a&gt; which allows handling Windows Services.&lt;br /&gt;Here is a small class for managing local services:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;WindowsServiceHandler.h&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color:maroon;"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; WindowsServiceHandler&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;enum&lt;/span&gt; ServiceStatus {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WS_ERROR                = -1,&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WS_STOPPED              = 0x00000001,&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WS_START_PENDING        = 0x00000002,&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WS_STOP_PENDING         = 0x00000003,&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WS_RUNNING              = 0x00000004,&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WS_CONTINUE_PENDING     = 0x00000005,&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WS_PAUSE_PENDING        = 0x00000006,&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WS_PAUSED               = 0x00000007&lt;/p&gt;&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WindowsServiceHandler(&lt;span style="color:blue;"&gt;void&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;virtual&lt;/span&gt; ~WindowsServiceHandler(&lt;span style="color:blue;"&gt;void&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;virtual&lt;/span&gt; &lt;span style="color:blue;"&gt;bool&lt;/span&gt; Stop(coname);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;virtual&lt;/span&gt; &lt;span style="color:blue;"&gt;bool&lt;/span&gt; Start(&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;char&lt;/span&gt;* serviceName);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;virtual&lt;/span&gt; &lt;span style="color:blue;"&gt;bool&lt;/span&gt; Restart(&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;char&lt;/span&gt;* serviceName);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;virtual&lt;/span&gt; &lt;span style="color:blue;"&gt;bool&lt;/span&gt; QueryStatus(&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;char&lt;/span&gt;* serviceName, ServiceStatus&amp;amp; status);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    SC_HANDLE    m_hManager;st &lt;span style="color:blue;"&gt;char&lt;/span&gt;* serviceN&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;private&lt;/span&gt;:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WindowsServiceHandler(WindowsServiceHandler&amp;amp;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WindowsServiceHandler&amp;amp; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;=(WindowsServiceHandler&amp;amp;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;};&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;WindowsServiceHandler.cpp&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color:maroon;"&gt;"WindowsServiceHandler.h"&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;WindowsServiceHandler::WindowsServiceHandler(&lt;span style="color:blue;"&gt;void&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;:m_hManager(0)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    m_hManager = OpenSCManager(                   &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        NULL,                   &lt;span style="color:green;"&gt;// machine (NULL == local)     &lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        NULL,                   &lt;span style="color:green;"&gt;// database (NULL == default)         &lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        SC_MANAGER_ALL_ACCESS); &lt;span style="color:green;"&gt;// access required                    &lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;WindowsServiceHandler::~WindowsServiceHandler(&lt;span style="color:blue;"&gt;void&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    CloseServiceHandle(m_hManager); &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    m_hManager = 0;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;bool&lt;/span&gt; WindowsServiceHandler::Stop(&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;char&lt;/span&gt;* serviceName)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;(!m_hManager || !serviceName)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    SC_HANDLE hService = 0;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt; (0 == (hService = OpenService(m_hManager, TEXT(serviceName), SERVICE_ALL_ACCESS))) {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    SERVICE_STATUS status = {0};&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!ControlService(hService, SERVICE_CONTROL_STOP, &amp;amp;status)) {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;while&lt;/span&gt;(QueryServiceStatus(hService, &amp;amp;status))        &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {                    &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;if&lt;/span&gt; (status.dwCurrentState == SERVICE_STOP_PENDING)                                       &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            Sleep(10);                    &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;else&lt;/span&gt;                &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;break&lt;/span&gt;;                &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }                &lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; (status.dwCurrentState == SERVICE_STOPPED);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;bool&lt;/span&gt; WindowsServiceHandler::Start(&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;char&lt;/span&gt;* serviceName)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;(!m_hManager || !serviceName)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    SC_HANDLE hService = 0;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt; (0 == (hService = OpenService(m_hManager, TEXT(serviceName), SERVICE_ALL_ACCESS))) {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;(!StartService(hService, 0, NULL)) {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    SERVICE_STATUS status = {0};&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;while&lt;/span&gt;(QueryServiceStatus(hService, &amp;amp;status))        &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {                    &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;if&lt;/span&gt; (status.dwCurrentState == SERVICE_START_PENDING)                       &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            Sleep(10);                    &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;else&lt;/span&gt;                &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;break&lt;/span&gt;;                &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }                &lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; (status.dwCurrentState == SERVICE_RUNNING);  &lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;bool&lt;/span&gt; WindowsServiceHandler::Restart(&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;char&lt;/span&gt;* serviceName)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; (Stop(serviceName) &amp;amp;&amp;amp; Start(serviceName));&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;bool&lt;/span&gt; WindowsServiceHandler::QueryStatus(&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;char&lt;/span&gt;* serviceName, ServiceStatus&amp;amp; status)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;(!m_hManager || !serviceName) {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        status = ServiceStatus::WS_ERROR;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    SC_HANDLE hService = 0;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt; (0 == (hService = OpenService(m_hManager, TEXT(serviceName), SERVICE_ALL_ACCESS))) {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        status = ServiceStatus::WS_ERROR;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    SERVICE_STATUS sStatus = {0};&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;if&lt;/span&gt;(!QueryServiceStatus(hService, &amp;amp;sStatus)) {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        status = ServiceStatus::WS_ERROR;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    status = (ServiceStatus)sStatus.dwCurrentState;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;main.cpp&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color:maroon;"&gt;"WindowsServiceHandler.h"&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color:maroon;"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; &lt;span style="color:blue;"&gt;namespace&lt;/span&gt; std;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;void&lt;/span&gt; main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;char&lt;/span&gt; serviceName[] = &lt;span style="color:maroon;"&gt;"MSSQL$SQLEXPRESS"&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WindowsServiceHandler serviceHandler;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    WindowsServiceHandler::ServiceStatus status;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    cout&amp;lt;&amp;lt;&lt;span style="color:maroon;"&gt;"status..."&lt;/span&gt;&amp;lt;&amp;lt;endl;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    serviceHandler.QueryStatus(serviceName, status);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    cout&amp;lt;&amp;lt;&lt;span style="color:maroon;"&gt;"Service is running: "&lt;/span&gt;&amp;lt;&amp;lt;(status==WindowsServiceHandler::ServiceStatus::WS_RUNNING?&lt;span style="color:maroon;"&gt;"true"&lt;/span&gt;:&lt;span style="color:maroon;"&gt;"false"&lt;/span&gt;)&amp;lt;&amp;lt;endl;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    cout&amp;lt;&amp;lt;&lt;span style="color:maroon;"&gt;"stop..."&lt;/span&gt;&amp;lt;&amp;lt;endl;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    serviceHandler.Stop(serviceName);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    serviceHandler.QueryStatus(serviceName, status);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    cout&amp;lt;&amp;lt;&lt;span style="color:maroon;"&gt;"Service is running: "&lt;/span&gt;&amp;lt;&amp;lt;(status==WindowsServiceHandler::ServiceStatus::WS_RUNNING?&lt;span style="color:maroon;"&gt;"true"&lt;/span&gt;:&lt;span style="color:maroon;"&gt;"false"&lt;/span&gt;)&amp;lt;&amp;lt;endl;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    cout&amp;lt;&amp;lt;&lt;span style="color:maroon;"&gt;"start..."&lt;/span&gt;&amp;lt;&amp;lt;endl;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    serviceHandler.Start(serviceName);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    serviceHandler.QueryStatus(serviceName, status);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    cout&amp;lt;&amp;lt;&lt;span style="color:maroon;"&gt;"Service is running: "&lt;/span&gt;&amp;lt;&amp;lt;(status==WindowsServiceHandler::ServiceStatus::WS_RUNNING?&lt;span style="color:maroon;"&gt;"true"&lt;/span&gt;:&lt;span style="color:maroon;"&gt;"false"&lt;/span&gt;)&amp;lt;&amp;lt;endl;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    cout&amp;lt;&amp;lt;&lt;span style="color:maroon;"&gt;"restart..."&lt;/span&gt;&amp;lt;&amp;lt;endl;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    serviceHandler.Restart(serviceName);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    serviceHandler.QueryStatus(serviceName, status);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    cout&amp;lt;&amp;lt;&lt;span style="color:maroon;"&gt;"Service is running: "&lt;/span&gt;&amp;lt;&amp;lt;(status==WindowsServiceHandler::ServiceStatus::WS_RUNNING?&lt;span style="color:maroon;"&gt;"true"&lt;/span&gt;:&lt;span style="color:maroon;"&gt;"false"&lt;/span&gt;)&amp;lt;&amp;lt;endl;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-6366875331575791489?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/6366875331575791489/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/handling-windows-services-unmanaged-way.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6366875331575791489'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6366875331575791489'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/handling-windows-services-unmanaged-way.html' title='Handling Windows Services (the unmanaged way)'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-3200108086151141084</id><published>2009-04-22T13:57:00.000-07:00</published><updated>2009-04-22T14:07:41.158-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='Registry'/><title type='text'>Handling registry entities manualy</title><content type='html'>In order to export registry entity manually, type regedit command in “Run” command prompt,&lt;br /&gt;Go to the desired key, right click on it and choose “Export” option:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_bgdRGHjn5sE/Se-Gw0uOE0I/AAAAAAAAACk/lkWUItcN1dk/s1600-h/untitled2.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 281px;" src="http://3.bp.blogspot.com/_bgdRGHjn5sE/Se-Gw0uOE0I/AAAAAAAAACk/lkWUItcN1dk/s400/untitled2.JPG" alt="" id="BLOGGER_PHOTO_ID_5327625057546933058" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Give a name to file and click save button.&lt;br /&gt;&lt;br /&gt;Open the created file:&lt;br /&gt;&lt;blockquote&gt;Windows Registry Editor Version 5.00&lt;br /&gt;[HKEY_CURRENT_USER\Software\MyWindowsApplication]&lt;br /&gt;"Version"="1.0"&lt;br /&gt;"xPosition"=dword:00000234&lt;br /&gt;"yPosition"=dword:00000176&lt;/blockquote&gt;&lt;br /&gt;You can update the Values and update the registry by double-clicking on the updated file!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-3200108086151141084?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/3200108086151141084/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/handling-registry-entities-manualy.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/3200108086151141084'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/3200108086151141084'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/handling-registry-entities-manualy.html' title='Handling registry entities manualy'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_bgdRGHjn5sE/Se-Gw0uOE0I/AAAAAAAAACk/lkWUItcN1dk/s72-c/untitled2.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-9076009279387393611</id><published>2009-04-22T13:17:00.000-07:00</published><updated>2009-04-22T13:27:08.794-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Registry'/><title type='text'>Saving application’s settings in Registry</title><content type='html'>The Windows Registry is a data storage which holds information about the operating system and software/hardware configurations.&lt;br /&gt;It has a tree-like structure which based on key/value settings.&lt;br /&gt;Keys are represented as folder (value holders) and values can store different value types:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_bgdRGHjn5sE/Se97zQwoDaI/AAAAAAAAACU/n5DRHJJ0JTI/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 176px;" src="http://3.bp.blogspot.com/_bgdRGHjn5sE/Se97zQwoDaI/AAAAAAAAACU/n5DRHJJ0JTI/s400/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5327613004805049762" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Many applications use Registry to store application's configurations such as version, screen location, paths and etc...&lt;br /&gt;&lt;br /&gt;The registry can be edited manually by running regedt executable:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_bgdRGHjn5sE/Se98K6lijhI/AAAAAAAAACc/NlrWFLlJlew/s1600-h/untitled1.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 218px;" src="http://3.bp.blogspot.com/_bgdRGHjn5sE/Se98K6lijhI/AAAAAAAAACc/NlrWFLlJlew/s400/untitled1.JPG" alt="" id="BLOGGER_PHOTO_ID_5327613411169832466" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;The Registry is split into a number of logical sections. &lt;a href="http://en.wikipedia.org/wiki/Windows_registry"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;Each section has its own purpose and responsibilities&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A C# Example: The application saves version and Form's screen location in registry.&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.ComponentModel;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Data;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Drawing;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Text;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Windows.Forms;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; Microsoft.Win32;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;namespace&lt;/span&gt; MyWindowsApplication&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;partial&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Form1&lt;/span&gt; : &lt;span style="color: rgb(43, 145, 175);"&gt;Form&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    { &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt; appConfigPath = &lt;span style="color: rgb(163, 21, 21);"&gt;"Software\\MyWindowsApplication"&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; Form1()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            InitializeComponent();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; OnClosing(&lt;span style="color: rgb(43, 145, 175);"&gt;CancelEventArgs&lt;/span&gt; e)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;             &lt;span style="color:blue;"&gt;base&lt;/span&gt;.OnClosed(e);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;             &lt;span style="color: rgb(43, 145, 175);"&gt;RegistryKey&lt;/span&gt; key = &lt;span style="color: rgb(43, 145, 175);"&gt;Registry&lt;/span&gt;.OpenSubKey(appConfigPath, &lt;span style="color:blue;"&gt;true&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;             &lt;span style="color:blue;"&gt;if&lt;/span&gt; (key == &lt;span style="color:blue;"&gt;null&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color: rgb(43, 145, 175);"&gt;Registry&lt;/span&gt;.CurrentUser.CreateSubKey(appConfigPath);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;             key.SetValue(&lt;span style="color: rgb(163, 21, 21);"&gt;"Version"&lt;/span&gt;, &lt;span style="color: rgb(163, 21, 21);"&gt;"1.0"&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;RegistryValueKind&lt;/span&gt;.String);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;             key.SetValue(&lt;span style="color: rgb(163, 21, 21);"&gt;"xPosition"&lt;/span&gt;, &lt;span style="color:blue;"&gt;this&lt;/span&gt;.DesktopLocation.X, &lt;span style="color: rgb(43, 145, 175);"&gt;RegistryValueKind&lt;/span&gt;.DWord);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;             key.SetValue(&lt;span style="color: rgb(163, 21, 21);"&gt;"yPosition"&lt;/span&gt;, &lt;span style="color:blue;"&gt;this&lt;/span&gt;.DesktopLocation.Y, &lt;span style="color: rgb(43, 145, 175);"&gt;RegistryValueKind&lt;/span&gt;.DWord);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; OnLoad(&lt;span style="color: rgb(43, 145, 175);"&gt;EventArgs&lt;/span&gt; e)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;base&lt;/span&gt;.OnLoad(e);&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;RegistryKey&lt;/span&gt; key = &lt;span style="color: rgb(43, 145, 175);"&gt;Registry&lt;/span&gt;.CurrentUser.OpenSubKey(appConfigPath);&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;if&lt;/span&gt; (key != &lt;span style="color:blue;"&gt;null&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;this&lt;/span&gt;.Text = (&lt;span style="color:blue;"&gt;string&lt;/span&gt;)key.GetValue(&lt;span style="color: rgb(163, 21, 21);"&gt;"Version"&lt;/span&gt;);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color: rgb(43, 145, 175);"&gt;Point&lt;/span&gt; point = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Point&lt;/span&gt;(&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    (&lt;span style="color:blue;"&gt;int&lt;/span&gt;)key.GetValue(&lt;span style="color: rgb(163, 21, 21);"&gt;"xPosition"&lt;/span&gt;),&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    (&lt;span style="color:blue;"&gt;int&lt;/span&gt;)key.GetValue(&lt;span style="color: rgb(163, 21, 21);"&gt;"yPosition"&lt;/span&gt;));&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;this&lt;/span&gt;.DesktopLocation = point;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-9076009279387393611?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/9076009279387393611/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/saving-applications-settings-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/9076009279387393611'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/9076009279387393611'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/saving-applications-settings-in.html' title='Saving application’s settings in Registry'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_bgdRGHjn5sE/Se97zQwoDaI/AAAAAAAAACU/n5DRHJJ0JTI/s72-c/untitled.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-171302640826198085</id><published>2009-04-21T12:42:00.000-07:00</published><updated>2009-04-21T13:11:12.491-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Useful Tools'/><title type='text'>The Dependency Walker</title><content type='html'>&lt;a href="http://en.wikipedia.org/wiki/Dependency_walker"&gt;&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;Dependency Walker&lt;/span&gt;&lt;/a&gt; is a utility that scans any 32-bit or 64-bit Windows module (exe, dll, ocx, sys, etc.) and builds a hierarchical tree diagram of all dependent modules.&lt;br /&gt;&lt;br /&gt;For each module found, it lists all the functions that are exported by that module, and which of those functions are actually being called by other modules. Another view displays detailed information about each file including a full path to the file, base address, version numbers, machine type, debug information, and more.&lt;br /&gt;&lt;br /&gt;Dependency Walker runs on Windows 95, 98, Me, NT, 2000, XP, 2003, and Vista.&lt;br /&gt;&lt;br /&gt;You can find it at:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Microsoft Visual Studio 6:&lt;/span&gt;&lt;br /&gt;C:\Program Files\Microsoft Visual Studio\Common\Tools\DEPENDS.EXE&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Microsoft Visual Studio 2005:&lt;/span&gt;&lt;br /&gt;C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\Bin\Depends.Exe&lt;br /&gt;&lt;br /&gt;Or you can download it from &lt;a style="color: rgb(255, 102, 0);" href="http://www.dependencywalker.com"&gt;http://www.dependencywalker.com&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-171302640826198085?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/171302640826198085/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/dependency-walker.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/171302640826198085'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/171302640826198085'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/dependency-walker.html' title='The Dependency Walker'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-7200159581200291233</id><published>2009-04-20T03:53:00.000-07:00</published><updated>2009-04-20T03:56:53.748-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>What is vshost.exe file do?</title><content type='html'>Many of you may seen that when you run your .Net application in DEBUG mode&lt;br /&gt;The "Windows Task Manager" shows .vshost.exe instead of your executable name:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_bgdRGHjn5sE/SexUdamRyCI/AAAAAAAAACE/IIwuvB5I3Ks/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 238px; height: 208px;" src="http://1.bp.blogspot.com/_bgdRGHjn5sE/SexUdamRyCI/AAAAAAAAACE/IIwuvB5I3Ks/s400/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5326725323605264418" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;So what is it vshost.exe, and why we need it?&lt;br /&gt;&lt;br /&gt;vshost.exe is a hosting process feature that improves debugging performance, enables partial trust debugging, and enables design time expression evaluation:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_bgdRGHjn5sE/SexUzYD47sI/AAAAAAAAACM/4ZKm8rN2ZO8/s1600-h/untitled1.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 55px;" src="http://4.bp.blogspot.com/_bgdRGHjn5sE/SexUzYD47sI/AAAAAAAAACM/4ZKm8rN2ZO8/s400/untitled1.JPG" alt="" id="BLOGGER_PHOTO_ID_5326725700881280706" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;The hosting process files contain vshost in the file name and are placed in the output folder of your project.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-7200159581200291233?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/7200159581200291233/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/what-is-vshostexe-file-do.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7200159581200291233'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7200159581200291233'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/what-is-vshostexe-file-do.html' title='What is vshost.exe file do?'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_bgdRGHjn5sE/SexUdamRyCI/AAAAAAAAACE/IIwuvB5I3Ks/s72-c/untitled.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-3408656625832355496</id><published>2009-04-18T12:09:00.000-07:00</published><updated>2009-04-18T12:11:49.573-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Useful Tools'/><category scheme='http://www.blogger.com/atom/ns#' term='Website Optimizations'/><title type='text'>Cascading Style Sheets Compressors</title><content type='html'>&lt;span style="font-weight: bold;"&gt;CSS Compressors:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a style="color: rgb(51, 51, 255);" href="http://www.csscompressor.com/"&gt;http://www.csscompressor.com/&lt;/a&gt;&lt;br /&gt;&lt;a style="color: rgb(255, 102, 0);" href="http://www.cssdrive.com/index.php/main/csscompressor/"&gt;&lt;br /&gt;http://www.cssdrive.com/index.php/main/csscompressor/&lt;/a&gt;&lt;br /&gt;&lt;a style="color: rgb(51, 204, 0);" href="http://www.blumentals.net/csstool/tour.php"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;http://www.blumentals.net/csstool/tour.php&lt;/span&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-3408656625832355496?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/3408656625832355496/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/cascading-style-sheets-compressors.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/3408656625832355496'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/3408656625832355496'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/cascading-style-sheets-compressors.html' title='Cascading Style Sheets Compressors'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-2523150672897058995</id><published>2009-04-18T11:46:00.000-07:00</published><updated>2009-04-18T11:48:36.888-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='Website Optimizations'/><title type='text'>Speeding up your web site  roles.</title><content type='html'>A list of &lt;a href="http://developer.yahoo.com/performance/rules.html"&gt;&lt;span style="font-weight: bold; color: rgb(51, 51, 255);"&gt;best practices&lt;/span&gt;&lt;/a&gt; for speeding up your web site.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-2523150672897058995?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/2523150672897058995/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/speeding-up-your-web-site-roles.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/2523150672897058995'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/2523150672897058995'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/speeding-up-your-web-site-roles.html' title='Speeding up your web site  roles.'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-4996223126153652622</id><published>2009-04-18T11:07:00.000-07:00</published><updated>2009-04-18T11:09:52.726-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Useful Tools'/><category scheme='http://www.blogger.com/atom/ns#' term='Website Optimizations'/><title type='text'>Jesse Hansen's JavaScript Compressor</title><content type='html'>A JavaScript compression is one of many ways to improve performance of your Website.&lt;br /&gt;Check out Jesse Hansen's &lt;a href="http://dean.edwards.name/packer/"&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;JavaScript Compressor&lt;/span&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-4996223126153652622?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/4996223126153652622/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/jesse-hansens-javascript-compressor.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/4996223126153652622'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/4996223126153652622'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/jesse-hansens-javascript-compressor.html' title='Jesse Hansen&apos;s JavaScript Compressor'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-2175488926653728509</id><published>2009-04-17T11:00:00.000-07:00</published><updated>2009-04-17T11:02:57.651-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Useful Tools'/><title type='text'>How to make bootable USB Flash Stick</title><content type='html'>&lt;a style="color: rgb(51, 51, 255); font-weight: bold;" href="http://gocoding.com/projects/bootflashdos/BI4K6/BootFlashDos.zip"&gt;BootFlashDOS&lt;/a&gt; is a small utility that will format and make DOS bootable your USB Flash Stick.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-2175488926653728509?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/2175488926653728509/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/how-to-make-bootable-usb-flash-stick.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/2175488926653728509'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/2175488926653728509'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/how-to-make-bootable-usb-flash-stick.html' title='How to make bootable USB Flash Stick'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-6391080415471004680</id><published>2009-04-17T09:36:00.000-07:00</published><updated>2009-04-17T09:41:34.640-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Your object’s residents and Garbage Collection generations</title><content type='html'>The .Net garbage collector divides managed heap memory in to &lt;span style="font-weight: bold;font-family:courier new;font-size:130%;"  &gt;3&lt;/span&gt; parts(generations).&lt;br /&gt;&lt;br /&gt;All new heap allocated objects are placed on generation &lt;span style="font-weight: bold;font-family:georgia;font-size:130%;"  &gt;0&lt;/span&gt;.&lt;br /&gt;When generation &lt;span style="font-weight: bold;font-family:georgia;font-size:130%;"  &gt;0&lt;/span&gt; section is full the garbage collector moves the reachable (not disposed) objects into generation &lt;span style="font-weight: bold;font-family:georgia;font-size:130%;"  &gt;1&lt;/span&gt; heap area and if it's full also then garbage collector will scan generation &lt;span style="font-weight: bold;font-family:georgia;font-size:130%;"  &gt;1&lt;/span&gt;, reclaiming memory for unreachable objects and promoting reachable objects to generation &lt;span style="font-weight: bold;font-family:georgia;font-size:130%;"  &gt;2&lt;/span&gt;.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Generation &lt;span style="font-size:130%;"&gt;0&lt;/span&gt; - usually 128 - 256 KB &lt;/li&gt;&lt;li&gt;Generation &lt;span style="font-size:130%;"&gt;1&lt;/span&gt; - usually up to 2MB&lt;/li&gt;&lt;li&gt;Generation &lt;span style="font-size:130%;"&gt;2&lt;/span&gt; - usually 10 MB or larger&lt;/li&gt;&lt;/ul&gt;              Use &lt;span style="color: rgb(51, 153, 153);"&gt;System&lt;/span&gt;.GC.&lt;span style="color: rgb(51, 51, 255);"&gt;GetGeneration&lt;/span&gt; function to define generation residents of your object.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-6391080415471004680?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/6391080415471004680/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/your-objects-residents-and-garbage.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6391080415471004680'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6391080415471004680'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/your-objects-residents-and-garbage.html' title='Your object’s residents and Garbage Collection generations'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-8006716691681828580</id><published>2009-04-17T04:09:00.000-07:00</published><updated>2009-04-17T04:14:33.079-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Writing messages to the trace listeners.</title><content type='html'>&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Text;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;namespace&lt;/span&gt; ConsoleApplication1&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Program&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; Main(&lt;span style="color:blue;"&gt;string&lt;/span&gt;[] args)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            System.Diagnostics.&lt;span style="color: rgb(43, 145, 175);"&gt;Debug&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;"Hello Debug World!"&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.ReadKey();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_bgdRGHjn5sE/SehkfuyFtVI/AAAAAAAAAB8/LGEZr9b8c0E/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 184px;" src="http://2.bp.blogspot.com/_bgdRGHjn5sE/SehkfuyFtVI/AAAAAAAAAB8/LGEZr9b8c0E/s400/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5325617055662454098" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-8006716691681828580?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/8006716691681828580/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/writing-messages-to-trace-listeners.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/8006716691681828580'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/8006716691681828580'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/writing-messages-to-trace-listeners.html' title='Writing messages to the trace listeners.'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_bgdRGHjn5sE/SehkfuyFtVI/AAAAAAAAAB8/LGEZr9b8c0E/s72-c/untitled.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-7830068959943740137</id><published>2009-04-17T03:34:00.000-07:00</published><updated>2009-04-17T03:43:34.225-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Sending strings to the debugger for display</title><content type='html'>&lt;a href="http://msdn.microsoft.com/en-us/library/aa363362.aspx"&gt;&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;OutputDebugString&lt;/span&gt;&lt;/a&gt; - Sends a string to the debugger for display.&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;"stdafx.h"&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;"windows.h"&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;int&lt;/span&gt; _tmain(&lt;span style="color:blue;"&gt;int&lt;/span&gt; argc, _TCHAR* argv[])&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    OutputDebugString(_T(&lt;span style="color: rgb(163, 21, 21);"&gt;"Application started...\n"&lt;/span&gt;));&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;int&lt;/span&gt; i = 0, j = 2;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    OutputDebugString(_T(&lt;span style="color: rgb(163, 21, 21);"&gt;"Variables declaration finished...\n"&lt;/span&gt;));&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    getchar();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;return&lt;/span&gt; 0;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_bgdRGHjn5sE/Sehclxs9hCI/AAAAAAAAAB0/i6OmBkXkz54/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 196px;" src="http://3.bp.blogspot.com/_bgdRGHjn5sE/Sehclxs9hCI/AAAAAAAAAB0/i6OmBkXkz54/s400/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5325608363432444962" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Other &lt;a href="http://msdn.microsoft.com/en-us/library/ms679303%28VS.85%29.aspx"&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;Debugging Functions&lt;/span&gt;&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-7830068959943740137?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/7830068959943740137/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/sending-strings-to-debugger-for-display.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7830068959943740137'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7830068959943740137'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/sending-strings-to-debugger-for-display.html' title='Sending strings to the debugger for display'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_bgdRGHjn5sE/Sehclxs9hCI/AAAAAAAAAB0/i6OmBkXkz54/s72-c/untitled.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-4608058546004590800</id><published>2009-04-16T15:04:00.000-07:00</published><updated>2009-04-16T15:14:04.696-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Saving application's events/errors in Windows Event log</title><content type='html'>&lt;a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx"&gt;&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;EventLog&lt;/span&gt;&lt;/a&gt; object lets you access or customize Windows event logs, which record information about important software or hardware events.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Example:&lt;/span&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Text;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Diagnostics;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;namespace&lt;/span&gt; EventLogger&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Program&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt; _appSource = &lt;span style="color: rgb(163, 21, 21);"&gt;"APPLICATION_LOG"&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt; _appLogName = &lt;span style="color: rgb(163, 21, 21);"&gt;"MY_EVENT_LOG"&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;EventLog&lt;/span&gt; _logHandle = &lt;span style="color:blue;"&gt;null&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; Main(&lt;span style="color:blue;"&gt;string&lt;/span&gt;[] args)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;Program&lt;/span&gt; obj = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Program&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            obj.WriteEvent(&lt;span style="color: rgb(163, 21, 21);"&gt;"Hello World!"&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; Program()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;try&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:green;"&gt;// Create the source, if it does not already exist.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color: rgb(43, 145, 175);"&gt;EventLog&lt;/span&gt;.SourceExists(_appSource))&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    &lt;span style="color: rgb(43, 145, 175);"&gt;EventLog&lt;/span&gt;.CreateEventSource(_appSource, _appLogName);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:green;"&gt;// Create an EventLog instance and assign its source.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                _logHandle = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;EventLog&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                _logHandle.Source = _appLogName;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;catch&lt;/span&gt; { }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; WriteEvent(&lt;span style="color:blue;"&gt;string&lt;/span&gt; msg)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            _logHandle.WriteEntry(msg, &lt;span style="color: rgb(43, 145, 175);"&gt;EventLogEntryType&lt;/span&gt;.Information);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; WriteError(&lt;span style="color:blue;"&gt;string&lt;/span&gt; msg)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;             _logHandle.WriteEntry(msg, &lt;span style="color: rgb(43, 145, 175);"&gt;EventLogEntryType&lt;/span&gt;.Error);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; WriteError(&lt;span style="color: rgb(43, 145, 175);"&gt;Exception&lt;/span&gt; exp)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;StringBuilder&lt;/span&gt; msg = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;StringBuilder&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;while&lt;/span&gt; (exp != &lt;span style="color:blue;"&gt;null&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                msg.AppendLine(exp.Message);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                msg.AppendLine(exp.StackTrace);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                exp = exp.InnerException;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;            WriteError(msg.ToString());&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; WriteError(&lt;span style="color: rgb(43, 145, 175);"&gt;Exception&lt;/span&gt; exp, &lt;span style="color:blue;"&gt;string&lt;/span&gt; errorMsg)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;StringBuilder&lt;/span&gt; msg = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;StringBuilder&lt;/span&gt;(errorMsg);&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;while&lt;/span&gt; (exp != &lt;span style="color:blue;"&gt;null&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                msg.AppendLine(exp.Message);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                msg.AppendLine(exp.StackTrace);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                exp = exp.InnerException;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            WriteError(msg.ToString());&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;The output:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_bgdRGHjn5sE/SeetFbNvIHI/AAAAAAAAABk/LG8L9S5mayc/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 191px; height: 124px;" src="http://2.bp.blogspot.com/_bgdRGHjn5sE/SeetFbNvIHI/AAAAAAAAABk/LG8L9S5mayc/s400/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5325415393105223794" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_bgdRGHjn5sE/Seetmrar6VI/AAAAAAAAABs/Td_t8yWWnx8/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 394px; height: 223px;" src="http://3.bp.blogspot.com/_bgdRGHjn5sE/Seetmrar6VI/AAAAAAAAABs/Td_t8yWWnx8/s400/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5325415964390189394" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-4608058546004590800?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/4608058546004590800/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/saving-applications-eventserrors-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/4608058546004590800'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/4608058546004590800'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/saving-applications-eventserrors-in.html' title='Saving application&apos;s events/errors in Windows Event log'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_bgdRGHjn5sE/SeetFbNvIHI/AAAAAAAAABk/LG8L9S5mayc/s72-c/untitled.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-310954579601525253</id><published>2009-04-15T13:22:00.000-07:00</published><updated>2009-04-15T13:28:46.506-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>3 ways of getting .aspx file name at run-time</title><content type='html'>&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;        System.IO.&lt;span style="color: rgb(43, 145, 175);"&gt;Path&lt;/span&gt;.GetFileName(Request.PhysicalPath);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        System.Web.&lt;span style="color: rgb(43, 145, 175);"&gt;VirtualPathUtility&lt;/span&gt;.GetFileName(Request.RawUrl)&lt;/p&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;string&lt;/span&gt; sPath = System.Web.&lt;span style="color: rgb(43, 145, 175);"&gt;HttpContext&lt;/span&gt;.Current.Request.Url.AbsolutePath;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        System.IO.&lt;span style="color: rgb(43, 145, 175);"&gt;FileInfo&lt;/span&gt; oInfo = &lt;span style="color:blue;"&gt;new&lt;/span&gt; System.IO.&lt;span style="color: rgb(43, 145, 175);"&gt;FileInfo&lt;/span&gt;(sPath);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;string&lt;/span&gt; sRet = oInfo.Name;&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-310954579601525253?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/310954579601525253/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/3-ways-of-getting-aspx-file-name-at-run.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/310954579601525253'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/310954579601525253'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/3-ways-of-getting-aspx-file-name-at-run.html' title='3 ways of getting .aspx file name at run-time'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-5112112747013976594</id><published>2009-04-15T05:45:00.000-07:00</published><updated>2009-04-15T05:58:01.159-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><title type='text'>typeof &amp; instanceof</title><content type='html'>JavaScript operators:&lt;br /&gt;&lt;br /&gt;The &lt;a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Instanceof_Operator"&gt;&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;instanceof&lt;/span&gt;&lt;/a&gt; operator returns true if the specified object is of the specified object type:&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;function&lt;/span&gt; HelloPrinter()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;this&lt;/span&gt;.Print = &lt;span style="color:blue;"&gt;function&lt;/span&gt;(){&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        alert(&lt;span style="color: rgb(163, 21, 21);"&gt;'Hello'&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;var&lt;/span&gt; printer = &lt;span style="color:blue;"&gt;new&lt;/span&gt; HelloPrinter();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;printer.Print();&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;alert(printer &lt;span style="color:blue;"&gt;instanceof&lt;/span&gt; Date);         &lt;span style="color:green;"&gt;// false&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;alert(printer &lt;span style="color:blue;"&gt;instanceof&lt;/span&gt; Object);       &lt;span style="color:green;"&gt;// true&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;alert(printer &lt;span style="color:blue;"&gt;instanceof&lt;/span&gt; String);       &lt;span style="color:green;"&gt;// false&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;alert(printer &lt;span style="color:blue;"&gt;instanceof&lt;/span&gt; HelloPrinter); &lt;span style="color:green;"&gt;// true&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;br /&gt;The&lt;a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Typeof_Operator"&gt;&lt;span style="font-weight: bold; color: rgb(51, 51, 255);"&gt; typeof&lt;/span&gt;&lt;/a&gt; operator returns a string indicating the type of the unevaluated operand. operand is the string, variable, keyword, or object for which the type is to be returned:&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;var&lt;/span&gt; myFun = &lt;span style="color:blue;"&gt;new&lt;/span&gt; Function(&lt;span style="color: rgb(163, 21, 21);"&gt;"5+2"&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;var&lt;/span&gt; shape=&lt;span style="color: rgb(163, 21, 21);"&gt;"round"&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;var&lt;/span&gt; size=1&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;var&lt;/span&gt; today=&lt;span style="color:blue;"&gt;new&lt;/span&gt; Date()&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; myFun == &lt;span style="color: rgb(163, 21, 21);"&gt;'function'&lt;/span&gt;   &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; shape == &lt;span style="color: rgb(163, 21, 21);"&gt;'string'&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; size == &lt;span style="color: rgb(163, 21, 21);"&gt;'number'&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; today == &lt;span style="color: rgb(163, 21, 21);"&gt;'object'&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; dontExist == &lt;span style="color: rgb(163, 21, 21);"&gt;'undefined'&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-5112112747013976594?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/5112112747013976594/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/typeof-instanceof.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/5112112747013976594'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/5112112747013976594'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/typeof-instanceof.html' title='typeof &amp;amp; instanceof'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-3930137101237951383</id><published>2009-04-15T01:38:00.001-07:00</published><updated>2009-04-15T01:43:23.914-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Useful Tools'/><title type='text'>PInvoke Visual Studio Add-in</title><content type='html'>&lt;a style="font-weight: bold; color: rgb(51, 51, 255);" href="http://www.red-gate.com/products/pinvoke.net/index.htm"&gt;PInvoke Visual Studio Add-in&lt;/a&gt;  - Find and contribute PInvoke signatures.&lt;br /&gt;&lt;br /&gt;PInvoke.net, meaning Platform Invoke, is a wiki used by around 50,000 .NET developers a month to find and contribute PInvoke signatures (also known as Declare statements in VB), type definitions, and any other information related to calling Win32, or other unmanaged APIs in managed code (languages such as C# or VB.NET).&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_bgdRGHjn5sE/SeWeCAsjFHI/AAAAAAAAABc/7NpvksT-hfw/s1600-h/PInvoke.gif"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 281px;" src="http://3.bp.blogspot.com/_bgdRGHjn5sE/SeWeCAsjFHI/AAAAAAAAABc/7NpvksT-hfw/s400/PInvoke.gif" alt="" id="BLOGGER_PHOTO_ID_5324835891819254898" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-3930137101237951383?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/3930137101237951383/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/pinvoke-visual-studio-add-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/3930137101237951383'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/3930137101237951383'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/pinvoke-visual-studio-add-in.html' title='PInvoke Visual Studio Add-in'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_bgdRGHjn5sE/SeWeCAsjFHI/AAAAAAAAABc/7NpvksT-hfw/s72-c/PInvoke.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-7968685375182553521</id><published>2009-04-15T01:38:00.000-07:00</published><updated>2009-04-19T13:23:08.254-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Useful Tools'/><title type='text'>PInvoke Visual Studio Add-in</title><content type='html'>&lt;a style="font-weight: bold; color: rgb(51, 51, 255);" href="http://www.red-gate.com/products/pinvoke.net/index.htm"&gt;PInvoke Visual Studio Add-in&lt;/a&gt;  - Find and contribute PInvoke signatures.&lt;br /&gt;&lt;br /&gt;PInvoke.net, meaning Platform Invoke, is a wiki used by around 50,000 .NET developers a month to find and contribute PInvoke signatures (also known as Declare statements in VB), type definitions, and any other information related to calling Win32, or other unmanaged APIs in managed code (languages such as C# or VB.NET).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-7968685375182553521?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/7968685375182553521/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/pinvoke-visual-studio-add-in_15.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7968685375182553521'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7968685375182553521'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/pinvoke-visual-studio-add-in_15.html' title='PInvoke Visual Studio Add-in'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-7369107742271619677</id><published>2009-04-15T01:33:00.000-07:00</published><updated>2009-04-15T01:37:30.419-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Useful Tools'/><title type='text'>.NET Reflector</title><content type='html'>&lt;a href="http://www.red-gate.com/products/reflector/index.htm"&gt;&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;.NET Reflector&lt;/span&gt;&lt;/a&gt; enables you to easily view, explore, browse, and analyze .NET assemblies.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_bgdRGHjn5sE/SeWcoGlMmHI/AAAAAAAAABU/ZgeMArYf8_c/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 185px;" src="http://4.bp.blogspot.com/_bgdRGHjn5sE/SeWcoGlMmHI/AAAAAAAAABU/ZgeMArYf8_c/s400/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5324834347210807410" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-7369107742271619677?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/7369107742271619677/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/net-reflector.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7369107742271619677'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7369107742271619677'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/net-reflector.html' title='.NET Reflector'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_bgdRGHjn5sE/SeWcoGlMmHI/AAAAAAAAABU/ZgeMArYf8_c/s72-c/untitled.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-1421812908219937467</id><published>2009-04-14T15:25:00.000-07:00</published><updated>2009-04-14T15:51:23.275-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Plug and Play Application in C#</title><content type='html'>&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;What Is Plug and Play Application?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Plug and Play is a technology that lets applications and moduls (Dlls for instance) work together automatically.&lt;br /&gt;A user can simply add a new module ("plug it in") and begin working ("begin playing"). This should be possible even while the Application is running, without restarting it.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;How it works?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;First you need to create a Class Library project for modules interface (IPluggableModule)&lt;br /&gt;Than create another Class Library for each new module. All modules must inherit from IPluggableModule interface.&lt;br /&gt;Create a folder C:\\MyModules (a place for all modules).&lt;br /&gt;&lt;br /&gt;Use my demo ConsoleApplication1.Program to test the result.&lt;br /&gt;Each time you copy a dll module you created, it will load it and execute its action&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;PluggableModulesInterface project:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;namespace&lt;/span&gt; PluggableModulesInterface&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;interface&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IPluggableModule&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;void&lt;/span&gt; Execute();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;PluggableModuleA project:&lt;/span&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Text;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; PluggableModulesInterface;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;namespace&lt;/span&gt; PluggableModules&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModuleA&lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;IPluggableModule&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;string&lt;/span&gt; m_name=&lt;span style="color:blue;"&gt;string&lt;/span&gt;.Empty;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;void&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IPluggableModule&lt;/span&gt;.Execute()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:green;"&gt;// Module A execute implementation&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;            Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;"Module A execute implementation"&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;Test project:&lt;/span&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Text;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.IO;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Threading;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; PluggableModulesInterface;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;namespace&lt;/span&gt; ConsoleApplication1&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Program&lt;/span&gt;&lt;/p&gt;&lt;pre&gt;    {&lt;/pre&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt; App_Modules_Path = &lt;span style="color: rgb(163, 21, 21);"&gt;"C:\\MyModules"&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;bool&lt;/span&gt; isRunning = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; Time_Out = 300; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; Main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:green;"&gt;// load existing moduls&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            LoadExistingModules();&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:green;"&gt;// start the watcher thread, load a new moduls&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;Thread&lt;/span&gt; watcherThread = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Thread&lt;/span&gt;(&lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ThreadStart&lt;/span&gt;(RunWatcher));&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            watcherThread.Start();&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:green;"&gt;// start the performer thread, execute moduls each 5 seconds&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;Thread&lt;/span&gt; performerThread = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Thread&lt;/span&gt;(&lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ThreadStart&lt;/span&gt;(ExecuteModules));&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            performerThread.Start();&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;while&lt;/span&gt;(isRunning);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; RunWatcher()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;FileSystemWatcher&lt;/span&gt; modulesWatcher = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;FileSystemWatcher&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            modulesWatcher.Path = App_Modules_Path;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            modulesWatcher.Filter = &lt;span style="color: rgb(163, 21, 21);"&gt;"*.dll"&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            modulesWatcher.NotifyFilter = &lt;span style="color: rgb(43, 145, 175);"&gt;NotifyFilters&lt;/span&gt;.FileName;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            modulesWatcher.Created += &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;FileSystemEventHandler&lt;/span&gt;(OnCreated);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            modulesWatcher.Deleted += &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;FileSystemEventHandler&lt;/span&gt;(OnCreated);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:green;"&gt;// Begin watching.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            modulesWatcher.EnableRaisingEvents = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.ReadKey();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; OnCreated(&lt;span style="color:blue;"&gt;object&lt;/span&gt; source, &lt;span style="color: rgb(43, 145, 175);"&gt;FileSystemEventArgs&lt;/span&gt; e)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            LoadAssembly(e.FullPath);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; OnDeleted(&lt;span style="color:blue;"&gt;object&lt;/span&gt; source, &lt;span style="color: rgb(43, 145, 175);"&gt;RenamedEventArgs&lt;/span&gt; e)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModulesHandler&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt;&amp;gt; modulesHandler = &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModulesHandler&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt;&amp;gt;.Instance;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;for&lt;/span&gt; (&lt;span style="color:blue;"&gt;int&lt;/span&gt; i = 0; i &amp;lt; modulesHandler.Count; ++i)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;if&lt;/span&gt; (modulesHandler[i].Name == e.Name)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    modulesHandler.Remove(modulesHandler[i]);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; LoadAssembly(&lt;span style="color:blue;"&gt;string&lt;/span&gt; fileName)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;try&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = 0;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;while&lt;/span&gt; (!CanLoad(fileName) == &lt;span style="color:blue;"&gt;true&lt;/span&gt; &amp;amp;&amp;amp; counter &amp;lt; 5)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    System.Threading.&lt;span style="color: rgb(43, 145, 175);"&gt;Thread&lt;/span&gt;.Sleep(Time_Out); counter++;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;                System.Reflection.&lt;span style="color: rgb(43, 145, 175);"&gt;Assembly&lt;/span&gt; assembly = System.Reflection.&lt;span style="color: rgb(43, 145, 175);"&gt;Assembly&lt;/span&gt;.LoadFile(fileName);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt; type &lt;span style="color:blue;"&gt;in&lt;/span&gt; assembly.GetTypes())&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    &lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt; moduleInterface = type.GetInterface(&lt;span style="color: rgb(163, 21, 21);"&gt;"PluggableModulesInterface.IPluggableModule"&lt;/span&gt;);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;                    &lt;span style="color:blue;"&gt;if&lt;/span&gt; (moduleInterface != &lt;span style="color:blue;"&gt;null&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                        &lt;span style="color:green;"&gt;// add the object to the collection&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                        &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModulesHandler&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt;&amp;gt;.Instance.Add(type);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;catch&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;Exception&lt;/span&gt; exp)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:green;"&gt;// log it&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; ExecuteModules()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;try&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;while&lt;/span&gt; (isRunning)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModulesHandler&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt;&amp;gt; modulesHandler = &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModulesHandler&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt;&amp;gt;.Instance;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;                    &lt;span style="color:blue;"&gt;for&lt;/span&gt; (&lt;span style="color:blue;"&gt;int&lt;/span&gt; i = 0; i &amp;lt; modulesHandler.Count; ++i)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                        &lt;span style="color: rgb(43, 145, 175);"&gt;IPluggableModule&lt;/span&gt; module = (&lt;span style="color: rgb(43, 145, 175);"&gt;IPluggableModule&lt;/span&gt;)&lt;span style="color: rgb(43, 145, 175);"&gt;Activator&lt;/span&gt;.CreateInstance(modulesHandler[i]);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                        module.Execute();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    &lt;span style="color: rgb(43, 145, 175);"&gt;Thread&lt;/span&gt;.Sleep(5 * 1000);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;catch&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Exception&lt;/span&gt; exp)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:green;"&gt;// log it&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; LoadExistingModules()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;DirectoryInfo&lt;/span&gt; dir = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;DirectoryInfo&lt;/span&gt;(App_Modules_Path);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;FileInfo&lt;/span&gt;[] files = dir.GetFiles(&lt;span style="color: rgb(163, 21, 21);"&gt;"*.dll"&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;FileInfo&lt;/span&gt; file &lt;span style="color:blue;"&gt;in&lt;/span&gt; files)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                LoadAssembly(file.DirectoryName + &lt;span style="color: rgb(163, 21, 21);"&gt;"\\"&lt;/span&gt; + file.Name);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;bool&lt;/span&gt; CanLoad(&lt;span style="color:blue;"&gt;string&lt;/span&gt; fileName)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;try&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;if&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;File&lt;/span&gt;.Exists(fileName))&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    &lt;span style="color: rgb(43, 145, 175);"&gt;FileStream&lt;/span&gt; stream = &lt;span style="color: rgb(43, 145, 175);"&gt;File&lt;/span&gt;.Open(fileName, &lt;span style="color: rgb(43, 145, 175);"&gt;FileMode&lt;/span&gt;.Open);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    stream.Dispose();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;catch&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-1421812908219937467?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/1421812908219937467/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/plug-and-play-application-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/1421812908219937467'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/1421812908219937467'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/plug-and-play-application-in-c.html' title='Plug and Play Application in C#'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-5788077947053391098</id><published>2009-04-14T14:51:00.000-07:00</published><updated>2009-04-14T15:03:18.663-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Using Reflection to create class instance from DLL for a specific interface</title><content type='html'>An interface:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;interface&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IPluggableModule&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;void&lt;/span&gt; Execute();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;string&lt;/span&gt; Name { &lt;span style="color:blue;"&gt;get&lt;/span&gt;; &lt;span style="color:blue;"&gt;set&lt;/span&gt;;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModuleA&lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;IPluggableModule&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;string&lt;/span&gt; m_name=&lt;span style="color:blue;"&gt;string&lt;/span&gt;.Empty;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;void&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IPluggableModule&lt;/span&gt;.Execute()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:green;"&gt;// Module A execute implementation&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;"Module A execute implementation"&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;string&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IPluggableModule&lt;/span&gt;.Name&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;get&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;"PluggableModuleA"&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;set&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            m_name = &lt;span style="color:blue;"&gt;value&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;The loader:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; LoadAssembly(&lt;span style="color:blue;"&gt;string&lt;/span&gt; fileName)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;try&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        System.Reflection.&lt;span style="color: rgb(43, 145, 175);"&gt;Assembly&lt;/span&gt; assembly = System.Reflection.&lt;span style="color: rgb(43, 145, 175);"&gt;Assembly&lt;/span&gt;.LoadFile(fileName);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt; type &lt;span style="color:blue;"&gt;in&lt;/span&gt; assembly.GetTypes())&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt; moduleInterface = type.GetInterface(&lt;span style="color: rgb(163, 21, 21);"&gt;"IPluggableModule"&lt;/span&gt;);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;if&lt;/span&gt; (moduleInterface != &lt;span style="color:blue;"&gt;null&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color: rgb(43, 145, 175);"&gt;IPluggableModule&lt;/span&gt; module = (&lt;span style="color: rgb(43, 145, 175);"&gt;IPluggableModule&lt;/span&gt;)&lt;span style="color: rgb(43, 145, 175);"&gt;Activator&lt;/span&gt;.CreateInstance(type);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;                module.Execute(); &lt;span style="color:green;"&gt;// &lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;catch&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;Exception&lt;/span&gt; exp)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:green;"&gt;// log it&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-5788077947053391098?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/5788077947053391098/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/using-reflection-to-create-class.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/5788077947053391098'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/5788077947053391098'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/using-reflection-to-create-class.html' title='Using Reflection to create class instance from DLL for a specific interface'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-5791947668317181727</id><published>2009-04-14T12:11:00.000-07:00</published><updated>2009-04-25T10:19:21.996-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Multithreading'/><title type='text'>Synchronized collection class</title><content type='html'>Provides a thread-safe collection that contains objects of a type specified by the generic parameter as elements.&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModulesHandler&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;T&amp;gt; m_moduls = &lt;span style="color:blue;"&gt;null&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModulesHandler&lt;/span&gt;&amp;lt;T&amp;gt; m_instance = &lt;span style="color:blue;"&gt;null&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; syncObj = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt;();&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;private&lt;/span&gt; PluggableModulesHandler()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        m_moduls = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;T&amp;gt;();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModulesHandler&lt;/span&gt;&amp;lt;T&amp;gt; Instance&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;get&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;if&lt;/span&gt; (m_instance == &lt;span style="color:blue;"&gt;null&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;lock&lt;/span&gt; (syncObj)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    &lt;span style="color:blue;"&gt;if&lt;/span&gt; (m_instance == &lt;span style="color:blue;"&gt;null&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                        m_instance = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModulesHandler&lt;/span&gt;&amp;lt;T&amp;gt;();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;return&lt;/span&gt; m_instance;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; Add(T obj)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;lock&lt;/span&gt; (syncObj)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            m_moduls.Add(obj);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; Remove(T obj)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;lock&lt;/span&gt; (syncObj)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            m_moduls.Remove(obj);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; T &lt;span style="color:blue;"&gt;this&lt;/span&gt;[&lt;span style="color:blue;"&gt;int&lt;/span&gt; index]&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;get&lt;/span&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        { &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;lock&lt;/span&gt; (syncObj) &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            { &lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;return&lt;/span&gt; m_moduls[index]; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            } &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; Count &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    { &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;get&lt;/span&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;lock&lt;/span&gt; (syncObj) &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;return&lt;/span&gt; m_moduls.Count; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;            } &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        } &lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:8pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; Main()&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModulesHandler&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt;.Instance.Add(&lt;span style="color: rgb(163, 21, 21);"&gt;"Module A"&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModulesHandler&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt;.Instance.Add(&lt;span style="color: rgb(163, 21, 21);"&gt;"Module B"&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModulesHandler&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt;.Instance.Add(&lt;span style="color: rgb(163, 21, 21);"&gt;"Module C"&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;"Number of modules: "&lt;/span&gt; + &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModulesHandler&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt;.Instance.Count);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;"Modules:\n"&lt;/span&gt;);&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;for&lt;/span&gt; (&lt;span style="color:blue;"&gt;int&lt;/span&gt; i = 0; i &amp;lt; &lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModulesHandler&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt;.Instance.Count; ++i)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(43, 145, 175);"&gt;PluggableModulesHandler&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt;.Instance[i]);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;} &lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;NET Framework 3.5, 3.0 equivalent: &lt;a href="http://msdn.microsoft.com/en-us/library/ms668265.aspx"&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;SynchronizedCollection(T) Class&lt;/span&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-5791947668317181727?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/5791947668317181727/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/synchronized-collection-class.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/5791947668317181727'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/5791947668317181727'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/synchronized-collection-class.html' title='Synchronized collection class'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-7578986383510612942</id><published>2009-04-14T09:20:00.000-07:00</published><updated>2009-04-14T09:23:39.195-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>The way to receive command line arguments for the current process</title><content type='html'>&lt;pre&gt;&lt;span style="font-size:130%;color:blue;"&gt;    string&lt;/span&gt;&lt;span style="font-size:130%;"&gt;[] args = System.&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);font-size:130%;" &gt;Environment&lt;/span&gt;&lt;span style="font-size:130%;"&gt;.GetCommandLineArgs();&lt;/span&gt;&lt;div face="Courier New" size="10pt" color="black" style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-7578986383510612942?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/7578986383510612942/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/way-to-receive-command-line-arguments.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7578986383510612942'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7578986383510612942'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/way-to-receive-command-line-arguments.html' title='The way to receive command line arguments for the current process'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-1871484738922774256</id><published>2009-04-14T08:25:00.000-07:00</published><updated>2009-04-14T08:43:34.137-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><title type='text'>window.showModalDialog and Postbak problem</title><content type='html'>According to &lt;a href="http://msdn.microsoft.com/en-us/library/ms536759%28VS.85%29.aspx"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;MSDN&lt;/span&gt;&lt;/a&gt;, A modal dialog box retains the input focus while open. The user cannot switch windows until the dialog box is closed.&lt;br /&gt;&lt;br /&gt;A strange bug (or feature) takes place when you make a postback inside modal dialog box:&lt;br /&gt;after the postback, a page is duplicated to in a new browser window.&lt;br /&gt;&lt;br /&gt;Solution is to add&lt;span style="color:blue;"&gt; &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;base&lt;/span&gt; &lt;span style="color:red;"&gt;target&lt;/span&gt;&lt;span style="color:blue;"&gt;='_self'&lt;/span&gt; &lt;span style="color:blue;"&gt;/&amp;gt;&lt;/span&gt; inside the HEAD tag:&lt;br /&gt;&lt;pre&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;head&lt;/span&gt; &lt;span style="color:red;"&gt;runat&lt;/span&gt;&lt;span style="color:blue;"&gt;="server"&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;title&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;title&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;base&lt;/span&gt; &lt;span style="color:red;"&gt;target&lt;/span&gt;&lt;span style="color:blue;"&gt;='_self'&lt;/span&gt; &lt;span style="color:blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;head&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;&lt;/pre&gt;When you use window.showModalDialog to show content which resident inside MasterPage container or you need to add a base tag dinamicly use:&lt;br /&gt;&lt;pre&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;this&lt;/span&gt;.Page.Header.Attributes.Add(&lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;LiteralControl&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"&amp;lt;base target='_self' /&amp;gt;"&lt;/span&gt;));&lt;/p&gt;&lt;/pre&gt;in Page_Load method.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-1871484738922774256?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/1871484738922774256/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/windowshowmodaldialog-and-postbak.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/1871484738922774256'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/1871484738922774256'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/windowshowmodaldialog-and-postbak.html' title='window.showModalDialog and Postbak problem'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-202412684494649359</id><published>2009-04-13T10:53:00.000-07:00</published><updated>2009-04-13T10:55:40.934-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Useful Tools'/><title type='text'>Free online image editors</title><content type='html'>&lt;a style="color: rgb(255, 102, 0);" href="http://www.sumopaint.com/app/"&gt;http://www.sumopaint.com/app/&lt;br /&gt;&lt;/a&gt;&lt;a style="color: rgb(0, 153, 0);" href="http://www.pixlr.com/editor/"&gt;http://www.pixlr.com/editor/&lt;/a&gt;&lt;br /&gt;&lt;a style="color: rgb(51, 51, 255);" href="http://fotoflexer.com/app/"&gt;http://fotoflexer.com/app/&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-202412684494649359?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/202412684494649359/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/free-online-image-editors.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/202412684494649359'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/202412684494649359'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/free-online-image-editors.html' title='Free online image editors'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-7454757727975802575</id><published>2009-04-13T09:05:00.000-07:00</published><updated>2009-04-13T10:22:10.530-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='MS SQL'/><title type='text'>Identity insert/update workarounds</title><content type='html'>Many times during my work I needed to insert specific identity number or update an existing identity column.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Here are some solutions:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let’s create a test table:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;CREATE TABLE &lt;/span&gt;&lt;span style="font-size:85%;"&gt;[dbo].[Configuration]&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="font-size:85%;"&gt;(&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="font-size:85%;"&gt;        [Id] [int] &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;PRIMARY KEY IDENTITY&lt;/span&gt;&lt;span style="font-size:85%;"&gt;(1,1) &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;NOT NULL&lt;/span&gt;&lt;span style="font-size:85%;"&gt;,&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="font-size:85%;"&gt;        [Key] [nvarchar](50) &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;NOT NULL&lt;/span&gt;&lt;span style="font-size:85%;"&gt;,&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="font-size:85%;"&gt;     [Value] [nvarchar](&lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;max&lt;/span&gt;&lt;span style="font-size:85%;"&gt;) &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;NOT NULL&lt;/span&gt;&lt;span style="font-size:85%;"&gt;,&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="font-size:85%;"&gt;        [Description] [nvarchar](&lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;max&lt;/span&gt;&lt;span style="font-size:85%;"&gt;) &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;NULL&lt;/span&gt;&lt;span style="font-size:85%;"&gt;,&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="font-size:85%;"&gt;)&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;Insert some values:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;INSERT INTO &lt;/span&gt;&lt;span style="font-size:85%;"&gt;[Configuration]([Key], [Value], [Description])&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    &lt;span style=";font-size:85%;color:blue;"  &gt;VALUES&lt;/span&gt;&lt;span style="font-size:85%;"&gt;(&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);font-size:85%;" &gt;'Application_Thread_Timeout' &lt;/span&gt;&lt;span style="font-size:85%;"&gt;,&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);font-size:85%;" &gt;'2000' &lt;/span&gt;&lt;span style="font-size:85%;"&gt;,&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);font-size:85%;" &gt;''&lt;/span&gt;&lt;span style="font-size:85%;"&gt;)&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;INSERT INTO &lt;/span&gt;&lt;span style="font-size:85%;"&gt;[Configuration]([Key], [Value], [Description])&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                &lt;span style=";font-size:85%;color:blue;"  &gt;VALUES&lt;/span&gt;&lt;span style="font-size:85%;"&gt;(&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);font-size:85%;" &gt;'Application_Path' &lt;/span&gt;&lt;span style="font-size:85%;"&gt;,&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);font-size:85%;" &gt;'http://progfeatures.blogspot.com' &lt;/span&gt;&lt;span style="font-size:85%;"&gt;,&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);font-size:85%;" &gt;''&lt;/span&gt;&lt;span style="font-size:85%;"&gt;)&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;Here is what happens if I’ll try to insert a new config key with Id = 1000:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;INSERT INTO &lt;/span&gt;&lt;span style="font-size:85%;"&gt;[Configuration]([Id], [Key], [Value], [Description])&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                    &lt;span style=";font-size:85%;color:blue;"  &gt;VALUES&lt;/span&gt;&lt;span style="font-size:85%;"&gt;(1000,&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);font-size:85%;" &gt;'Application_Log_BackUp_File' &lt;/span&gt;&lt;span style="font-size:85%;"&gt;,&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);font-size:85%;" &gt;'SomeName' &lt;/span&gt;&lt;span style="font-size:85%;"&gt;,&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);font-size:85%;" &gt;''&lt;/span&gt;&lt;span style="font-size:85%;"&gt;)&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;p style="margin: 0px; color: rgb(0, 0, 0);"&gt;&lt;span style="font-size:85%;"&gt;-----------------------------------------------------------------&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px; color: rgb(255, 0, 0);"&gt;&lt;span style="font-size:85%;"&gt;Msg 544, Level 16, State 1, Line 2&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px; color: rgb(255, 0, 0);"&gt;&lt;span style="font-size:85%;"&gt;Cannot insert explicit value for identity column in table &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);font-size:85%;" &gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'Configuration'&lt;/span&gt; &lt;/span&gt;&lt;span style="font-size:85%;"&gt;when IDENTITY_INSERT is set to OFF.&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;The solution is to use IDENTITY_INSERT keyword:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;SET IDENTITY_INSERT &lt;/span&gt;&lt;span style="font-size:85%;"&gt;[Configuration] &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;ON&lt;/span&gt;&lt;/p&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;INSERT INTO &lt;/span&gt;&lt;span style="font-size:85%;"&gt;[Configuration]([Id], [Key], [Value], [Description])&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                 &lt;span style=";font-size:85%;color:blue;"  &gt;VALUES&lt;/span&gt;&lt;span style="font-size:85%;"&gt;(1000,&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);font-size:85%;" &gt;'Application_Log_BackUp_File' &lt;/span&gt;&lt;span style="font-size:85%;"&gt;,&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);font-size:85%;" &gt;'SomeName' &lt;/span&gt;&lt;span style="font-size:85%;"&gt;,&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);font-size:85%;" &gt;''&lt;/span&gt;&lt;span style="font-size:85%;"&gt;)&lt;/span&gt;&lt;/p&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;SET IDENTITY_INSERT &lt;/span&gt;&lt;span style="font-size:85%;"&gt;[Configuration] &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;OFF&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;How to update Identity:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Unfortunately we cannot update existing identity&lt;br /&gt;without some tricks.&lt;br /&gt;&lt;br /&gt;First trick is to save the relevant row in temporary variable,&lt;br /&gt;delete the real row, and then insert the temporary row with new identity:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;DECLARE &lt;/span&gt;&lt;span style="font-size:85%;"&gt;@Configuration &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;TABLE &lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="font-size:85%;"&gt;(&lt;/span&gt;&lt;/p&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="font-size:85%;"&gt;    [Id] &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;int&lt;/span&gt;&lt;span style="font-size:85%;"&gt;,&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="font-size:85%;"&gt;    [Key] &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;nvarchar&lt;/span&gt;&lt;span style="font-size:85%;"&gt;(50),&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="font-size:85%;"&gt;    [Value] &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;nvarchar&lt;/span&gt;&lt;span style="font-size:85%;"&gt;(&lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;max&lt;/span&gt;&lt;span style="font-size:85%;"&gt;),&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="font-size:85%;"&gt;    [Description] &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;nvarchar&lt;/span&gt;&lt;span style="font-size:85%;"&gt;(&lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;max&lt;/span&gt;&lt;span style="font-size:85%;"&gt;)&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="font-size:85%;"&gt;)&lt;/span&gt;&lt;/p&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;INSERT INTO &lt;/span&gt;&lt;span style="font-size:85%;"&gt;@Configuration &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;Select &lt;/span&gt;&lt;span style="font-size:85%;"&gt;1000, [Key], [Value], [Description] &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;FROM &lt;/span&gt;&lt;span style="font-size:85%;"&gt;[Configuration]&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style=";font-size:85%;color:blue;"  &gt;WHERE &lt;/span&gt;&lt;span style="font-size:85%;"&gt;[Configuration].[Key] &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;LIKE &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);font-size:85%;" &gt;'Application_Thread_Timeout'&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:green;"  &gt;-- Now @Configuration contains the desired row&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;SELECT &lt;/span&gt;&lt;span style="font-size:85%;"&gt;* &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;FROM &lt;/span&gt;&lt;span style="font-size:85%;"&gt;@Configuration&lt;/span&gt;&lt;/p&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;DELETE FROM &lt;/span&gt;&lt;span style="font-size:85%;"&gt;Configuration &lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style=";font-size:85%;color:blue;"  &gt;WHERE &lt;/span&gt;&lt;span style="font-size:85%;"&gt;[Configuration].[Key] &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;LIKE &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);font-size:85%;" &gt;'Application_Thread_Timeout'&lt;/span&gt;&lt;/p&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;SET IDENTITY_INSERT &lt;/span&gt;&lt;span style="font-size:85%;"&gt;[Configuration] &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;ON&lt;/span&gt;&lt;/p&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:green;"  &gt;   -- Insert a temp row with a new identity (1000)&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;   INSERT INTO &lt;/span&gt;&lt;span style="font-size:85%;"&gt;[Configuration]([Id], [Key], [Value], [Description])&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;                   Select &lt;/span&gt;&lt;span style="font-size:85%;"&gt;[Id], [Key], [Value], [Description] &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;FROM &lt;/span&gt;&lt;span style="font-size:85%;"&gt;@Configuration&lt;/span&gt;&lt;/p&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;SET IDENTITY_INSERT &lt;/span&gt;&lt;span style="font-size:85%;"&gt;[Configuration] &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;OFF&lt;/span&gt;&lt;/p&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;p style="margin: 0px;"&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;SELECT &lt;/span&gt;&lt;span style="font-size:85%;"&gt;* &lt;/span&gt;&lt;span style=";font-size:85%;color:blue;"  &gt;FROM &lt;/span&gt;&lt;span style="font-size:85%;"&gt;Configuration&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;Another trick to update Identity:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Right click on the table in the "Object Explorer"&lt;/li&gt;&lt;li&gt;Select "Design" option&lt;/li&gt;&lt;li&gt;Select the identity row and set the "Is Identity" property to false&lt;/li&gt;&lt;/ol&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_bgdRGHjn5sE/SeNwTGt3roI/AAAAAAAAABE/cxotzBWEUXY/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 112px;" src="http://1.bp.blogspot.com/_bgdRGHjn5sE/SeNwTGt3roI/AAAAAAAAABE/cxotzBWEUXY/s320/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5324222658004889218" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_bgdRGHjn5sE/SeNwzQG2cFI/AAAAAAAAABM/LSzMy1iHHGI/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 37px;" src="http://1.bp.blogspot.com/_bgdRGHjn5sE/SeNwzQG2cFI/AAAAAAAAABM/LSzMy1iHHGI/s320/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5324223210281398354" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Now you can insert/update the way you like.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-7454757727975802575?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/7454757727975802575/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/identity-insertupdate-workarounds.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7454757727975802575'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7454757727975802575'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/identity-insertupdate-workarounds.html' title='Identity insert/update workarounds'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_bgdRGHjn5sE/SeNwTGt3roI/AAAAAAAAABE/cxotzBWEUXY/s72-c/untitled.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-6434308396273206488</id><published>2009-04-12T11:13:00.000-07:00</published><updated>2009-04-12T11:34:45.755-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='MS SQL'/><title type='text'>Exception handling in SQL</title><content type='html'>Many modern languages such as C++, Java, C#, Ada and many others contains &lt;a href="http://en.wikipedia.org/wiki/Exception_handling"&gt;&lt;span style="font-weight: bold; color: rgb(51, 51, 255);"&gt;Exception handling&lt;/span&gt;&lt;/a&gt; mechanism.&lt;br /&gt;It designed to handle the occurrence of special conditions that change the normal flow of execution.&lt;br /&gt;&lt;br /&gt;Why should use we &lt;a style="color: rgb(51, 51, 255); font-weight: bold;" href="http://msdn.microsoft.com/en-us/library/ms175976.aspx"&gt;Exception handling&lt;/a&gt; in SQL?&lt;br /&gt;&lt;br /&gt;Exceptions handling mechanism can handle errors generated in stored procedures and triggers. It can be very useful for Transactions.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Here are some examples:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A divide-by-zero error:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;BEGIN TRY&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SELECT&lt;/span&gt; 1/0;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;END TRY&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;BEGIN CATCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SELECT&lt;/span&gt;&lt;br /&gt;   ERROR_NUMBER() AS ErrorNumber,&lt;br /&gt;   ERROR_SEVERITY() AS ErrorSeverity,&lt;br /&gt;   ERROR_STATE() AS ErrorState,&lt;br /&gt;   ERROR_PROCEDURE() AS ErrorProcedure,&lt;br /&gt;   ERROR_LINE() AS ErrorLine,&lt;br /&gt;   ERROR_MESSAGE() AS ErrorMessage;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;END CATCH&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Call a stored procedure which selects values from unexisting table:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;CREATE PROCEDURE&lt;/span&gt; SP_SelectUsers&lt;br /&gt;AS&lt;br /&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;select&lt;/span&gt; Users.name from Users&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;BEGIN TRY&lt;/span&gt;&lt;br /&gt; exec SP_SelectUsers&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;END TRY&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;BEGIN CATCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;  SELECT&lt;/span&gt;&lt;br /&gt;     ERROR_NUMBER() AS ErrorNumber,&lt;br /&gt;     ERROR_SEVERITY() AS ErrorSeverity,&lt;br /&gt;     ERROR_STATE() AS ErrorState,&lt;br /&gt;     ERROR_PROCEDURE() AS ErrorProcedure,&lt;br /&gt;     ERROR_LINE() AS ErrorLine,&lt;br /&gt;     ERROR_MESSAGE() AS ErrorMessage;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;END CATCH&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;TRY…CATCH block inside a transaction:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;-- insert values to unexisting table:&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;CREATE PROCEDURE&lt;/span&gt; SP_InsertTaskValue&lt;br /&gt;AS&lt;br /&gt;  &lt;span style="color: rgb(51, 51, 255);"&gt;INSERT INTO&lt;/span&gt; Tasks values('some value')&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;BEGIN TRANSACTION&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;BEGIN TRY&lt;/span&gt;&lt;br /&gt;  exec SP_InsertTaskValue&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;END TRY&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;BEGIN CATCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;   SELECT&lt;/span&gt;&lt;br /&gt;      ERROR_NUMBER() AS ErrorNumber,&lt;br /&gt;      ERROR_SEVERITY() AS ErrorSeverity,&lt;br /&gt;      ERROR_STATE() AS ErrorState,&lt;br /&gt;      ERROR_PROCEDURE() AS ErrorProcedure,&lt;br /&gt;      ERROR_LINE() AS ErrorLine,&lt;br /&gt;      ERROR_MESSAGE() AS ErrorMessage;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(51, 51, 255);"&gt;IF&lt;/span&gt; &lt;span style="color: rgb(204, 51, 204);"&gt;@@TRANCOUNT&lt;/span&gt; &gt; 0&lt;br /&gt;      &lt;span style="color: rgb(51, 51, 255);"&gt;ROLLBACK TRANSACTION&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;END CATCH&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;IF&lt;/span&gt; &lt;span style="color: rgb(204, 51, 204);"&gt;@@TRANCOUNT&lt;/span&gt; &gt; 0&lt;br /&gt;  &lt;span style="color: rgb(51, 51, 255);"&gt;COMMIT TRANSACTION&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Unfortunately, Exceptions handling has its weaknesses.&lt;br /&gt;Here are the conditions that unaffected by a TRY…CATCH construct:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Warnings or informational messages that have a severity of 10 or lower.&lt;/li&gt;&lt;li&gt;Errors that have a severity of 20 or higher that stops the SQL Server Database Engine task processing for the session. &lt;/li&gt;&lt;li&gt;Attentions, such as client-interrupt requests or broken client connections.&lt;/li&gt;&lt;li&gt;When the session is ended by a system administrator by using the KILL statement.&lt;/li&gt;&lt;li&gt;Compile errors, such as syntax errors, that prevent a batch from running.&lt;/li&gt;&lt;li&gt;Errors that occur during statement-level recompilation.&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-6434308396273206488?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/6434308396273206488/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/exception-handling-in-sql.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6434308396273206488'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6434308396273206488'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/exception-handling-in-sql.html' title='Exception handling in SQL'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-8042369910329910796</id><published>2009-04-12T10:11:00.000-07:00</published><updated>2009-04-12T10:29:08.963-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='MS SQL'/><title type='text'>Set variables with the SELECT statement</title><content type='html'>An alternative way to set variables:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;DECLARE&lt;/span&gt; @Year &lt;span style="color: rgb(51, 51, 255);"&gt;int&lt;/span&gt;, @Month &lt;span style="color: rgb(51, 51, 255);"&gt;nvarchar&lt;/span&gt;(10), @Day &lt;span style="color: rgb(51, 51, 255);"&gt;nvarchar&lt;/span&gt;(10)&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SELECT&lt;/span&gt; @Year = 2008, @Month = 'April', @Day = 'Friday'&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SELECT&lt;/span&gt; @Year as Year, @Month as Month , @Day as Day&lt;br /&gt;&lt;br /&gt;Year        Month      Day&lt;br /&gt;----------- ---------- ----------&lt;br /&gt;2008        April      Friday&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;or&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;DECLARE&lt;/span&gt; @Id &lt;span style="color: rgb(51, 51, 255);"&gt;int&lt;/span&gt;, @UserName &lt;span style="color: rgb(51, 51, 255);"&gt;nvarchar&lt;/span&gt;(15), @Password &lt;span style="color: rgb(51, 51, 255);"&gt;nvarchar&lt;/span&gt;(15)&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SELECT&lt;/span&gt; @Id = Id&lt;br /&gt;  ,@UserName = UserName&lt;br /&gt;  ,@Password = Password&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;FROM&lt;/span&gt; Users&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SELECT&lt;/span&gt; @Id ,@UserName ,@Password&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;FROM&lt;/span&gt; Users&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-8042369910329910796?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/8042369910329910796/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/set-variables-with-select-statement.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/8042369910329910796'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/8042369910329910796'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/set-variables-with-select-statement.html' title='Set variables with the SELECT statement'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-8521917532410437934</id><published>2009-04-12T07:23:00.000-07:00</published><updated>2009-04-12T07:38:04.554-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='MS SQL'/><title type='text'>Ho to modifying global configuration settings for the current server</title><content type='html'>If you need to modify global configuration settings such as: maximum server memory", &lt;br /&gt;"remote login timeout", "user maximum connections" and many others, you can use &lt;br /&gt;&lt;a style="font-weight: bold;" href="http://msdn.microsoft.com/en-us/library/ms188787%28SQL.90%29.aspx"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;sp_configure&lt;/span&gt;&lt;/a&gt; stored procedure.&lt;br /&gt;&lt;br /&gt;Basically sp_configure updates the &lt;a href="http://msdn.microsoft.com/en-us/library/ms174422.aspx"&gt;&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;sys.sysconfigures&lt;/span&gt;&lt;/a&gt; view which contains one row for each&lt;br /&gt;configuration option set by a user. sysconfigures contains the configuration options that&lt;br /&gt;are defined before the most recent startup of SQL Server, plus any dynamic configuration&lt;br /&gt;options set since then.&lt;br /&gt;&lt;br /&gt;You can run sp_configure without params to see all configuration key names.&lt;br /&gt;&lt;span style="font-size:85%;"&gt;*The &lt;a style="font-weight: bold;" href="http://msdn.microsoft.com/en-us/library/ms189631.aspx"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;list&lt;/span&gt;&lt;/a&gt; of all configuration options.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Example:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let's say that I would like to limit the maximum number of simultaneous user connections allowed on an instance of SQL Server. Therefore I need to use the "user connections option is an advanced setting. If you are using the sp_configure system stored procedure to change the setting, you can change user connections only when show advanced options is set to 1 (&lt;span style="font-size:85%;"&gt;*The setting takes effect after the server is restarted&lt;/span&gt;).&lt;br /&gt;&lt;br /&gt;The "user connections" option is an advanced setting. In order to change it you need to set "show advanced options" to 1.&lt;br /&gt;&lt;br /&gt;sp_configure 'show advanced options', 1;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms176069%28SQL.90%29.aspx"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;RECONFIGURE&lt;/span&gt;&lt;/a&gt; WITH OVERRIDE&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Then run:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: verdana;"&gt;sp_configure 'user connections', 255&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: verdana;"&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms176069%28SQL.90%29.aspx"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;RECONFIGURE&lt;/span&gt;&lt;/a&gt; WITH OVERRIDE&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Restart SQL Server for this change to take effect.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms187030.aspx"&gt;&lt;span style="font-weight: bold; color: rgb(51, 51, 255);"&gt;Another way of see and change user connections:&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;   1. In Object Explorer, right-click a server and click Properties.&lt;br /&gt;   2. Click the Connections node.&lt;br /&gt;   3. Under Connections, in the Max number of concurrent connections box, type or select a   value to set the maximum number of users that are allowed to connect simultaneously to the instance of SQL Server.&lt;br /&gt;   4. Restart SQL Server.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-8521917532410437934?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/8521917532410437934/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/ho-to-modifying-global-configuration.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/8521917532410437934'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/8521917532410437934'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/ho-to-modifying-global-configuration.html' title='Ho to modifying global configuration settings for the current server'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-7246095075964702388</id><published>2009-04-11T14:24:00.000-07:00</published><updated>2009-04-11T15:15:58.880-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='MS SQL'/><title type='text'>TRANSACTIONS and Data Integrity</title><content type='html'>That's not secret that all information system applications depend on data, and it's crucial for developers to keep it reliable and valid.&lt;br /&gt;&lt;br /&gt;&lt;a style="font-weight: bold; color: rgb(51, 51, 255);" href="http://en.wikipedia.org/wiki/Database_transaction"&gt;TRANSACTION&lt;/a&gt; is a mechanism for keeping Data Integrity and, stands for "Everything or Nothing" principal.&lt;br /&gt;It have the following properties: atomic, consistent, isolated, and durable (ACID).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Atomic&lt;/span&gt; - Either all of the operations in the transaction succeed or none of the operations persist.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Consistent&lt;/span&gt; - If the data are consistent before the transaction begins, then they will be consistent after the transaction finishes.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Isolated&lt;/span&gt; - The effects of a transaction that is in progress are hidden from all other transactions.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Durable&lt;/span&gt; - When a transaction finishes, its results are persistent and will survive a system crash.&lt;br /&gt;&lt;br /&gt;&lt;a style="font-weight: bold; color: rgb(51, 51, 255);" href="http://en.wikipedia.org/wiki/Database_transaction"&gt;TRANSACTION&lt;/a&gt; means that only if every part of operation succeeds then a transaction can be committed, or it can be rolled back if any statement fails.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When to use a TRANSACTION?&lt;br /&gt;&lt;br /&gt;Wheel it's important to use it whenever an operation must perform multiple modify actions (Insert, Update or Delete)  spatially when one action depends on other.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Example:&lt;/span&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;CREATE&lt;/span&gt; PROCEDURE SP_TransactionTest&lt;br /&gt;(&lt;br /&gt;@UserName &lt;span style="color: rgb(51, 51, 255);"&gt;nvarchar&lt;/span&gt;(20)&lt;br /&gt;,@Password &lt;span style="color: rgb(51, 51, 255);"&gt;int&lt;/span&gt;&lt;br /&gt;,@TaskId &lt;span style="color: rgb(51, 51, 255);"&gt;int&lt;/span&gt;&lt;br /&gt;)&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;AS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;BEGIN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;BEGIN TRANSACTION&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;UPDATE&lt;/span&gt; Users&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SET&lt;/span&gt; UserName = @UserName, Password = @Password&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;Where&lt;/span&gt; TaskId = @TaskId&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;INSERT INTO&lt;/span&gt; Tasks(TaskName)&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;VALUES&lt;/span&gt; ('Some Value')&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;IF&lt;/span&gt; &lt;span style="color: rgb(204, 51, 204);"&gt;@@ROWCOUNT&lt;/span&gt; &gt; 0 AND &lt;span style="color: rgb(204, 51, 204);"&gt;@@ERROR&lt;/span&gt; = 0&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;BEGIN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;COMMIT TRANSACTION&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SELECT &lt;/span&gt;1&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;END&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;ELSE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;BEGIN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;ROLLBACK &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;TRANSACTION&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SELECT &lt;/span&gt;0&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;END&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;END&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-7246095075964702388?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/7246095075964702388/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/transactions-and-data-integrity.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7246095075964702388'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7246095075964702388'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/transactions-and-data-integrity.html' title='TRANSACTIONS and Data Integrity'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-28339259940862645</id><published>2009-04-11T09:18:00.000-07:00</published><updated>2009-04-11T09:40:34.161-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='MS SQL'/><title type='text'>Prefer using IDENT_CURRENT over @@Identity and SCOPE_IDENTITY</title><content type='html'>When you look at IDENT_CURRENT, @@Identity and SCOPE_IDENTITY it seams that they do same job.&lt;br /&gt;All three functions return last-generated identity values. However each of these functions differ.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms175098.aspx"&gt;&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;IDENT_CURRENT&lt;/span&gt;&lt;/a&gt; returns the last identity value generated for a specific table in any session and any scope. The function returns NULL when the function is invoked on an empty table or on a table that has no identity column.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms187342.aspx"&gt;&lt;span style="font-weight: bold; color: rgb(51, 51, 255);"&gt;@@IDENTITY&lt;/span&gt;&lt;/a&gt; returns the last identity value generated for any table in the current session, across all scopes.&lt;br /&gt;&lt;br /&gt;&lt;a style="color: rgb(51, 51, 255); font-weight: bold;" href="http://msdn.microsoft.com/en-us/library/ms190315.aspx"&gt;SCOPE_IDENTITY&lt;/a&gt; returns the last identity value generated for any table in the current session and the current scope.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Here is a simple example:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Think about next scenario:&lt;br /&gt;My application must perform an update action each time when a new user created in the system.&lt;br /&gt;When new user is inserted into Users table its trigger executes and makes insert into Tasks table.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;code:&lt;/span&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;CREATE&lt;/span&gt; TABLE Users&lt;br /&gt;(&lt;br /&gt; Id int IDENTITY(5,1) NOT NULL,&lt;br /&gt; UserName nvarchar(15) NOT NULL,&lt;br /&gt; Password nvarchar(15) NOT NULL&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;GO&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;CREATE&lt;/span&gt; TABLE Tasks&lt;br /&gt;(&lt;br /&gt;Id int IDENTITY(1,1) NOT NULL,&lt;br /&gt;TaskName nvarchar(50) NOT NULL&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;GO&lt;br /&gt;&lt;span style="color: rgb(102, 0, 204);"&gt;CREATE&lt;/span&gt; TRIGGER Users_Trigger&lt;br /&gt;ON Users AFTER INSERT&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;INSERT&lt;/span&gt; INTO Tasks(TaskName)&lt;br /&gt;   &lt;span style="color: rgb(51, 51, 255);"&gt;VALUES&lt;/span&gt; (IDENT_CURRENT('Users'))&lt;br /&gt;&lt;br /&gt;END&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;a test query:&lt;/span&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;INSERT&lt;/span&gt; INTO Users(UserName,Password)&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;VALUES&lt;/span&gt; ('DavidLibinson','111111')&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SELECT&lt;/span&gt; &lt;span style="color: rgb(204, 51, 204);"&gt;@@Identity&lt;/span&gt; as '@@Identity'&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SELECT&lt;/span&gt; &lt;span style="color: rgb(204, 51, 204);"&gt;SCOPE_IDENTITY()&lt;/span&gt; as 'SCOPE_IDENTITY'&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SELECT&lt;/span&gt; &lt;span style="color: rgb(204, 51, 204);"&gt;IDENT_CURRENT&lt;/span&gt;('Tasks') as 'IDENT_CURRENT'&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;The result:&lt;/span&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;pre&gt; (1 row(s) affected)&lt;br /&gt;@@Identity&lt;br /&gt;---------------------------------------&lt;br /&gt;1&lt;br /&gt;&lt;br /&gt;(1 row(s) affected)&lt;br /&gt;SCOPE_IDENTITY&lt;br /&gt;---------------------------------------&lt;br /&gt;5&lt;br /&gt;&lt;br /&gt;(1 row(s) affected)&lt;br /&gt;IDENT_CURRENT&lt;br /&gt;---------------------------------------&lt;br /&gt;1&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;When you use &lt;span style="font-weight: bold;font-size:85%;" &gt;&lt;span style="color: rgb(255, 102, 0);"&gt;IDENT_CURRENT&lt;/span&gt;&lt;/span&gt; it always returns the last identity value generated for a &lt;span style="font-weight: bold;"&gt;specific&lt;/span&gt; table in any session and any scope!&lt;br /&gt;&lt;br /&gt;*Failed statements and transactions can &lt;span style="font-weight: bold;"&gt;change &lt;/span&gt;the current identity for a table and create gaps in the identity column values. The identity value &lt;span style="font-weight: bold;"&gt;is never&lt;/span&gt; rolled back even though the transaction that tried to insert the value into the table is not committed.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-28339259940862645?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/28339259940862645/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/prefer-using-identcurrent-over-identity.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/28339259940862645'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/28339259940862645'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/prefer-using-identcurrent-over-identity.html' title='Prefer using IDENT_CURRENT over @@Identity and SCOPE_IDENTITY'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-7654521969708077084</id><published>2009-04-11T06:05:00.000-07:00</published><updated>2009-04-11T06:07:06.553-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='MS SQL'/><title type='text'>How to create GUID in MS SQL</title><content type='html'>&lt;a href="http://msdn.microsoft.com/en-us/library/ms190348.aspx"&gt;&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;NewID&lt;/span&gt;&lt;/a&gt; - function creates a unique value of type uniqueidentifier.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Example:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;select NewID()&lt;br /&gt;------------------------------------&lt;br /&gt;6D4C17C3-5EBD-4261-B05C-BA1A88852F38&lt;br /&gt;&lt;br /&gt;(1 row(s) affected)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-7654521969708077084?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/7654521969708077084/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/how-to-create-guid-in-ms-sql.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7654521969708077084'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7654521969708077084'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/how-to-create-guid-in-ms-sql.html' title='How to create GUID in MS SQL'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-2069959316675202860</id><published>2009-04-11T03:19:00.000-07:00</published><updated>2009-04-11T03:44:50.618-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='MS SQL'/><title type='text'>sp_help and sp_helptext</title><content type='html'>Some times during the development process you need to see a definitions, dependencies or source code of user-defined rules (such as stored procedures, functions, views, tables, etc...).&lt;br /&gt;From my experience I can tell that sometimes it takes allot of time to find it.&lt;br /&gt;&lt;br /&gt;Fortunately, MS SQL provides a very useful stored procedures which can help us!&lt;br /&gt;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa933429%28SQL.80%29.aspx"&gt;&lt;span style="font-weight: bold; color: rgb(51, 51, 255);"&gt;sp_help&lt;/span&gt;&lt;/a&gt; - Provides information about a database object, a user-defined data type, or a data type.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Syntax&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;sp_help [ [ @objname = ] name ]&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Arguments&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;[@objname =] name&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_bgdRGHjn5sE/SeBz6hOYHjI/AAAAAAAAAA0/Ibqk3nG7rLo/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 85px;" src="http://1.bp.blogspot.com/_bgdRGHjn5sE/SeBz6hOYHjI/AAAAAAAAAA0/Ibqk3nG7rLo/s320/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5323382208740204082" border="0" /&gt;&lt;/a&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;&lt;br /&gt;You can write object name, select it and use ALT+F1 shortcut to see it's definition.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;___________________________________________________________&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms176112.aspx"&gt;&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;sp_helptext&lt;/span&gt;&lt;/a&gt; - Displays the definition (source text) of a user-defined rule, default, unencrypted Transact-SQL stored procedure, user-defined Transact-SQL function, trigger, computed column, CHECK constraint, view, or system object such as a system stored procedure.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Syntax&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;sp_helptext [ @objname = ] 'name'&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Arguments&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;[ @objname = ] 'name'&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_bgdRGHjn5sE/SeB0SAVczdI/AAAAAAAAAA8/JYe7zNgB2Lo/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 151px;" src="http://3.bp.blogspot.com/_bgdRGHjn5sE/SeB0SAVczdI/AAAAAAAAAA8/JYe7zNgB2Lo/s320/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5323382612228361682" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;font-size:85%;" &gt;You can add sp_helptext to shortcut list: go to Tools -&gt; Options, Environment -&gt; Keyboard.&lt;br /&gt;Type sp_helptext near to CTRL+F1, press OK button and restart the Microsoft SQL Server Management Studio.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_bgdRGHjn5sE/SeBx1r2f1vI/AAAAAAAAAAs/t8oFb1KdOp0/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 146px;" src="http://4.bp.blogspot.com/_bgdRGHjn5sE/SeBx1r2f1vI/AAAAAAAAAAs/t8oFb1KdOp0/s320/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5323379926670235378" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-2069959316675202860?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/2069959316675202860/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/sphelp-and-sphelptext.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/2069959316675202860'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/2069959316675202860'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/sphelp-and-sphelptext.html' title='sp_help and sp_helptext'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_bgdRGHjn5sE/SeBz6hOYHjI/AAAAAAAAAA0/Ibqk3nG7rLo/s72-c/untitled.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-1937307185009290146</id><published>2009-04-11T02:50:00.000-07:00</published><updated>2009-04-11T02:55:16.042-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='MS SQL'/><title type='text'>How to kill user processes</title><content type='html'>&lt;a href="http://msdn.microsoft.com/en-us/library/ms173730.aspx"&gt;&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;KILL&lt;/span&gt;&lt;/a&gt; function is commonly used to terminate a process that is blocking other important processes with locks, or a process that is executing a query that is using necessary system resources. &lt;span style="font-style: italic;"&gt;System processes and processes running an extended stored procedure cannot be terminated&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;In order to kill user process we must know it's &lt;span style="font-weight: bold;"&gt;session ID&lt;/span&gt; (spid).&lt;br /&gt;&lt;br /&gt;Use &lt;a href="http://msdn.microsoft.com/en-us/library/aa260384%28SQL.80%29.aspx"&gt;&lt;span style="font-weight: bold; color: rgb(51, 51, 255);"&gt;sp_who&lt;/span&gt;&lt;/a&gt; stored procedure to find information about current Microsoft® SQL Server™ users and processes.&lt;br /&gt;&lt;br /&gt;kill 51&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-1937307185009290146?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/1937307185009290146/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/how-to-kill-user-processes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/1937307185009290146'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/1937307185009290146'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/how-to-kill-user-processes.html' title='How to kill user processes'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-7174886610421228599</id><published>2009-04-10T12:04:00.001-07:00</published><updated>2009-04-10T14:43:01.190-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='OOP'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>The foundation stones of Object-Oriented Programming</title><content type='html'>Allot of people in software engineering don't know what Object-Oriented Programming stands for.&lt;br /&gt;&lt;br /&gt;Here are basic principles of &lt;a href="http://en.wikipedia.org/wiki/Object_oriented"&gt;OOP&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Encapsulation &lt;/strong&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Inheritance &lt;/strong&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Polymorphism &lt;/strong&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Generic programming &lt;/strong&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;a style="color: rgb(255, 102, 0);" href="http://en.wikipedia.org/wiki/Encapsulation_%28computer_science%29"&gt;&lt;span style="font-weight: bold;"&gt;Encapsulation&lt;/span&gt;&lt;/a&gt; is the hiding of the internal mechanisms and data structures of a software component behind a defined interface, in such a way that users of the component only need to know what the component does, and cannot make themselves dependent on the details of how it does it. The purpose is to achieve potential for change: the internal mechanisms of the component can be improved without impact on other components, or the component can be replaced with a different one that supports the same public interface.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a style="color: rgb(0, 153, 0);" href="http://en.wikipedia.org/wiki/Inheritance_%28computer_science%29"&gt;&lt;span style="font-weight: bold;"&gt;Inheritance&lt;/span&gt;&lt;/a&gt; is a way to form new classes using classes that have already been defined. The new classes, known as derived classes, take over (or inherit) attributes and behavior of the pre-existing classes, which are referred to as base classes. It is intended to help reuse existing code with little or no modification.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29"&gt;&lt;span style="font-weight: bold; color: rgb(51, 51, 255);"&gt;Polymorphism&lt;/span&gt;&lt;/a&gt; allows values of different data types to be handled using a uniform interface. The concept of parametric polymorphism applies to both data types and functions.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a style="color: rgb(102, 51, 102);" href="http://en.wikipedia.org/wiki/Generic_programming"&gt;&lt;span style="font-weight: bold;"&gt;Generic programming&lt;/span&gt;&lt;/a&gt; is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters. This approach permits writing common functions or types that differ only in the set of types on which they operate when used, thus reducing duplication.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-7174886610421228599?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/7174886610421228599/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/foundation-stones-of-object-oriented.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7174886610421228599'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7174886610421228599'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/foundation-stones-of-object-oriented.html' title='The foundation stones of Object-Oriented Programming'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-9023596880593421928</id><published>2009-04-10T05:56:00.000-07:00</published><updated>2009-04-10T15:03:13.038-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='Communication Protocols'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Win32 Serial Communication Programming</title><content type='html'>Allot of &lt;a href="http://en.wikipedia.org/wiki/Embedded_system"&gt;embedded systems&lt;/a&gt; (Windows Ce, Windows Embedded) provide an API for working with &lt;a href="http://en.wikipedia.org/wiki/Serial_communication"&gt;Serial Communication&lt;/a&gt; protocols such as &lt;a href="http://en.wikipedia.org/wiki/RS-232"&gt;RS232&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/RS-485"&gt;RS422&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/RS-485"&gt;RS485&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Initial steps for creating synchronies connection:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;At first&lt;/span&gt; we must open communications port.&lt;br /&gt;It can be made by using &lt;a href="http://msdn.microsoft.com/en-us/library/aa363858%28VS.85%29.aspx"&gt;CreateFile&lt;/a&gt; function which can create a handle to a communications resource, such as the serial port (COM1 for example).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Next&lt;/span&gt; we need to set a Baud rate. Baud rate is a measure of how fast data are moving between systems that use serial communication (most modern embedded systems transmitting at 115,200 baud and higher).&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa363436%28VS.85%29.aspx"&gt;SetCommState&lt;/a&gt; function configures a communications device according to the specifications in a device-control block.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;In addition&lt;/span&gt; it is necessary to sets the time-out parameters for all read and write operations our communications device (we don't want to wait for response forever!).&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa363437%28VS.85%29.aspx"&gt;SetCommTimeouts&lt;/a&gt; - is the right function for this.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now you can send and receive data!&lt;br /&gt;In many applications developers preferred not to close a handle after one transition.&lt;br /&gt;Single handle can be used for multiple I/O transitions.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255); font-weight: bold;"&gt;Here is a simple class for synchronies serial communications:&lt;/span&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;SerialAPI.h&lt;br /&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;#include &lt;/span&gt;&lt;span style="color:maroon;"&gt;&amp;lt;windows.h&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;class &lt;/span&gt;SerialAPI&lt;br /&gt;{&lt;br /&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt;:&lt;br /&gt;   SerialAPI(&lt;span style="color:blue;"&gt;int &lt;/span&gt;portNumber);&lt;br /&gt;   &lt;span style="color:blue;"&gt;virtual &lt;/span&gt;~SerialAPI(&lt;span style="color:blue;"&gt;void&lt;/span&gt;);&lt;br /&gt;  &lt;br /&gt;   &lt;span style="color:blue;"&gt;virtual bool    &lt;/span&gt;Write(&lt;span style="color:blue;"&gt;const void &lt;/span&gt;*buf,&lt;span style="color:blue;"&gt;int &lt;/span&gt;length);&lt;br /&gt;   &lt;span style="color:blue;"&gt;virtual bool    &lt;/span&gt;Read(&lt;span style="color:blue;"&gt;void &lt;/span&gt;*buf,&lt;span style="color:blue;"&gt;int &lt;/span&gt;length);&lt;br /&gt;   &lt;span style="color:blue;"&gt;virtual bool    &lt;/span&gt;SetBaudRate(&lt;span style="color:blue;"&gt;int &lt;/span&gt;rate);   &lt;br /&gt;   &lt;span style="color:blue;"&gt;virtual bool    &lt;/span&gt;SetTimeout(&lt;span style="color:blue;"&gt;int &lt;/span&gt;timeoutMs);&lt;br /&gt;   &lt;span style="color:blue;"&gt;virtual bool    &lt;/span&gt;Connect();&lt;br /&gt;   &lt;span style="color:blue;"&gt;virtual bool    &lt;/span&gt;Disconnect();&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:blue;"&gt;inline bool        &lt;/span&gt;IsConnected()&lt;span style="color:blue;"&gt;const&lt;/span&gt;{&lt;span style="color:blue;"&gt;return &lt;/span&gt;m_isConnected;}&lt;br /&gt;   &lt;span style="color:blue;"&gt;inline &lt;/span&gt;HANDLE    GetHandle()&lt;span style="color:blue;"&gt;const&lt;/span&gt;{&lt;span style="color:blue;"&gt;return &lt;/span&gt;m_handle;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;private&lt;/span&gt;:&lt;br /&gt;   TCHAR    m_portNumberName[15];&lt;br /&gt;   &lt;span style="color:blue;"&gt;bool    &lt;/span&gt;m_isConnected;&lt;br /&gt;   HANDLE    m_handle;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;private&lt;/span&gt;:   &lt;br /&gt;   SerialAPI(SerialAPI&amp;amp;);&lt;br /&gt;   SerialAPI&amp;amp; &lt;span style="color:blue;"&gt;operator&lt;/span&gt;=(SerialAPI&amp;amp;);&lt;br /&gt;&lt;br /&gt;};&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;br /&gt;SerialAPI.cpp&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;#include &lt;/span&gt;&lt;span style="color:maroon;"&gt;"SerialAPI.h"&lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;#include &lt;/span&gt;&lt;span style="color:maroon;"&gt;&amp;lt;stdio.h&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;SerialAPI::SerialAPI(&lt;span style="color:blue;"&gt;int &lt;/span&gt;portNumber)&lt;br /&gt;{   &lt;br /&gt;   _stprintf(m_portNumberName,_T(&lt;span style="color:maroon;"&gt;"COM%d:"&lt;/span&gt;),portNumber);&lt;br /&gt;   Connect();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;SerialAPI::~SerialAPI(&lt;span style="color:blue;"&gt;void&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;   Disconnect() ;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;bool &lt;/span&gt;SerialAPI::Write(&lt;span style="color:blue;"&gt;const void &lt;/span&gt;*buf, &lt;span style="color:blue;"&gt;int &lt;/span&gt;length)&lt;br /&gt;{&lt;br /&gt;   DWORD bufLength    = length, bytesWritten = 0;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:blue;"&gt;if &lt;/span&gt;(!WriteFile(m_handle, (LPVOID)buf, bufLength, &amp;amp;bytesWritten, 0) || length != bytesWritten)&lt;br /&gt;       &lt;span style="color:blue;"&gt;return false&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color:blue;"&gt;return true&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;bool &lt;/span&gt;SerialAPI::Read(&lt;span style="color:blue;"&gt;void &lt;/span&gt;*buf,&lt;span style="color:blue;"&gt;int &lt;/span&gt;length)&lt;br /&gt;{&lt;br /&gt;   DWORD bufLength    = length, bytesRead    = 0;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:blue;"&gt;if &lt;/span&gt;(!ReadFile(m_handle, (LPVOID)buf, bufLength, &amp;amp;bytesRead, 0) || length != bytesRead)&lt;br /&gt;       &lt;span style="color:blue;"&gt;return false&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color:blue;"&gt;return true&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;bool &lt;/span&gt;SerialAPI::SetBaudRate(&lt;span style="color:blue;"&gt;int &lt;/span&gt;rate)&lt;br /&gt;{&lt;br /&gt;   DCB PortDCB;&lt;br /&gt;   PortDCB.DCBlength = &lt;span style="color:blue;"&gt;sizeof&lt;/span&gt;(DCB);&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:blue;"&gt;if &lt;/span&gt;(!GetCommState (m_handle, &amp;amp;PortDCB))&lt;br /&gt;       &lt;span style="color:blue;"&gt;return false&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   PortDCB.BaudRate = (DWORD)rate;         &lt;br /&gt;&lt;br /&gt;   &lt;span style="color:blue;"&gt;if &lt;/span&gt;(!SetCommState(m_handle, &amp;amp;PortDCB))&lt;br /&gt;       &lt;span style="color:blue;"&gt;return false&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:blue;"&gt;return true&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;bool &lt;/span&gt;SerialAPI::SetTimeout(&lt;span style="color:blue;"&gt;int &lt;/span&gt;timeoutMs)&lt;br /&gt;{&lt;br /&gt;   COMMTIMEOUTS CommTimeouts;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:blue;"&gt;if &lt;/span&gt;(!GetCommTimeouts(m_handle, &amp;amp;CommTimeouts))&lt;br /&gt;       &lt;span style="color:blue;"&gt;return false&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   CommTimeouts.ReadTotalTimeoutConstant    = timeoutMs;   &lt;br /&gt;   CommTimeouts.WriteTotalTimeoutConstant    = timeoutMs;   &lt;br /&gt;&lt;br /&gt;   &lt;span style="color:blue;"&gt;if &lt;/span&gt;(!SetCommTimeouts(m_handle, &amp;amp;CommTimeouts))&lt;br /&gt;       &lt;span style="color:blue;"&gt;return true&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;bool &lt;/span&gt;SerialAPI::Connect()&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:blue;"&gt;if &lt;/span&gt;(m_isConnected)&lt;br /&gt;       &lt;span style="color:blue;"&gt;return false&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   m_handle = CreateFile (m_portNumberName,        &lt;span style="color:green;"&gt;// Pointer to the name of the port&lt;br /&gt;                     &lt;/span&gt;GENERIC_READ | GENERIC_WRITE,    &lt;span style="color:green;"&gt;// Access (read-write) mode&lt;br /&gt;                     &lt;/span&gt;0,                            &lt;span style="color:green;"&gt;// Share mode&lt;br /&gt;                     &lt;/span&gt;NULL,                            &lt;span style="color:green;"&gt;// Pointer to the security attribute&lt;br /&gt;                     &lt;/span&gt;OPEN_EXISTING,                &lt;span style="color:green;"&gt;// How to open the serial port&lt;br /&gt;                     &lt;/span&gt;0,                            &lt;span style="color:green;"&gt;// Port attributes&lt;br /&gt;                     &lt;/span&gt;NULL);                        &lt;span style="color:green;"&gt;// Handle to port with attribute to copy&lt;br /&gt;&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:blue;"&gt;if &lt;/span&gt;(m_handle == INVALID_HANDLE_VALUE)&lt;br /&gt;       &lt;span style="color:blue;"&gt;return false&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   m_isConnected = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:blue;"&gt;return true&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;bool &lt;/span&gt;SerialAPI::Disconnect()&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:blue;"&gt;if &lt;/span&gt;(m_isConnected)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:blue;"&gt;if &lt;/span&gt;(!CloseHandle(m_handle))&lt;br /&gt;           &lt;span style="color:blue;"&gt;return false&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;       m_handle = INVALID_HANDLE_VALUE;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   m_isConnected = &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:blue;"&gt;return true&lt;/span&gt;;&lt;br /&gt;}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-9023596880593421928?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/9023596880593421928/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/win32-serial-communication-programming.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/9023596880593421928'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/9023596880593421928'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/win32-serial-communication-programming.html' title='Win32 Serial Communication Programming'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-7986510036382149819</id><published>2009-04-09T15:47:00.000-07:00</published><updated>2009-04-25T10:17:47.998-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Multithreading'/><title type='text'>Asynchronous read operations on StandardOutput</title><content type='html'>&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;Requirement:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;An application must perform &lt;a href="http://en.wikipedia.org/wiki/Antivirus_software"&gt;Anti-Virus&lt;/a&gt; scan for all newly downloaded files.&lt;br /&gt;In order to make sure that files are not infected, the application can use &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; number of Anti-Virus software applications by using their &lt;a href="http://en.wikipedia.org/wiki/Application_programming_interface"&gt;API&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;To learn about how to redirect process standard output/input read my previos post &lt;a href="http://progfeatures.blogspot.com/2009/04/redirecting-process-standard.html"&gt;"Redirecting process standard output/input"&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;To start asynchronous read operations, you must redirect the StandardOutput stream of a Process, add your event handler to the OutputDataReceived event, and call BeginOutputReadLine.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:teal;"&gt;Scanner&lt;/span&gt;{&lt;pre&gt;&lt;div white="" none="" repeat="" scroll=""&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;   private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; _syncOutput = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt;();&lt;/p&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;   public&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; Scan()&lt;br /&gt;&lt;p style="margin: 0px;"&gt;   {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        &lt;span style="color:teal;"&gt;Process&lt;/span&gt; process = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:teal;"&gt;Process&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        process.StartInfo.Arguments = &lt;span style="color:maroon;"&gt;"C:\\GridView_Example.zip"&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        process.StartInfo.FileName = &lt;span style="color:maroon;"&gt;"F:\\ESET NOD32 Antivirus\\ecls.exe"&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;        process.StartInfo.WorkingDirectory = &lt;span style="color:maroon;"&gt;"F:\\ESET NOD32 Antivirus\\"&lt;/span&gt;;&lt;/p&gt;&lt;br /&gt;&lt;span style="color:green;"&gt;        // must be set to false to perform redirect actions&lt;/span&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        process.StartInfo.UseShellExecute = &lt;span style="color:blue;"&gt;false&lt;/span&gt;;  &lt;/p&gt;        process.StartInfo.CreateNoWindow = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        process.StartInfo.RedirectStandardOutput = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/p&gt;        process.StartInfo.RedirectStandardError = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;br /&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;        process.OutputDataReceived += &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:teal;"&gt;DataReceivedEventHandler&lt;/span&gt;(OutputHandler);&lt;/p&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;        if&lt;/span&gt; (process.Start())&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        {&lt;/p&gt;                process.BeginOutputReadLine();&lt;br /&gt;&lt;p style="margin: 0px;"&gt;                process.WaitForExit();&lt;/p&gt;        }&lt;br /&gt;&lt;p style="margin: 0px;"&gt;        process.Close();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;   }&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;   &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; OutputHandler(&lt;span style="color:blue;"&gt;object&lt;/span&gt; sendingProcess, &lt;span style="color:teal;"&gt;DataReceivedEventArgs&lt;/span&gt; outLine)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;   {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;       &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(outLine.Data))&lt;/p&gt;&lt;p style="margin: 0px;"&gt;       {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;          &lt;span style="color:blue;"&gt;lock&lt;/span&gt; (_syncOutput)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;          {&lt;/p&gt;&lt;p style="margin: 0px;"&gt;              &lt;span style="color:teal;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color:maroon;"&gt;"Output: "&lt;/span&gt; + outLine.Data);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;          }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;       }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;   }&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-7986510036382149819?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/7986510036382149819/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/asynchronous-read-operations-on.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7986510036382149819'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/7986510036382149819'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/asynchronous-read-operations-on.html' title='Asynchronous read operations on StandardOutput'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-3431587037652805707</id><published>2009-04-09T12:35:00.000-07:00</published><updated>2009-04-10T05:00:30.443-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Redirecting process standard output/input</title><content type='html'>In my experience I needed to write an application which executed &lt;a href="http://en.wikipedia.org/wiki/MS-DOS"&gt;Microsoft DOS&lt;/a&gt; &lt;a style="font-style: italic;" href="http://www.computerhope.com/msdos.htm"&gt;commands&lt;/a&gt;.&lt;br /&gt;In addition the application had to analyze DOS command's output.&lt;br /&gt;&lt;br /&gt;So basically the idea was to get a handle of command's console window, insert input and read command's output.&lt;br /&gt;&lt;br /&gt;Fortunately, System.&lt;span style="color: rgb(0, 0, 0);"&gt;Diagnostics&lt;/span&gt; namespace offers allot of system handle classes:&lt;br /&gt;System.&lt;span style="color: rgb(0, 0, 0);"&gt;Diagnostics&lt;/span&gt;.&lt;span style="color: rgb(51, 153, 153);"&gt;Process  &lt;/span&gt;and System.Diagnostics.&lt;span style="color: rgb(51, 153, 153);"&gt;ProcessStartInfo&lt;/span&gt;&lt;br /&gt;&lt;div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; width: 98%; cursor: text; line-height: 12pt; background-color: rgb(253, 252, 252);font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;pre&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;string&lt;/span&gt; command = &lt;span style="color:maroon;"&gt;"ping"&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;string&lt;/span&gt; arguments = &lt;span style="color:maroon;"&gt;"http://progfeatures.blogspot.com"&lt;/span&gt;;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;System.Diagnostics.&lt;span style="color:teal;"&gt;Process&lt;/span&gt; process = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:teal;"&gt;Process&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;process.StartInfo.FileName = &lt;span style="color:maroon;"&gt;"cmd"&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;process.StartInfo.RedirectStandardOutput = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;process.StartInfo.RedirectStandardInput = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;process.StartInfo.UseShellExecute = &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;process.StartInfo.CreateNoWindow = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;if&lt;/span&gt; (process.Start())&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;     process.StandardInput.WriteLine(command + &lt;span style="color:maroon;"&gt;" "&lt;/span&gt; + arguments);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;     process.StandardInput.Close();&lt;/p&gt;&lt;p style="margin: 0px;"&gt;     process.WaitForExit(&lt;span style="color:blue;"&gt;this&lt;/span&gt;.MaxTimeOut);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;     AnalyzeResult(process.StandardOutput.ReadToEnd());&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}         &lt;/p&gt;&lt;p style="margin: 0px;"&gt;process.Close();&lt;/p&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;When a Process writes text to its standard stream, that text is typically displayed on the console. By redirecting the StandardOutput stream, we can manipulate or suppress the output of a process.&lt;/span&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;&lt;br /&gt;&lt;br /&gt;ProcessStartInfo&lt;/span&gt;.RedirectStandardOutput Property:&lt;br /&gt;Gets or sets a value that indicates whether the output of an application is written to the &lt;span style="color: rgb(51, 153, 153);"&gt;Process&lt;/span&gt;.StandardOutput stream.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;div style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; AnalyzeResult(&lt;span style="color:blue;"&gt;string&lt;/span&gt; output)&lt;/p&gt;&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:teal;"&gt;Console&lt;/span&gt;.WriteLine(output);&lt;/p&gt;&lt;p style="margin: 0px;"&gt;    &lt;span style="color:green;"&gt;// any action...&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px; font-weight: bold;"&gt;Output result:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;Microsoft Windows XP [Version 5.1.2600]&lt;br /&gt;(C) Copyright 1985-2001 Microsoft Corp.&lt;br /&gt;&lt;br /&gt;G:\ConsoleApplication1\bin\Debug&gt;ping http://progfeatures.blogspot.com&lt;br /&gt;&lt;br /&gt;Pinging blogspot.l.google.com [209.85.227.191] with 32 bytes of data:&lt;br /&gt;&lt;br /&gt;Reply from 209.85.227.191: bytes=32 time=115ms TTL=242&lt;br /&gt;Reply from 209.85.227.191: bytes=32 time=109ms TTL=242&lt;br /&gt;Reply from 209.85.227.191: bytes=32 time=109ms TTL=242&lt;br /&gt;Reply from 209.85.227.191: bytes=32 time=109ms TTL=242&lt;br /&gt;&lt;br /&gt;Ping statistics for 209.85.227.191:&lt;br /&gt;Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),&lt;br /&gt;Approximate round trip times in milli-seconds:&lt;br /&gt;Minimum = 109ms, Maximum = 115ms, Average = 110ms&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-3431587037652805707?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/3431587037652805707/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/redirecting-process-standard.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/3431587037652805707'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/3431587037652805707'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/redirecting-process-standard.html' title='Redirecting process standard output/input'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-3036809416102606710</id><published>2009-04-09T10:27:00.000-07:00</published><updated>2009-04-09T10:31:49.416-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Running processes from my application</title><content type='html'>A simple way of running executables from C# application:&lt;br /&gt;&lt;br /&gt;System.Diagnostics.&lt;span style="color:teal;"&gt;Process&lt;/span&gt; process = &lt;span style="color:blue;"&gt;new&lt;/span&gt; System.Diagnostics.&lt;span style="color:teal;"&gt;Process&lt;/span&gt;();&lt;br /&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;process.StartInfo.Arguments = &lt;span style="color:maroon;"&gt;""&lt;/span&gt;;&lt;/p&gt;process.StartInfo.FileName = &lt;span style="color:maroon;"&gt;"calc"&lt;/span&gt;;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;process.StartInfo.WorkingDirectory = &lt;span style="color:maroon;"&gt;"C:\\WINDOWS\\system32\\"&lt;/span&gt;;&lt;/p&gt;process.StartInfo.UseShellExecute = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;br /&gt;&lt;p style="margin: 0px;"&gt; &lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;if&lt;/span&gt; (process.Start())&lt;/p&gt;&lt;p style="margin: 0px;"&gt;   process.WaitForExit();&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;process.Close();&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-3036809416102606710?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/3036809416102606710/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/running-processes-from-my-application.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/3036809416102606710'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/3036809416102606710'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/running-processes-from-my-application.html' title='Running processes from my application'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-6282918059798160838</id><published>2009-04-09T09:00:00.000-07:00</published><updated>2009-04-09T09:03:39.643-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><title type='text'>10 Tips for Writing High-Performance Web Applications</title><content type='html'>&lt;a href="http://msdn.microsoft.com/en-gb/magazine/cc163854.aspx"&gt;Useful performance tips and tricks for ASP.NET:&lt;/a&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Tip 1—Return Multiple Resultsets&lt;/li&gt;&lt;li&gt;Tip 2—Paged Data Access&lt;/li&gt;&lt;li&gt;Tip 3—Connection Pooling&lt;/li&gt;&lt;li&gt;Tip 4—ASP.NET Cache API&lt;/li&gt;&lt;li&gt;Tip 5—Per-Request Caching&lt;/li&gt;&lt;li&gt;Tip 6—Background Processing&lt;/li&gt;&lt;li&gt;Tip 7—Page Output Caching and Proxy Servers&lt;/li&gt;&lt;li&gt;Tip 8—Run IIS 6.0 (If Only for Kernel Caching)&lt;/li&gt;&lt;li&gt;Tip 9—Use Gzip Compression&lt;/li&gt;&lt;li&gt;Tip 10—Server Control View State &lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-6282918059798160838?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/6282918059798160838/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/10-tips-for-writing-high-performance.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6282918059798160838'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6282918059798160838'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/10-tips-for-writing-high-performance.html' title='10 Tips for Writing High-Performance Web Applications'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-3985425593787229210</id><published>2009-04-08T23:11:00.000-07:00</published><updated>2009-04-09T00:17:03.155-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><title type='text'>debugger - The JavaScript debugging.</title><content type='html'>Many Web developers adding &lt;span style="font-style: italic;"&gt;alert()&lt;/span&gt; snippets at strategic points in the &lt;a href="http://en.wikipedia.org/wiki/Client-side_scripting"&gt;Client-side&lt;/a&gt; code.&lt;br /&gt;Sometimes we use these prompts to see the values of certain variables as the code progresses.&lt;br /&gt;This process can take allot of time to replace the alert from section to section and reloading the page each time.&lt;br /&gt;&lt;br /&gt;In order to do debug a &lt;a href="http://en.wikipedia.org/wiki/Client-side_scripting"&gt;Client-side&lt;/a&gt; scripts you can use the "&lt;span style="color: rgb(51, 51, 255); font-weight: bold;"&gt;debugger&lt;/span&gt;" keyword to invoke the Microsoft Script Debugger or Microsoft Visual Studio programmatically in the script code.&lt;br /&gt;&lt;br /&gt;First you must disable the "Disable Script Debugging" in the Internet Explorer properties under "Advanced" section:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_bgdRGHjn5sE/Sd2S5L4dLwI/AAAAAAAAAAc/g3KijmgYoZE/s1600-h/201528-412-516.null.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 255px; height: 320px;" src="http://1.bp.blogspot.com/_bgdRGHjn5sE/Sd2S5L4dLwI/AAAAAAAAAAc/g3KijmgYoZE/s320/201528-412-516.null.jpg" alt="" id="BLOGGER_PHOTO_ID_5322571845761576706" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Insert keyword &lt;span style="color: rgb(51, 51, 255);"&gt;debugger&lt;/span&gt; in a place where debugging process should take place:&lt;br /&gt;&lt;br /&gt;&lt;div    style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:10pt;color:black;"&gt;&lt;p style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;function&lt;/span&gt; PerformLogin(){&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;            &lt;span style="font-weight: bold;color:blue;" &gt;debugger&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;             &lt;span style="color:blue;"&gt;if&lt;/span&gt;(document.getElementById(&lt;span style="color:maroon;"&gt;'iframeHolder'&lt;/span&gt;))&lt;/p&gt;&lt;p style="margin: 0px;"&gt;                     &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/p&gt;&lt;br /&gt;&lt;p style="margin: 0px;"&gt;               &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;And when an execution will hit the debugger point, a miracle will take place:&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin: 0px;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_bgdRGHjn5sE/Sd2dVuDafmI/AAAAAAAAAAk/yquSnOIWgsM/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 96px;" src="http://4.bp.blogspot.com/_bgdRGHjn5sE/Sd2dVuDafmI/AAAAAAAAAAk/yquSnOIWgsM/s320/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5322583331086958178" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-3985425593787229210?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/3985425593787229210/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/debugger-javascript-debugging.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/3985425593787229210'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/3985425593787229210'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/debugger-javascript-debugging.html' title='debugger - The JavaScript debugging.'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_bgdRGHjn5sE/Sd2S5L4dLwI/AAAAAAAAAAc/g3KijmgYoZE/s72-c/201528-412-516.null.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-6096012468336458676</id><published>2009-04-08T15:25:00.000-07:00</published><updated>2009-04-08T15:59:19.264-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Useful Tools'/><title type='text'>Programming Code formatting and highlight tool for my Blog</title><content type='html'>A great tool for posting well-formatted programming code:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.jtleigh.com/people/colin/software/CopySourceAsHtml/"&gt;CopySourceAsHtml&lt;/a&gt; - is an add-in for &lt;a href="http://en.wikipedia.org/wiki/Microsoft_Visual_Studio"&gt;Microsoft Visual Studio&lt;/a&gt; 2005/2008 that allows you to copy source code, syntax highlighting, and line numbers as HTML.&lt;br /&gt;&lt;br /&gt;After downloading and installing the &lt;a href="http://www.jtleigh.com/people/colin/software/CopySourceAsHtml/"&gt;CopySourceAsHtm&lt;/a&gt;l select, the relevant area of the code.&lt;br /&gt;If it's a client-side code (.HTML, .ASPX, .JS, etc…) click on "&lt;span style="color: rgb(204, 0, 0); font-weight: bold;"&gt;Copy As Html&lt;/span&gt;" in the "Edit" section.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_bgdRGHjn5sE/Sd0qJmG6SGI/AAAAAAAAAAM/mlbAh--fLMs/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 266px;" src="http://4.bp.blogspot.com/_bgdRGHjn5sE/Sd0qJmG6SGI/AAAAAAAAAAM/mlbAh--fLMs/s320/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5322456678958516322" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;For a server-side code simply select a code and use right-mouse click to select the "&lt;span style="color: rgb(204, 0, 0); font-weight: bold;"&gt;Copy As Html&lt;/span&gt;" menu option:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_bgdRGHjn5sE/Sd0qy-AuQ2I/AAAAAAAAAAU/A6atzPqxQ5Q/s1600-h/untitled.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 201px; height: 111px;" src="http://1.bp.blogspot.com/_bgdRGHjn5sE/Sd0qy-AuQ2I/AAAAAAAAAAU/A6atzPqxQ5Q/s320/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5322457389749650274" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-6096012468336458676?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/6096012468336458676/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/programming-code-formatting-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6096012468336458676'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/6096012468336458676'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/programming-code-formatting-and.html' title='Programming Code formatting and highlight tool for my Blog'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_bgdRGHjn5sE/Sd0qJmG6SGI/AAAAAAAAAAM/mlbAh--fLMs/s72-c/untitled.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-567117964862396379</id><published>2009-04-07T22:35:00.000-07:00</published><updated>2009-04-08T15:17:25.782-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Client-side Id of a Web Server Controls</title><content type='html'>I think that it will be fair enough to say that each Web application must perform “client side” validations before making a post-back with a new input to Server.&lt;br /&gt;In many cases when we use “Web Server Controls” the final control's ID is not the same as we gave it on the Server(especially when we use Master Pages and Web User Controls).&lt;br /&gt;&lt;br /&gt;The ClientID value is often used to programmatically access the HTML element rendered for a control in client-side script.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;em&gt;Server-side:&lt;/em&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="overflow:auto;background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;br /&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:maroon;"&gt;Content&lt;/span&gt; &lt;span style="color:red;"&gt;ID&lt;/span&gt;&lt;span style="color:blue;"&gt;="Content1"&lt;/span&gt; &lt;span style="color:red;"&gt;ContentPlaceHolderID&lt;/span&gt;&lt;span style="color:blue;"&gt;="PlaceHolder1"&lt;/span&gt; &lt;span style="color:red;"&gt;Runat&lt;/span&gt;&lt;span style="color:blue;"&gt;="Server"&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:maroon;"&gt;TextBox&lt;/span&gt; &lt;span style="color:red;"&gt;ID&lt;/span&gt;&lt;span style="color:blue;"&gt;="TXT_UserName"&lt;/span&gt; &lt;span style="color:red;"&gt;runat&lt;/span&gt;&lt;span style="color:blue;"&gt;="server"&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:maroon;"&gt;TextBox&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:maroon;"&gt;Button&lt;/span&gt; &lt;span style="color:red;"&gt;ID&lt;/span&gt;&lt;span style="color:blue;"&gt;="BTN_LogIn"&lt;/span&gt; &lt;span style="color:red;"&gt;runat&lt;/span&gt;&lt;span style="color:blue;"&gt;="server"&lt;/span&gt; &lt;span style="color:red;"&gt;OnClientClick&lt;/span&gt;&lt;span style="color:blue;"&gt;="return PerformLogin();"&lt;/span&gt; &lt;span style="color:blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt; &lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;="text/javascript"&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;        &lt;span style="color:blue;"&gt;function&lt;/span&gt; PerformLogin(){&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;if&lt;/span&gt;(document.getElementById(&lt;span style="color:maroon;"&gt;'TXT_UserName'&lt;/span&gt;).value!=&lt;span style="color:maroon;"&gt;''&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;        }&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;    &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:maroon;"&gt;Content&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;em&gt;Client-side result:&lt;/em&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="overflow:auto;background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input&lt;/span&gt; &lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;="ctl00_PlaceHolder1_TXT_UserName"&lt;/span&gt; &lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;="text"&lt;/span&gt; &lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;="ctl00$PlaceHolder1$TXT_UserName"/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input&lt;/span&gt; &lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;="ctl00_PlaceHolder1_BTN_LogIn"&lt;/span&gt; &lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;="submit"&lt;/span&gt; &lt;span style="color:red;"&gt;onclick&lt;/span&gt;&lt;span style="color:blue;"&gt;="return PerformLogin();"&lt;/span&gt; &lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=""&lt;/span&gt; &lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;="ctl00$PlaceHolder1$BTN_LogIn"/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;As you can see, ASP.NET automatically generates a unique ClientID value for each server control on a page.&lt;br /&gt;The ClientID value is generated by concatenating the ID value of the control and the UniqueID value of its parent control.&lt;br /&gt;&lt;br /&gt;Use .&lt;a href="http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx"&gt;ClientID&lt;/a&gt; property to get the real control’s Client-side ID:&lt;br /&gt;&lt;br /&gt;&lt;div style="overflow:auto;background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:Courier New;font-size:9pt;color:black;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color:blue;"&gt;function&lt;/span&gt; PerformLogin(){&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;if&lt;/span&gt;(document.getElementById(&lt;span style="color:maroon;"&gt;'&amp;lt;%=TXT_UserName.ClientID%&amp;gt;'&lt;/span&gt;).value!=&lt;span style="color:maroon;"&gt;''&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;                &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;            &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;        }&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-567117964862396379?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/567117964862396379/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/client-side-id-of-web-server-controls.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/567117964862396379'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/567117964862396379'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/client-side-id-of-web-server-controls.html' title='Client-side Id of a Web Server Controls'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-2797458611700004241</id><published>2009-04-07T12:46:00.000-07:00</published><updated>2009-04-07T12:54:13.246-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>"foreach" iterating over an Enum</title><content type='html'>The &lt;a href="http://msdn.microsoft.com/en-us/library/sbbt4032%28VS.71%29.aspx"&gt;&lt;span style="font-weight: bold;"&gt;enum&lt;/span&gt;&lt;/a&gt; keyword is used to declare an enumeration, a distinct type consisting of a set of named constants.&lt;br /&gt;&lt;br /&gt;In order to iterate over Enumeration (set of constants) we can use next functions:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;enum&lt;/span&gt; &lt;span style="color: rgb(0, 102, 0);"&gt;Days&lt;/span&gt; { Sunday, Monday, Tuesday, Wednesday };&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;class&lt;/span&gt; &lt;span style="color: rgb(0, 102, 0);"&gt;Program&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: rgb(51, 51, 255);"&gt;static void&lt;/span&gt; Main()&lt;br /&gt;    {&lt;br /&gt;            &lt;span style="color: rgb(0, 102, 0);"&gt;Array&lt;/span&gt; daysValue = &lt;span style="color: rgb(0, 102, 0);"&gt;Enum&lt;/span&gt;.GetValues(&lt;span style="color: rgb(51, 51, 255);"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(0, 102, 0);"&gt;Days&lt;/span&gt;));&lt;br /&gt;            &lt;span style="color: rgb(51, 51, 255);"&gt;foreach &lt;/span&gt;(&lt;span style="color: rgb(51, 51, 255);"&gt;int &lt;/span&gt;i &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; daysValue)&lt;br /&gt;                &lt;span style="color: rgb(0, 102, 0);"&gt;Console&lt;/span&gt;.WriteLine(i);&lt;br /&gt;&lt;br /&gt;            &lt;span style="color: rgb(51, 51, 255);"&gt;string&lt;/span&gt;[] daysName = &lt;span style="color: rgb(0, 102, 0);"&gt;Enum&lt;/span&gt;.GetNames(&lt;span style="color: rgb(51, 51, 255);"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(0, 102, 0);"&gt;Days&lt;/span&gt;));&lt;br /&gt;            &lt;span style="color: rgb(51, 51, 255);"&gt;foreach &lt;/span&gt;(&lt;span style="color: rgb(51, 51, 255);"&gt;string &lt;/span&gt;str &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; daysName)&lt;br /&gt;                &lt;span style="color: rgb(0, 102, 0);"&gt;Console&lt;/span&gt;.WriteLine(str);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-2797458611700004241?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/2797458611700004241/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/foreach-iterating-over-enum.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/2797458611700004241'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/2797458611700004241'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/foreach-iterating-over-enum.html' title='&quot;foreach&quot; iterating over an Enum'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-10455146818084740.post-4194744905068127386</id><published>2009-04-06T13:18:00.000-07:00</published><updated>2009-04-06T22:10:03.585-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Tips/Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>atexit - A way to perform tasks after the main function</title><content type='html'>In many cases programmers face a situation where application must perform task/s after the &lt;span style="font-style: italic;"&gt;main function&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;For instance&lt;/span&gt;:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Memory/Resource Deallocation&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Messaging/Notifications&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Resource/s Updates&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/tze57ck3.aspx"&gt;atexit&lt;/a&gt; - Processes the specified function at exit. &lt;span style="color: rgb(0, 0, 255);"&gt;&lt;br /&gt;&lt;br /&gt;int&lt;/span&gt; atexit( &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;(__cdecl *func )( &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; )  );&lt;br /&gt;&lt;br /&gt;Parameters:&lt;br /&gt;&lt;br /&gt;func&lt;br /&gt;  Function to be called.&lt;br /&gt;&lt;br /&gt;Return Value&lt;br /&gt;  &lt;a href="http://msdn.microsoft.com/en-us/library/tze57ck3.aspx"&gt;atexit&lt;/a&gt; returns 0 if successful, or a nonzero value if an error occurs.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Accounting to &lt;a href="http://msdn.microsoft.com/"&gt;MSDN&lt;/a&gt;:&lt;/span&gt;&lt;br /&gt;Successive calls to &lt;a href="http://msdn.microsoft.com/en-us/library/tze57ck3.aspx"&gt;atexit&lt;/a&gt; create a register of functions that are executed in last-in, first-out (&lt;a href="http://en.wikipedia.org/wiki/LIFO_%28computing%29"&gt;LIFO&lt;/a&gt;) order.&lt;br /&gt;The functions passed to atexit cannot take parameters.&lt;br /&gt;atexit use the heap to hold the register of functions.&lt;br /&gt;Thus, the number of functions that can be registered is limited only by heap memory.&lt;br /&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;An example:&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);font-size:130%;" &gt;void&lt;/span&gt;&lt;span style="font-size:130%;"&gt; foo()&lt;br /&gt;{&lt;br /&gt;  printf("Hello World after the main function!");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);font-size:130%;" &gt;void&lt;/span&gt;&lt;span style="font-size:130%;"&gt; main()&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);font-size:130%;" &gt;   ---&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:130%;"&gt;    atexit(foo);&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/blockquote&gt;If you need to perform actions on &lt;a href="http://en.wikipedia.org/wiki/Dynamic-link_library"&gt;DLL&lt;/a&gt; use &lt;a href="http://msdn.microsoft.com/en-us/library/zk17ww08.aspx"&gt;_onexit&lt;/a&gt; which is a Microsoft extension.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10455146818084740-4194744905068127386?l=progfeatures.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://progfeatures.blogspot.com/feeds/4194744905068127386/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://progfeatures.blogspot.com/2009/04/atexit-way-to-perform-tasks-after-main.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/4194744905068127386'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/10455146818084740/posts/default/4194744905068127386'/><link rel='alternate' type='text/html' href='http://progfeatures.blogspot.com/2009/04/atexit-way-to-perform-tasks-after-main.html' title='atexit - A way to perform tasks after the main function'/><author><name>David Libinson</name><uri>http://www.blogger.com/profile/13842199893858763633</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
