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

<channel>
	<title>Talking to myself.....in public</title>
	<atom:link href="http://blog.jalil.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jalil.org</link>
	<description>Virtual home in cyberspace!</description>
	<lastBuildDate>Mon, 02 Mar 2009 03:12:06 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Quick and dirty way to serialize an object to XML and back</title>
		<link>http://blog.jalil.org/2009/03/01/quick-and-dirty-way-to-serialize-an-object-to-xml-and-back/</link>
		<comments>http://blog.jalil.org/2009/03/01/quick-and-dirty-way-to-serialize-an-object-to-xml-and-back/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 03:09:03 +0000</pubDate>
		<dc:creator>Jalil Vaidya</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Serialization]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://blog.jalil.org/?p=29</guid>
		<description><![CDATA[A lot of time I just want to quickly look at how an object graph will be serialized to XML. I end up writing quick and dirty code like below time after time since I cannot find the code snippet I had written a while back. I am documenting it here so I do not [...]]]></description>
				<content:encoded><![CDATA[<p>A lot of time I just want to quickly look at how an object graph will be serialized to XML. I end up writing quick and dirty code like below time after time since I cannot find the code snippet I had written a while back. I am documenting it here so I do not have to write it again when I need it next time.</p>
<pre class="csharp">&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> SerUtils
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> SerializeToString&lt;T&gt;<span style="color: #000000;">&#40;</span>T source<span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span> == source<span style="color: #000000;">&#41;</span>
	    <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>;
&nbsp;
    XmlSerializer ser = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> XmlSerializer<span style="color: #000000;">&#40;</span>source.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
    MemoryStream ms = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> MemoryStream<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    ser.<span style="color: #0000FF;">Serialize</span><span style="color: #000000;">&#40;</span>ms, source<span style="color: #000000;">&#41;</span>;			
    ms.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    ms.<span style="color: #0000FF;">Position</span> = <span style="color: #FF0000;">0</span>;
    StreamReader sr = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StreamReader<span style="color: #000000;">&#40;</span>ms<span style="color: #000000;">&#41;</span>;
    <span style="color: #FF0000;">string</span> s = sr.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #0600FF;">return</span> s;
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> T DeserializeFromString&lt;T&gt;<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> xml<span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span> == xml<span style="color: #000000;">&#41;</span>
	    <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">default</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span>;
&nbsp;
    XmlSerializer ser = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> XmlSerializer<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
    MemoryStream ms = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> MemoryStream<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    StreamWriter sw = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StreamWriter<span style="color: #000000;">&#40;</span>ms<span style="color: #000000;">&#41;</span>;
    sw.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>xml<span style="color: #000000;">&#41;</span>;
    sw.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    ms.<span style="color: #0000FF;">Position</span> = <span style="color: #FF0000;">0</span>;
    <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span> ser.<span style="color: #0000FF;">Deserialize</span><span style="color: #000000;">&#40;</span>ms<span style="color: #000000;">&#41;</span>;
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<p>Don't use this in production!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jalil.org/2009/03/01/quick-and-dirty-way-to-serialize-an-object-to-xml-and-back/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting declared type of a NulllableType variable</title>
		<link>http://blog.jalil.org/2009/03/01/getting-declared-type-for-nulllabletype-variable/</link>
		<comments>http://blog.jalil.org/2009/03/01/getting-declared-type-for-nulllabletype-variable/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 02:46:42 +0000</pubDate>
		<dc:creator>Jalil Vaidya</dc:creator>
				<category><![CDATA[.NET Framework]]></category>

		<guid isPermaLink="false">http://blog.jalil.org/?p=20</guid>
		<description><![CDATA[If the GetType method is called on a variable of NullableType then you will get the underlying type instead of the NullableType. This is because of boxing that occurs when the type object is converted to Object as documented at MSDN. So how do you get the declared type if you have a variable of [...]]]></description>
				<content:encoded><![CDATA[<p>If the GetType method is called on a variable of <a href="http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx">NullableType</a> then you will get the underlying type instead of the NullableType. This is because of boxing that occurs when the type object is converted to Object as documented at <a href="http://msdn.microsoft.com/en-us/library/ms366789.aspx">MSDN</a>. So how do you get the declared type if you have a variable of NullableType? Here is the code that will return the Type object of the declared type of the NullableType variable.</p>
<pre class="csharp">&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> Type GetUnderlyingType&lt;T&gt;<span style="color: #000000;">&#40;</span>T t<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<p>The code above uses <a href="http://msdn.microsoft.com/en-us/library/512aeb7t(VS.80).aspx">Generics</a> and <a href="http://msdn.microsoft.com/en-us/library/58918ffs(VS.80).aspx">typeof</a> operator to obtain the underlying NullableType. The trick here is to call typeof on the type itself and not the variable. This is not usually possible when you just have a variable but the power of Generics allows you to do so. So, say if you have declared a variable of NullableType int
<pre class="csharp"><span style="color: #FF0000;">int</span>? i;</pre>
<p> the code in GetUnderlyingType method above does the equivalent of
<pre class="csharp"><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span>?<span style="color: #000000;">&#41;</span></pre>
<p> to return the underlying NullableType. To further understand the difference between calling GetType on a variable and using typeof operator on the Type itself, look at the following code.</p>
<pre class="csharp">&nbsp;
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> PrintType&lt;T&gt;<span style="color: #000000;">&#40;</span>T t<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;{0}, {1}&quot;</span>, t.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FullName</span>, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FullName</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<p>Calling GetType on variable t returns the underlying type where as using typeof operator on Generic type parameter T returns the declared type of variable t.<br />
 Given a variable we can further test if the variable holds a NullableType by using the following code.</p>
<pre class="csharp">&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> IsNullable&lt;T&gt;<span style="color: #000000;">&#40;</span>T t<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Type tx = GetUnderlyingType<span style="color: #000000;">&#40;</span>t<span style="color: #000000;">&#41;</span>;
            <span style="color: #0600FF;">return</span> tx.<span style="color: #0000FF;">IsGenericType</span> &amp;&amp; tx.<span style="color: #0000FF;">GetGenericTypeDefinition</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> == <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>Nullable&lt;&gt;<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<p>The code above obtains the underlying type first and then tests if the type is Generic, and if it is Generic type then it tests for NullableType. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jalil.org/2009/03/01/getting-declared-type-for-nulllabletype-variable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How Windows CRT Checks For Managed Module</title>
		<link>http://blog.jalil.org/2008/03/09/how-windows-crt-checks-for-managed-module/</link>
		<comments>http://blog.jalil.org/2008/03/09/how-windows-crt-checks-for-managed-module/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 03:59:17 +0000</pubDate>
		<dc:creator>Jalil Vaidya</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[VS2005]]></category>
		<category><![CDATA[VS2008]]></category>

		<guid isPermaLink="false">http://blog.jalil.org/2008/03/09/how-windows-crt-checks-for-managed-module/</guid>
		<description><![CDATA[While debugging an issue I came across an interesting piece of code in Windows CRT source. So far I have been using the code below to determine if an executable is managed or native. Basically I am just checking for the presence of IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR Data Directory entry in PE headers. &#160; int CheckCLRHeader&#40;PBYTE pbFile&#41; &#123; [...]]]></description>
				<content:encoded><![CDATA[<p>While debugging an issue I came across an interesting piece of code in Windows CRT source. So far I have been using the code below to determine if an executable is managed or native. Basically I am just checking for the presence of IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR Data Directory entry in PE headers.</p>
<pre class="cpp">&nbsp;
<span style="color: #0000ff;">int</span> CheckCLRHeader<span style="color: #000000;">&#40;</span>PBYTE pbFile<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    PIMAGE_DOS_HEADER pDOSHeader;
    PIMAGE_NT_HEADERS pNTHeader;
    PIMAGE_OPTIONAL_HEADER32 pNTHeader32;
    PIMAGE_OPTIONAL_HEADER64 pNTHeader64;
    PIMAGE_DATA_DIRECTORY pDataDirectory;
&nbsp;
	<span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">NULL</span> == pbFile<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> &lt;&lt; <span style="color: #666666;">&quot;Invalid file&quot;</span> &lt;&lt; endl;
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	pDOSHeader = <span style="color: #0000ff;">reinterpret_cast</span>
&lt; PIMAGE_DOS_HEADER &gt;<span style="color: #000000;">&#40;</span>pbFile<span style="color: #000000;">&#41;</span>;
	<span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span>IMAGE_DOS_SIGNATURE != pDOSHeader-&gt;e_magic<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> &lt;&lt; <span style="color: #666666;">&quot;Cannot find DOS header. Not an executable file&quot;</span> &lt;&lt; endl;
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	pNTHeader = ImageNtHeader<span style="color: #000000;">&#40;</span>pbFile<span style="color: #000000;">&#41;</span>;
	<span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">NULL</span> == pNTHeader<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> &lt;&lt; <span style="color: #666666;">&quot;Cannot find PE header. Invalid or corrupt file&quot;</span> &lt;&lt; endl;
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span>IMAGE_NT_SIGNATURE != pNTHeader-&gt;Signature<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> &lt;&lt; <span style="color: #666666;">&quot;Cannot fine PE signature. Invalid or corrupt file&quot;</span> &lt;&lt; endl;
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">switch</span><span style="color: #000000;">&#40;</span>pNTHeader-&gt;OptionalHeader.<span style="color: #00eeff;">Magic</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0000ff;">case</span> IMAGE_NT_OPTIONAL_HDR32_MAGIC:
			pNTHeader32 = <span style="color: #0000ff;">reinterpret_cast</span>
&lt; PIMAGE_OPTIONAL_HEADER32 &gt;<span style="color: #000000;">&#40;</span>&amp;pNTHeader-&gt;OptionalHeader<span style="color: #000000;">&#41;</span>;
			<span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span>pNTHeader32-&gt;NumberOfRvaAndSizes &lt;= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0000dd;">cout</span> &lt;&lt; <span style="color: #666666;">&quot;No CLR Data Dictionary. Not a Managed Assembly&quot;</span> &lt;&lt; endl;
				<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
			<span style="color: #000000;">&#125;</span>
			pDataDirectory = pNTHeader32-&gt;DataDirectory;
			<span style="color: #0000ff;">break</span>;
&nbsp;
		<span style="color: #0000ff;">case</span> IMAGE_NT_OPTIONAL_HDR64_MAGIC:
			pNTHeader64 = <span style="color: #0000ff;">reinterpret_cast</span>
&lt; PIMAGE_OPTIONAL_HEADER64 &gt;<span style="color: #000000;">&#40;</span>&amp;pNTHeader-&gt;OptionalHeader<span style="color: #000000;">&#41;</span>;
			<span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span>pNTHeader64-&gt;NumberOfRvaAndSizes &lt;= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0000dd;">cout</span> &lt;&lt; <span style="color: #666666;">&quot;No CLR Data Dictionary. Not a Managed Assembly&quot;</span> &lt;&lt; endl;
				<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
			<span style="color: #000000;">&#125;</span>
			pDataDirectory = pNTHeader64-&gt;DataDirectory;
			<span style="color: #0000ff;">break</span>;
&nbsp;
		<span style="color: #0000ff;">default</span>:
			<span style="color: #0000dd;">cout</span> &lt;&lt; <span style="color: #666666;">&quot;Invalid NT header. Invalid or corrupt file&quot;</span> &lt;&lt; endl;
			<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">NULL</span> == pDataDirectory<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> &lt;&lt; <span style="color: #666666;">&quot;Cannot find data directories. Invalid or corrupt file&quot;</span> &lt;&lt; endl;
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0000dd;">0</span> == pDataDirectory<span style="color: #000000;">&#91;</span>IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">VirtualAddress</span> ||
		<span style="color: #0000dd;">0</span> == pDataDirectory<span style="color: #000000;">&#91;</span>IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">Size</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> &lt;&lt; <span style="color: #666666;">&quot;COM Data Directory not found. Not a Managed Assembly&quot;</span> &lt;&lt; endl;
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> &lt;&lt; <span style="color: #666666;">&quot;Managed Assembly&quot;</span> &lt;&lt; endl;
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<p>The code for check_managed_app function in crtexe.c is similar but slightly different in how it checks for the same IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR but only checks for presence of VirtualAddress.</p>
<pre class="cpp">&nbsp;
<span style="color: #ff0000; font-style: italic;">/***
*check_managed_app() - Check for a managed executable
*
*Purpose:
*       Determine if the EXE the startup code is linked into is a managed app
*       by looking for the COM Runtime Descriptor in the Image Data Directory
*       of the PE or PE+ header.
*
*Entry:
*       None
*
*Exit:
*       1 if managed app, 0 if not.
*
*Exceptions:
*
*******************************************************************************/</span>
&nbsp;
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> __cdecl check_managed_app <span style="color: #000000;">&#40;</span>
        <span style="color: #0000ff;">void</span>
        <span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
        PIMAGE_DOS_HEADER pDOSHeader;
        PIMAGE_NT_HEADERS pPEHeader;
        PIMAGE_OPTIONAL_HEADER32 pNTHeader32;
        PIMAGE_OPTIONAL_HEADER64 pNTHeader64;
&nbsp;
        pDOSHeader = <span style="color: #000000;">&#40;</span>PIMAGE_DOS_HEADER<span style="color: #000000;">&#41;</span>&amp;amp;__ImageBase;
        <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span> pDOSHeader-&gt;e_magic != IMAGE_DOS_SIGNATURE <span style="color: #000000;">&#41;</span>
            <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
&nbsp;
        pPEHeader = <span style="color: #000000;">&#40;</span>PIMAGE_NT_HEADERS<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">char</span> *<span style="color: #000000;">&#41;</span>pDOSHeader +
                                        pDOSHeader-&gt;e_lfanew<span style="color: #000000;">&#41;</span>;
        <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span> pPEHeader-&gt;Signature != IMAGE_NT_SIGNATURE <span style="color: #000000;">&#41;</span>
            <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
&nbsp;
        pNTHeader32 = <span style="color: #000000;">&#40;</span>PIMAGE_OPTIONAL_HEADER32<span style="color: #000000;">&#41;</span>&amp;pPEHeader-&gt;OptionalHeader;
        <span style="color: #0000ff;">switch</span> <span style="color: #000000;">&#40;</span> pNTHeader32-&gt;Magic <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0000ff;">case</span> IMAGE_NT_OPTIONAL_HDR32_MAGIC:
            <span style="color: #ff0000; font-style: italic;">/* PE header */</span>
            <span style="color: #ff0000; font-style: italic;">/* prefast assumes we are overflowing __ImageBase */</span>
<span style="color: #339900;">#pragma warning(push)</span>
<span style="color: #339900;">#pragma warning(disable:26000)</span>
            <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span> pNTHeader32-&gt;NumberOfRvaAndSizes &lt;=
                    IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR <span style="color: #000000;">&#41;</span>
                <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
<span style="color: #339900;">#pragma warning(pop)</span>
            <span style="color: #0000ff;">return</span> !! pNTHeader32 -&gt;
                      DataDirectory<span style="color: #000000;">&#91;</span>IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR<span style="color: #000000;">&#93;</span> .
                      <span style="color: #00eeff;">VirtualAddress</span>;
        <span style="color: #0000ff;">case</span> IMAGE_NT_OPTIONAL_HDR64_MAGIC:
            <span style="color: #ff0000; font-style: italic;">/* PE+ header */</span>
            pNTHeader64 = <span style="color: #000000;">&#40;</span>PIMAGE_OPTIONAL_HEADER64<span style="color: #000000;">&#41;</span>pNTHeader32;
            <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span> pNTHeader64-&gt;NumberOfRvaAndSizes &lt;=
                    IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR <span style="color: #000000;">&#41;</span>
                <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
            <span style="color: #0000ff;">return</span> !! pNTHeader64 -&gt;
                      DataDirectory<span style="color: #000000;">&#91;</span>IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR<span style="color: #000000;">&#93;</span> .
                      <span style="color: #00eeff;">VirtualAddress</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #ff0000; font-style: italic;">/* Not PE or PE+, so not managed */</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<p>I came across this code while debugging past the main() function. I thought this might be a new piece of code in CRT source that comes with VS2008 only to be proven wrong when searching through the CRT source that comes with all versions of Visual Studio starting from VS2002. Another interesting discovery (at least for me) is that similar code is in various CRT source files. In VS2008 CRT source the files crt0dat.c, crt0.c and crtexe.c contain check_managed_app function in Win32 CRT and crt0dat.c in WinCE CRT. In VS2005 the same function is in crt0.c, crt0dat.c and crtexe.c. In VS2003 and VS2002 it is in crt0.c and crtexe.c. There are other gems of code in CRT source waiting to be discovered!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jalil.org/2008/03/09/how-windows-crt-checks-for-managed-module/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to determine event subscribers</title>
		<link>http://blog.jalil.org/2008/02/18/how-to-determine-event-subscribers/</link>
		<comments>http://blog.jalil.org/2008/02/18/how-to-determine-event-subscribers/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 22:32:29 +0000</pubDate>
		<dc:creator>Jalil Vaidya</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[VS2005]]></category>
		<category><![CDATA[VS2008]]></category>

		<guid isPermaLink="false">http://blog.jalil.org/2008/02/18/how-to-determine-event-subscribers/</guid>
		<description><![CDATA[While debugging a reasonably complex .NET application, many times you may come across a situation where you want to know which different methods have subscribed to your event. If you are using VS2005 or VS2008 then DebuggerTypeProxyAttribute, a relatively unknown feature of Visual Studio, comes to your rescue. For a long time Visual Studio has [...]]]></description>
				<content:encoded><![CDATA[<p>While debugging a reasonably complex .NET application, many times you may come across a situation where you want to know which different methods have subscribed to your event. If you are using VS2005 or VS2008 then <a href="http://msdn2.microsoft.com/en-us/library/d8eyd8zc.aspx" title="Using DebuggerTypeProxy Attribute" target="_blank">DebuggerTypeProxyAttribute</a>, a relatively unknown feature of Visual Studio, comes to your rescue. For a long time Visual Studio has allowed developers to customize the information about the variable that is displayed in the IDE during debugging. Before VS2005 the customization was done using <a href="http://msdn2.microsoft.com/en-us/library/zf0e8s14(VS.71).aspx" title="Displaying Elements of a Custom Data Type" target="_blank">mcee_cs.dat</a> (C#), mcee_mc.dat (MC++), and autoexp.dat (C++) files. Starting with VS2005, this customization is taken to next level. VS2005 introduces two new attributes, <a href="http://msdn2.microsoft.com/en-us/library/system.diagnostics.debuggerdisplayattribute.aspx" title="DebuggerDisplayAttribute Class" target="_blank">DebuggerDisplayAttribute</a> and <a href="http://msdn2.microsoft.com/en-us/library/ms228992.aspx" title="Enhancing Debugging with the Debugger Display Attributes" target="_blank">DebuggerTypeProxyAttribute</a>, that allows you to change the information displayed in the Visual Studio debugger. Typically one would apply these attributes to the class in the code which the VS debugger will use to display information. However, there is a trick in which you can use these attributes outside of the original code. This means that you can change the information displayed about ANY class, even the classes that you did not write yourself. VS2005 and above use AutoExp.dll which provides display information about various types to the debugger. The code for AutoExp.dll is provided in <a href="http://msdn2.microsoft.com/en-us/library/x810d419.aspx" title="Using DebuggerDisplay Attribute" target="_blank">AutoExp.cs</a> file with the Visual Studio installation in Common7\Packages\Debugger\Visualizers\Original\ folder. You can add your own code to this file, compile the AutoExp.dll, and place it in either the Common7\Packages\Debugger\Visualizers folder or in the My Documents\Visual Studio 2005 (or Visual Studio 2008)\Visualizers folder. I learned about this trick a while back from <a href="http://www.wintellect.com/cs/blogs/jrobbins/default.aspx" title="John Robbins' Blog" target="_blank">John Robbins'</a> excellent book <a href="http://www.bookpool.com/sm/0735622027" title="Debugging Microsoft .NET 2.0 Applications, 3rd Edition" target="_blank">Debugging .NET 2.0 Applications</a>. It turns out that Visual Studio loads all the valid assemblies from the Visualizers folder, and if the assembly contains Visualizer or any debugger related objects then it will use it during the debugging session. Now back to the topic of this post; to easily display which methods have subscribed to an event I wrote a class called MulticastDelegateDebuggerProxy for MulticastDelegate class. Since, all the events are delegates that are derived from MulticastDelegate class, my MulticastDelegateDebuggerProxy class will be used for all the events. The DebuggerTypeProxyAttribute is used at the assembly level to tell Visual Studio debugger that MulticastDelegateDebuggerProxy class is the proxy for MulticastDelegate class. The code for MulticastDelegateDebuggerProxy is listed below.</p>
<pre class="csharp">&nbsp;
<span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>;
<span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Diagnostics</span>;
<span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>;
<span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Text</span>;
&nbsp;
<span style="color: #000000;">&#91;</span>assembly: DebuggerTypeProxy<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>MulticastDelegateDebuggerProxy<span style="color: #000000;">&#41;</span>,
		Target = <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>MulticastDelegate<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MulticastDelegateDebuggerProxy
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> m_methods;
&nbsp;
	<span style="color: #0600FF;">public</span> MulticastDelegateDebuggerProxy<span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">MulticastDelegate</span> del<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		m_methods = GetMethodList<span style="color: #000000;">&#40;</span>del<span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#91;</span>DebuggerDisplay<span style="color: #000000;">&#40;</span><span style="color: #808080;">@"Subscribers:\{{Methods.Length}}&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> Methods
	<span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> m_methods; <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> GetMethodList<span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">MulticastDelegate</span> myEvent<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #FF0000;">string</span> retType = myEvent.<span style="color: #0000FF;">Method</span>.<span style="color: #0000FF;">ReturnType</span>.<span style="color: #0000FF;">Name</span>;
&nbsp;
		<span style="color: #FF0000;">Delegate</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> delegates = myEvent.<span style="color: #0000FF;">GetInvocationList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> methods = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span>delegates.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#93;</span>;
&nbsp;
		<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i = <span style="color: #FF0000;">0</span>; i &amp;lt; delegates.<span style="color: #0000FF;">Length</span>; ++i<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #FF0000;">Delegate</span> del = delegates<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>;
			MethodInfo m = del.<span style="color: #0000FF;">Method</span>;
			<span style="color: #FF0000;">string</span> accessModifier =
				m.<span style="color: #0000FF;">IsPrivate</span> ? <span style="color: #808080;">&quot;private&quot;</span> : <span style="color: #000000;">&#40;</span>m.<span style="color: #0000FF;">IsPublic</span> ? <span style="color: #808080;">&quot;public&quot;</span> : <span style="color: #000000;">&#40;</span>m.<span style="color: #0000FF;">IsFamily</span> ? <span style="color: #808080;">&quot;protected&quot;</span> : <span style="color: #808080;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			StringBuilder sb = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>ParameterInfo param <span style="color: #0600FF;">in</span> m.<span style="color: #0000FF;">GetParameters</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>param.<span style="color: #0000FF;">ParameterType</span>.<span style="color: #0000FF;">FullName</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot; &quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>param.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;,&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>sb.<span style="color: #0000FF;">Length</span> &amp;gt; <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				sb.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span>sb.<span style="color: #0000FF;">Length</span> - <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #FF0000;">string</span> isStatic = m.<span style="color: #0000FF;">IsStatic</span> ? <span style="color: #808080;">&quot;static&quot;</span> : <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>;
			<span style="color: #FF0000;">string</span> isVirtual = m.<span style="color: #0000FF;">IsVirtual</span> ?
				<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>m.<span style="color: #0000FF;">Attributes</span> &amp;amp; MethodAttributes.<span style="color: #0000FF;">NewSlot</span><span style="color: #000000;">&#41;</span> == MethodAttributes.<span style="color: #0000FF;">ReuseSlot</span><span style="color: #000000;">&#41;</span>
				? <span style="color: #808080;">&quot;override&quot;</span> : <span style="color: #808080;">&quot;virtual&quot;</span><span style="color: #000000;">&#41;</span> : <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>;
			methods<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;{0} {1} {2} {3} {4}.{5}({6})&quot;</span>,
				accessModifier, isStatic, isVirtual, retType, m.<span style="color: #0000FF;">ReflectedType</span>.<span style="color: #0000FF;">FullName</span>, m.<span style="color: #0000FF;">Name</span>, sb.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF;">return</span> methods;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre>
<p>The information displayed in VS2005 by default is shown below.<br />
<a href="http://blog.jalil.org/wp-content/uploads/2008/02/withoutmulticastdelegateproxy.png" title="Default Event information in VS2005"><img src="http://blog.jalil.org/wp-content/uploads/2008/02/withoutmulticastdelegateproxy.png" alt="Default Event information in VS2005" /></a></p>
<p>The information displayed in VS2005 with MulticastDelegateDebuggerProxy is shown below. The MulticastDelegateDebuggerProxy displays fully qualified method name and the parameter names used in the method.</p>
<p><a href="http://blog.jalil.org/wp-content/uploads/2008/02/multicastdelegateproxy.png" title="Event information in VS2005 with MulticastDelegateDebuggerProxy"><img src="http://blog.jalil.org/wp-content/uploads/2008/02/multicastdelegateproxy.png" alt="Event information in VS2005 with MulticastDelegateDebuggerProxy" /></a><br />
In VS2008, the default information displayed is slightly better than VS2005 as it shows _invocationList by default which can be drilled down to the actual methods. However, the default display can get ambiguous as shown in the screenshot below. The MulticastDelegateDebuggerProxy does a much better job at displaying the same information.</p>
<p><a href="http://blog.jalil.org/wp-content/uploads/2008/02/withoutmulticastdelegateproxyvs2008.png" title="Default event information in VS2008"><img src="http://blog.jalil.org/wp-content/uploads/2008/02/withoutmulticastdelegateproxyvs2008.png" alt="Default event information in VS2008" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jalil.org/2008/02/18/how-to-determine-event-subscribers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning .NET debugging using WinDBG</title>
		<link>http://blog.jalil.org/2008/02/18/learning-net-debugging-using-windbg/</link>
		<comments>http://blog.jalil.org/2008/02/18/learning-net-debugging-using-windbg/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 21:36:44 +0000</pubDate>
		<dc:creator>Jalil Vaidya</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://blog.jalil.org/2008/02/18/learning-net-debugging-using-windbg/</guid>
		<description><![CDATA[If you wanted to learn advanced debugging of .NET application using WinDBG but didn't know where to start, look no further. Tess Ferrandez has started posting debugging labs to teach you how to debug tough problems. Her blog has invaluable tips about debugging real world problems. She has posted real world case studies and explained [...]]]></description>
				<content:encoded><![CDATA[<p>If you wanted to learn advanced debugging of .NET application using <a href="http://www.microsoft.com/whdc/devtools/debugging/default.mspx" title="Debugging Tools for Windows" target="_blank">WinDBG</a> but didn't know where to start, look no further. <a href="http://blogs.msdn.com/tess/default.aspx" title="If broken it is, fix it you should" target="_blank">Tess Ferrandez</a> has started posting <a href="http://blogs.msdn.com/tess/pages/net-debugging-demos-information-and-setup-instructions.aspx" title=".NET Debugging Demos - Information and setup instructions" target="_blank">debugging labs</a> to teach you how to debug tough problems. Her blog has invaluable tips about debugging real world problems. She has posted real world case studies and explained how to approach and solve the problems. In debugging labs, she posts a buggy application with instructions for troubleshooting the problem. A couple of days later, she posts a review of the lab where she walks you through troubleshooting the problem in great detail. So far she has posted 3 debugging labs (<a href="http://blogs.msdn.com/tess/archive/2008/02/04/net-debugging-demos-lab-1-hang.aspx" title="Lab 1: Hang" target="_blank">one</a>, <a href="http://blogs.msdn.com/tess/archive/2008/02/08/net-debugging-demos-lab-2-crash.aspx" title="Lab 2: Crash" target="_blank">two</a>, <a href="http://blogs.msdn.com/tess/archive/2008/02/15/net-debugging-demos-lab-3-memory.aspx" title="Lab 3: Memory" target="_blank">three</a>) and 2 lab reviews (<a href="http://blogs.msdn.com/tess/archive/2008/02/06/net-debugging-demos-lab-1-hang-review.aspx" title="Lab 1: Hang - review" target="_blank">one</a>, <a href="http://blogs.msdn.com/tess/archive/2008/02/11/net-debugging-demos-lab-2-crash-review.aspx" title="Lab 2: Crash - review" target="_blank">two</a>). She will be posting a total of 10 labs. These labs are a great learning tool and best of all, they are all free! Thank you Tess!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jalil.org/2008/02/18/learning-net-debugging-using-windbg/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ghazal-E-Surat</title>
		<link>http://blog.jalil.org/2008/02/10/ghazal-e-surat/</link>
		<comments>http://blog.jalil.org/2008/02/10/ghazal-e-surat/#comments</comments>
		<pubDate>Sun, 10 Feb 2008 17:34:05 +0000</pubDate>
		<dc:creator>Jalil Vaidya</dc:creator>
				<category><![CDATA[Ghazal]]></category>

		<guid isPermaLink="false">http://blog.jalil.org/2008/02/10/ghazal-e-surat/</guid>
		<description><![CDATA[Ghazal is a form of poetry that was introduced to India by the Moghuls in the 12th century. While Ghazals can be written in any language, in India it is more popularly written in Urdu, Hindi and Gujarati. When it comes to Gujarati Ghazals, the city of Surat is considered the front runner with the [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://smriti.com/urdu/ghazal.def.html" title="What is Ghazal?" target="_blank">Ghazal</a> is a form of poetry that was introduced to India by the Moghuls in the 12th century. While Ghazals can be written in any language, in India it is more popularly written in Urdu, Hindi and Gujarati. When it comes to Gujarati Ghazals, the city of Surat is considered the front runner with the most popular Ghazalkars or Shayers (i.e. poets) coming from Surat. In an unprecedented move, a publisher from Surat has published a book called Ghazal-E-Surat that contains about 76 Ghazals from the 41 living Ghazalkars. I came across this news on the <a href="http://vmtailor.com/archives/208" title="Ghazal-E-Surat" target="_blank">blog</a> of Dr. Vivek Tailor a Ghazalkar from Surat, and <a href="http://layastaro.com/?p=1045" title="Ghazal-E-Surat" target="_blank">Layastro</a> a leading Gujarati Ghazal site. One of the Ghazalkars published in the book is my Grandfather, H. N. Keshwani.  My Grandfather is well known in the Gujarati Ghazal circle in Surat, but little outside of it. I am very proud of my Grandpa for his achievements, and very happy that he was recognized by the publishers of Ghazal-E-Surat. Among the other Ghazalkars' pictures on the front and back cover of the book, my Grandpa is on front cover, first picture on the third row.</p>
<p><a href="http://blog.jalil.org/wp-content/uploads/2008/02/ghazal-e-surat-hnk.jpg" title="Ghazal-E-Surat"><img src="http://blog.jalil.org/wp-content/uploads/2008/02/ghazal-e-surat-hnk.jpg" alt="Ghazal-E-Surat" /></a></p>
<p>Image courtesy: <a href="http://www.vmtailor.com/" title="Dr. V. M. Tailor" target="_blank">Dr. Vivek Tailor</a> who is also published in the book.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jalil.org/2008/02/10/ghazal-e-surat/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Vista Dual Monitor Woes</title>
		<link>http://blog.jalil.org/2008/02/03/vista-dual-monitor-woes/</link>
		<comments>http://blog.jalil.org/2008/02/03/vista-dual-monitor-woes/#comments</comments>
		<pubDate>Sun, 03 Feb 2008 19:54:47 +0000</pubDate>
		<dc:creator>Jalil Vaidya</dc:creator>
				<category><![CDATA[Vista]]></category>

		<guid isPermaLink="false">http://blog.jalil.org/2008/02/03/vista-dual-monitor-woes/</guid>
		<description><![CDATA[This issue with the dual monitor setup in Vista has been bugging me a lot. I have 2 19" Westinghouse monitors that have DVI and VGA inputs. I have an ATI Radeon 9200 video card that has a DVI and a VGA output. The left monitor (primary) is connected using DVI and the right one [...]]]></description>
				<content:encoded><![CDATA[<p>This issue with the dual monitor setup in Vista has been bugging me a lot. I have 2 19" Westinghouse monitors that have DVI and VGA inputs. I have an ATI Radeon 9200 video card that has a DVI and a VGA output. The left monitor (primary) is connected using DVI and the right one is connected to VGA. The dual monitor is working fine except for one problem. When I turn off the left primary monitor (the one using DVI) and turn it back on, I get no signal. I only have two options, either reboot the machine, or login to the machine from another machine using remote desktop. As soon as I login using RDP, the monitor comes to life. I Googled for dual monitor related issues in Vista and followed through various suggestion including installing the latest ATI Catalyst video drivers, disabling turning off monitors in the power management settings and disabling Transient Multi-Monitor Manager (TMM) task, but nothing has helped so far. I have found only one <a href="http://groups.google.com/group/microsoft.public.windows.vista.general/browse_thread/thread/78395f057952363b/f8df69067033a60a" title=" Turn Off Display - but, it will not turn back on, must reboot" target="_blank">reference</a> to the exact same issue that I am facing, but there is no resolution. The exact same setup works fine on Windows XP.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jalil.org/2008/02/03/vista-dual-monitor-woes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DEP, NXCOMPAT Redux</title>
		<link>http://blog.jalil.org/2008/02/02/dep-nxcompat-redux/</link>
		<comments>http://blog.jalil.org/2008/02/02/dep-nxcompat-redux/#comments</comments>
		<pubDate>Sat, 02 Feb 2008 22:44:10 +0000</pubDate>
		<dc:creator>Jalil Vaidya</dc:creator>
				<category><![CDATA[.NET Framework]]></category>

		<guid isPermaLink="false">http://blog.jalil.org/2008/02/02/dep-nxcompat-redux/</guid>
		<description><![CDATA[I recently wrote about the impact of DEP on .NET applications due to changes in the .NET Framework 2.0 SP1. A couple of days later I came across Michael Howard's post about new APIs related to DEP. As Michael points out, in Windows Vista SP1, Windows XP SP3 and Windows Server 2008, it is possible [...]]]></description>
				<content:encoded><![CDATA[<p>I recently <a href="http://blog.jalil.org/2008/01/27/dep-nxcompat-and-changes-in-net-framework-20-sp1/" title="DEP, NXCOMPAT and changes in .NET Framework 2.0 SP1" target="_blank">wrote</a> about the impact of DEP on .NET applications due to changes in the .NET Framework 2.0 SP1. A couple of days later I came across Michael Howard's <a href="http://blogs.msdn.com/michael_howard/archive/2008/01/29/new-nx-apis-added-to-windows-vista-sp1-windows-xp-sp3-and-windows-server-2008.aspx" title="New NX APIs added to Windows Vista SP1, Windows XP SP3 and Windows Server 2008" target="_blank">post</a> about new APIs related to DEP. As Michael points out, in Windows Vista SP1, Windows XP SP3 and Windows Server 2008, it is possible for developers to enable DEP for an application at runtime instead of using a linker switch or being <a href="http://blogs.msdn.com/ed_maurer/archive/2007/12/14/nxcompat-and-the-c-compiler.aspx" title="NXCOMPAT and the C# compiler" target="_blank">forced</a> into setting NXCOMPAT bit by the C# compiler. The new function <a href="http://msdn2.microsoft.com/en-us/library/bb736299(VS.85).aspx" title="SetProcessDEPPolicy Function" target="_blank">SetProcessDEPPolicy</a>, allows the running process to change the DEP settings for the process calling it. I think this is a much more flexible option since it puts application developers in control of using DEP for their applications. Michael also explains the options available in <a href="http://msdn2.microsoft.com/en-us/library/bb736299(VS.85).aspx" title="SetProcessDEPPolicy Function" target="_blank">SetProcessDEPPolicy</a> API which allows the application that is using the old ATL code to enable DEP, and not throw the DEP exception at runtime. Raymond Chen has <a href="http://blogs.msdn.com/oldnewthing/archive/2007/11/16/6281925.aspx" title="In Windows XP, even when DEP is on, it's still sometimes off" target="_blank">written</a> about how in Windows XP, DEP will not throw an exception for what he calls "well-known DEP-voilating thunks". The new DEP API brings this feature to WinXP SP3, Vista, and Windows Server 2008. Now if only Microsoft would release a hotfix for the C# compiler to provide a /NXCOMPAT switch, the .NET development world would be a much better place.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jalil.org/2008/02/02/dep-nxcompat-redux/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How JNI calls functions from DLL</title>
		<link>http://blog.jalil.org/2008/02/02/how-jni-calls-functions-from-dll/</link>
		<comments>http://blog.jalil.org/2008/02/02/how-jni-calls-functions-from-dll/#comments</comments>
		<pubDate>Sat, 02 Feb 2008 21:57:37 +0000</pubDate>
		<dc:creator>Jalil Vaidya</dc:creator>
				<category><![CDATA[JNI]]></category>

		<guid isPermaLink="false">http://blog.jalil.org/2008/02/02/how-jni-calls-functions-from-dll/</guid>
		<description><![CDATA[When a function is exported from a DLL the compiler will decorate or mangle the function name. To export the undecorated function name the linker needs an export definition file (.DEF). A while back I was working on a project in Java that calls functions from a DLL using JNI. I had created a DLL [...]]]></description>
				<content:encoded><![CDATA[<p>When a function is exported from a DLL the compiler will decorate or <a href="http://en.wikipedia.org/wiki/Name_mangling" title="Name mangling" target="_blank">mangle</a> the function name. To export the undecorated function name the linker needs an export definition file (<a href="http://blog.jalil.org/wp-admin/Name%20manglinghttp://msdn2.microsoft.com/en-us/library/d91k01sh.aspx" title="Exporting from a DLL Using DEF Files" target="_blank">.DEF</a>). A while back I was working on a project in Java that calls functions from a DLL using <a href="http://java.sun.com/javase/6/docs/technotes/guides/jni/index.html" title="Java Native Interface" target="_blank">JNI</a>. I had created a DLL that exported some functions to be called from a Java application. Just when I started to test the application I realized that I had forgotten to update the .DEF file. I was expecting the Java application to fail when it would try to call the functions in the DLL. To my surprise, the function calls succeeded. I was curious to find out why these calls succeeded. I wrote a sample Java application that had one class called Hello which would call a native function called sayHello in a DLL called Hello.DLL. Running <a href="http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javah.html" title="javah - C Header and Stub File Generator" target="_blank">javah</a> on the Java source generated the header file with a native function named Java_Hello_sayHello. I created the DLL and made sure that the exported function was undecorated. Using the fantastic <a href="http://www.dependencywalker.com/" title="Depends" target="_blank">Dependency Walker's</a> lightweight profiler I ran the Java application. Looking at the output window in Depends, it was clear why a call to a function in decorated form, would succeed. JNI first tries to call the function using two decorated names before calling it with the undecorated name as illustrated in the image below.</p>
<p><a href="http://blog.jalil.org/wp-content/uploads/2008/02/jnicallingsayhellosmall.png" title="JNI calling native function"><img src="http://blog.jalil.org/wp-content/uploads/2008/02/jnicallingsayhellosmall.png" alt="JNI calling native function" /></a></p>
<p>The JNI <a href="http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/jniTOC.html" title="Java Native Interface Specification" target="_blank">specification</a> mentions the <a href="http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/design.html#wp615" title="Resolving Native Method Names" target="_blank">naming convention</a> for the native function. However, there is no mention of the fact that when calling the functions from a native library the runtime will try to call the functions using both the decorated and undecorated names. Another peculiar behavior that I noticed is that when calling the other JNI related functions that you can export from a native library such as JNI_OnLoad, JNI_OnUnload, the runtime tries only one decorated name before calling the undecorated function as shown above. I find this very strange.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jalil.org/2008/02/02/how-jni-calls-functions-from-dll/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DEP, NXCOMPAT and changes in .NET Framework 2.0 SP1</title>
		<link>http://blog.jalil.org/2008/01/27/dep-nxcompat-and-changes-in-net-framework-20-sp1/</link>
		<comments>http://blog.jalil.org/2008/01/27/dep-nxcompat-and-changes-in-net-framework-20-sp1/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 04:01:53 +0000</pubDate>
		<dc:creator>Jalil Vaidya</dc:creator>
				<category><![CDATA[.NET Framework]]></category>

		<guid isPermaLink="false">http://blog.jalil.org/2008/01/27/dep-nxcompat-and-changes-in-net-framework-20-sp1/</guid>
		<description><![CDATA[This topic has been written about but not as much as it should have been. Hence, I am repeating it here again. It will also serve as a future reference for me. DEP stands for Data Execution Prevention. It is a hardware based feature supported by Intel as well as AMD processors that disables execution [...]]]></description>
				<content:encoded><![CDATA[<p>This topic has been written about but not as much as it should have been. Hence, I am repeating it here again. It will also serve as a future reference for me.</p>
<p><a href="http://en.wikipedia.org/wiki/Data_Execution_Prevention" title="Data Execution Prevention" target="_blank">DEP</a> stands for Data Execution Prevention. It is a hardware based feature supported by Intel as well as AMD processors that disables execution of code that resides in memory areas marked as data pages. On AMD processors, this feature is implemented as NX (No eXecute) bit, and on Intel processors it is implemented as XD (eXecute Disable) bit. An OS can expose this feature and enable the application to take advantage of this protection. Once enabled, it becomes very difficult to inject and run malicious code using popular exploits such as buffer overflow which injects code on stack or heap memory and branches the instruction pointer (EIP) to execute the code. Microsoft has made this feature available in its operating systems starting with the Windows XP Service Pack 2. This feature can be <a href="http://support.microsoft.com/kb/875352/EN-US/" title="A detailed description of the Data Execution Prevention (DEP) feature in Windows XP Service Pack 2, Windows XP Tablet PC Edition 2005, and Windows Server 2003" target="_blank">enabled</a> either system wide, or per application in Windows XP Service Pack 2, Windows XP Tablet PC Edition 2005, and Windows Server 2003. In Windows Vista this feature is enabled by default for all the system applications. Vista also has an Opt-out feature that allows you to enable DEP for all the (system as well as user) applications except those on the opt-out list. <a href="http://blogs.technet.com/robert_hensing/default.aspx" target="_blank">Robert Hensing</a> explains this is great detail in his <a href="http://blogs.technet.com/robert_hensing/archive/2007/04/04/dep-on-vista-explained.aspx" title="DEP on Vista exposed!" target="_blank">post</a>. For VC++ developers there is one more way to take advantage of DEP. Visual Studio 2003 and above provide a <a href="http://msdn2.microsoft.com/en-us/library/y0zzbyt4(VS.71).aspx" title="Linker Options" target="_blank">linker</a> switch called <a href="http://msdn2.microsoft.com/en-us/library/ms235442(VS.80).aspx" title="NXCOMPAT" target="_blank">/NXCOMPAT</a>. This switch sets a bit in the executable that indicates the OS loader that the application is DEP enabled. However, there is one catch. Setting this bit in the executable overrides all the other DEP settings - meaning even if DEP is <em>disabled</em> system wide or the application is opted out of DEP, the OS will still enable DEP for that application!!! This is where the problem starts for applications compiled using .NET 2.0 SP1. The C# compiler was <a href="http://blogs.msdn.com/ed_maurer/archive/2007/12/14/nxcompat-and-the-c-compiler.aspx" title="NXCOMPAT and the C# compiler" target="_blank">updated</a> to set this bit in the assemblies that it creates, and there is no way to unset it as in VC++ linker. If your application does not use native code, then you have nothing to worry about. If your application uses native then you may be in for a rude shock; your application might suddenly crash with IP_ON_HEAP errors. The old ATL framework generated code on the fly that ran on the stack or heap. If your .NET application uses a component or an ActiveX control that was built using the old ATL framework then your application will fail. The option is to recompile your native code using VS2003 or above. If this is a third party component that the vendor has not updated then you may be out of luck. Don't panic however! There is a ray of hope. You can disable the NXCOMPAT bit set by the C# compiler in the executable using the EditBin utility that is a part of Visual Studio installation. If your assembly is strongly named then you will have to resign it using SN utility. I strongly believe that Microsoft made a horrible mistake by deciding to set this bit by default on all the assemblies. It would have been much better if they had provided a switch just like the C++ linker. This new C# compiler feature is not well publicized by Microsoft. All the .NET developers that I have spoken to so far do not even know what DEP or NXCOMPAT is. Following the pure managed code mantra, most .NET developers do not care about native code any more even if they are using native code in their application. There is still lot of native code out there in the enterprises that is incompatible with DEP. There are lot of .NET applications that use such native code. This creates quite a problem when unknowing developers are faced with a situation when their application(s) suddenly stops working by just recompiling it in .NET 2.0 SP1. There are also other <a href="http://www.hanselman.com/blog/content/binary/org2.0to2.0/APIChangesorg2.0to2.0.html" target="_blank">breaking changes</a> as well as new properties, methods and Types <a href="http://www.hanselman.com/blog/content/binary/RedBitsChangesv2.html" title="New Methods and Types in .NET 2.0 SP1" target="_blank"> added</a> in SP1 as mentioned in a <a href="http://www.hanselman.com/blog/CatchingRedBitsDifferencesInNET20AndNET20SP1.aspx" title="Catching RedBits differences in .NET 2.0 and .NET 2.0SP1" target="_blank">post</a> by <a href="http://www.hanselman.com/blog/" target="_blank">Scott Hanselman</a>. This situation is made even worse by the release of Visual Studio 2008 as it installs .NET FX 2.0 SP1. Developers are keen to take advantage of the new features in C# 3.0 in .NET 2.0 applications using VS2008's <a href="http://blog.jalil.org/2008/01/21/understanding-vs2008-multi-targeting/" title="Understanding VS2008 Multi-Targeting" target="_blank">multi-targeting</a> feature. In an enterprise application development scenario, it is quite possible that developers will have .NET 2.0 SP1 on their development and test machines due to VS2008, but the user desktops will not have SP1. If the application uses new methods or types then it will fail on user desktops. Although one might be inclined to blame developers for developing and testing on environment that differs from the user's environment, such a scenario is highly likely to occur since the changes in .NET 2.0 SP1 are not well known. I have been able to find a list of <a href="http://support.microsoft.com/kb/945757" title="Problems that are fixed in the .NET Framework 2.0 Service Pack 1" target="_blank">bug fixes</a> in SP1, but there is no knowledge base article that lists the differences between .NET 2.0 and SP1 other than the few scattered blog entries. It is a very bad idea to make such significant changes in a service pack level release. This is however a reality. . . so developers beware!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jalil.org/2008/01/27/dep-nxcompat-and-changes-in-net-framework-20-sp1/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
