<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Random.Next()</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/cgarcia/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/cgarcia/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 03:18:55 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Interop: marshaling double pointers that can be null</title>
      <link>https://docs.microsoft.com/archive/blogs/cgarcia/interop-marshaling-double-pointers-that-can-be-null</link>
      <pubDate>Wed, 15 Jun 2011 12:04:00 GMT</pubDate>
      <dc:creator><![CDATA[Carlos [MSR]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cgarcia/2011/06/15/interop-marshaling-double-pointers-that-can-be-null/</guid>
      <description><![CDATA[I recently had to implement IMediaBuffer in managed code. The GetBufferAndLength method takes a...]]></description>
      <content:encoded><![CDATA[<p>I recently had to implement <a href="https://msdn.microsoft.com/en-us/library/dd390166(VS.85).aspx"><span style="color: #ff2fc4;">IMediaBuffer</span></a> in managed code. The GetBufferAndLength method takes a BYTE** ppBuffer argument, so I initially went with the following straightforward C# declaration:</p>
<p>&nbsp;<span class="kwrd">public</span> <span class="kwrd">void</span> GetBufferAndLength(<span class="kwrd">out</span> IntPtr ppBuffer, <span class="kwrd">out</span> <span class="kwrd">uint</span> cbLength)<br />&nbsp;{</p>
<div class="csharpcode">
<pre class="alt"><blockquote><p>ppBuffer = myBuffer;</p><p>&hellip;</p></blockquote><p>}</p></pre>
</div>

<p>This seemed to work fine, until I noticed a lot of first time NullReferenceExceptions were scrolling by in the debugger output window. Puzzled, I turned on breaking on all exceptions in VS and found that the native code calling my IMediaBuffer implementation would sometimes call with a NULL first argument in cases where it was only interested in obtaining the buffer length (cbLength). Apparently the interop layer was silently handling such cases. This was less than ideal, so I spent some time searching for a better way to do the managed declaration, looking at the various MarshalAs and such attributes but with no luck. Finally it dawned on me that a double pointer is also just a pointer and that I could do the dereferencing myself:</p>
<p>&nbsp;</p>
<p><span class="kwrd">public</span> <span class="kwrd">void</span> GetBufferAndLength(IntPtr ppBuffer, <span class="kwrd">out</span> <span class="kwrd">uint</span> cbLength)<br />{</p>
<div class="csharpcode">
<pre class="alt">     <span class="kwrd">if</span> (ppBuffer != IntPtr.Zero)</pre>
<pre>     {</pre>
<pre class="alt">          Marshal.WriteIntPtr(ppBuffer, myBuffer);</pre>
<pre>     }</pre>
<pre class="alt">     ...</pre>
</div>
<p>Voila! This can handle the double pointer being NULL and no more NullReferenceExceptions :)</p>]]></content:encoded>
    </item>
    <item>
      <title>Silverlight anti-aliasing woes</title>
      <link>https://docs.microsoft.com/archive/blogs/cgarcia/silverlight-anti-aliasing-woes</link>
      <pubDate>Thu, 03 Dec 2009 09:36:53 GMT</pubDate>
      <dc:creator><![CDATA[Carlos [MSR]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cgarcia/2009/12/03/silverlight-anti-aliasing-woes/</guid>
      <description><![CDATA[I recently ran into an interesting problem. The designer in my team gave me a piece of XAML for my...]]></description>
      <content:encoded><![CDATA[<p>I recently ran into an interesting problem. The designer in my team gave me a piece of XAML for my UI containing horizontal lines of the form:</p>  <p>&#160;</p>  <p>&lt;Rectangle Fill=&quot;#FFDADADA&quot; Height=&quot;1&quot;&#160; …/&gt;</p>  <p>&#160;</p>  <p>I put this in the application and to our surprise, these lines showed twice as thick (2px) on my machine but looked fine on his. After researching this for a while I found it can happen when elements are not positioned on whole pixel boundaries. In this cases Silverlight has to do some anti-aliasing, which can result in additional pixels being rendered. For example, notice the difference between this rectangle when positioned at an integer and non-integer pixel positions (zoomed in):</p>  <p>&#160;</p>  <p>&lt;Rectangle Fill=&quot;Gray&quot; Width=&quot;100&quot; Height=&quot;4&quot; Canvas.Top=&quot;<strong>40</strong>&quot; Canvas.Left=&quot;20&quot;/&gt;</p>  <p><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/cgarcia/WindowsLiveWriter/Silverlightantialiasingwoes_F339/image_2.png" original-url="https://blogs.msdn.com/blogfiles/cgarcia/WindowsLiveWriter/Silverlightantialiasingwoes_F339/image_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/cgarcia/WindowsLiveWriter/Silverlightantialiasingwoes_F339/image_thumb.png" original-url="https://blogs.msdn.com/blogfiles/cgarcia/WindowsLiveWriter/Silverlightantialiasingwoes_F339/image_thumb.png" width="244" height="75" /></a> </p>  <p>&#160;</p>  <p>&lt;Rectangle Fill=&quot;Gray&quot; Width=&quot;100&quot; Height=&quot;4&quot; Canvas.Top=&quot;<strong>40.5</strong>&quot; Canvas.Left=&quot;20&quot;/&gt;</p>  <p><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/cgarcia/WindowsLiveWriter/Silverlightantialiasingwoes_F339/image_4.png" original-url="https://blogs.msdn.com/blogfiles/cgarcia/WindowsLiveWriter/Silverlightantialiasingwoes_F339/image_4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/cgarcia/WindowsLiveWriter/Silverlightantialiasingwoes_F339/image_thumb_1.png" original-url="https://blogs.msdn.com/blogfiles/cgarcia/WindowsLiveWriter/Silverlightantialiasingwoes_F339/image_thumb_1.png" width="219" height="76" /></a> </p>  <p>&#160;</p>  <p>So this was happening in my case and my 1 pixel line was becoming 2 pixels thick. However, the problem was still mystery to me because I only use integers for all the sizes and positions in the UI. Finally it dawned on me: I have some code to programmatically center a parent of this element on a canvas. When doing the math to center this, depending on the browser window size, I would get a non-integer position (which explained the variation across machines). </p>  <p>A simple Math.Round fixed the problem.</p>]]></content:encoded>
    </item>
    <item>
      <title>Deferred TLS/SSL handshakes on windows mobile</title>
      <link>https://docs.microsoft.com/archive/blogs/cgarcia/deferred-tlsssl-handshakes-on-windows-mobile</link>
      <pubDate>Tue, 15 Sep 2009 12:57:00 GMT</pubDate>
      <dc:creator><![CDATA[Carlos [MSR]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cgarcia/2009/09/15/deferred-tlsssl-handshakes-on-windows-mobile/</guid>
      <description><![CDATA[I recently received an email and blog comment from João Almeida, who was trying to use the SslHelper...]]></description>
      <content:encoded><![CDATA[<P>I recently received an email and blog comment from João Almeida, who was trying to use the <A href="https://blogs.msdn.com/cgarcia/archive/2009/08/22/getting-a-managed-socket-to-talk-ssl.aspx" mce_href="https://blogs.msdn.com/cgarcia/archive/2009/08/22/getting-a-managed-socket-to-talk-ssl.aspx">SslHelper</A> class to make a secure connection to an SMTP server and was having some trouble. After doing a little research I found this is a little different from doing an HTTPS connection. The SMTP protocol requires the connection to start in plain text and the TLS handshake to happen <STRONG>after</STRONG> the <A href="https://tools.ietf.org/html/rfc2487" mce_href="https://tools.ietf.org/html/rfc2487">STARTTLS</A> command is issued. Here’s how a typical secure SMTP session would start (bolded text is what the SMTP client needs to send, normal text are server responses):</P>
<P mce_keep="true">&nbsp;</P>
<P>&gt;telnet smtp.live.com 587 </P>
<P>220 BLU0-SMTP8.blu0.hotmail.com Microsoft ESMTP MAIL Service, Version: 6.0.3790.3959 ready at&nbsp; Mon, 14 Sep 2009 16:44:33 -0700 <BR><STRONG>EHLO</STRONG> <BR>250-BLU0-SMTP8.blu0.hotmail.com Hello [131.107.0.75] <BR>250-TURN <BR>250-SIZE 35840000 <BR>250-ETRN <BR>250-PIPELINING <BR>250-DSN <BR>250-ENHANCEDSTATUSCODES <BR>250-8bitmime <BR>250-BINARYMIME <BR>250-CHUNKING <BR>250-VRFY <BR>250-TLS <BR>250-STARTTLS <BR>250 OK <BR><STRONG>STARTTLS <BR></STRONG>220 2.0.0 SMTP server ready </P>
<P>Up to this point everything needs to be in plain text. But once the 220 status code is received the TLS handshake needs to be started.</P>
<P>So how does the SslHelper code need to change to accommodate this? We need to ask winsock to do a <A href="https://msdn.microsoft.com/en-us/library/ms885776.aspx" mce_href="https://msdn.microsoft.com/en-us/library/ms885776.aspx">deferred handshake</A> as follows (please refer to the <A href="https://blogs.msdn.com/cgarcia/archive/2009/08/21/enable-ssl-for-managed-socket-on-windows-mobile.aspx" mce_href="https://blogs.msdn.com/cgarcia/archive/2009/08/21/enable-ssl-for-managed-socket-on-windows-mobile.aspx">full listing</A> of SslHelper). First, we need to bring in a couple of constants from the mobile SDK: </P>
<DIV style="FONT-FAMILY: courier new; BACKGROUND: white; COLOR: black; FONT-SIZE: 10pt">
<P style="MARGIN: 0px"><SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">const</SPAN> <SPAN style="COLOR: blue">int</SPAN> SSL_FLAG_DEFER_HANDSHAKE = 0x0008;</P>
<P style="MARGIN: 0px"><SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">const</SPAN> <SPAN style="COLOR: blue">int</SPAN> _SO_SSL_PERFORM_HANDSHAKE = 0x0d;</P>
<P style="MARGIN: 0px"><SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">const</SPAN> <SPAN style="COLOR: blue">long</SPAN> SO_SSL_PERFORM_HANDSHAKE = _SO_SSL | _SO_SSL_PERFORM_HANDSHAKE;</P></DIV>
<P>Next, we need to tell the socket that we want it to use a deferred handshake by calling socket.IOControl with the SSL_FLAG_DEFER_HANDSHAKE. We do this in the constructor after setting the ssl cert validation hook:</P>
<DIV style="FONT-FAMILY: courier new; BACKGROUND: white; COLOR: black; FONT-SIZE: 10pt">
<P style="MARGIN: 0px">socket.IOControl((<SPAN style="COLOR: blue">int</SPAN>)SO_SSL_SET_VALIDATE_CERT_HOOK, inBuffer, <SPAN style="COLOR: blue">null</SPAN>);</P>
<P style="MARGIN: 0px"><SPAN style="COLOR: blue">if</SPAN> (deferHandshake)</P>
<P style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; socket.IOControl((<SPAN style="COLOR: blue">int</SPAN>)SO_SSL_SET_FLAGS, <SPAN style="COLOR: #2b91af">BitConverter</SPAN>.GetBytes(SSL_FLAG_DEFER_HANDSHAKE), <SPAN style="COLOR: blue">null</SPAN>);</P></DIV>
<P>Finally, we need to add a method that will let us start the handshake when we need to:</P>
<DIV style="FONT-FAMILY: courier new; BACKGROUND: white; COLOR: black; FONT-SIZE: 10pt">
<P style="MARGIN: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">void</SPAN> DoHandshake()</P>
<P style="MARGIN: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</P>
<P style="MARGIN: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; socket.IOControl((<SPAN style="COLOR: blue">int</SPAN>)SO_SSL_PERFORM_HANDSHAKE, <SPAN style="COLOR: #2b91af">BitConverter</SPAN>.GetBytes(ptrHost.ToInt32()), <SPAN style="COLOR: blue">null</SPAN>);</P>
<P style="MARGIN: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P></DIV>
<P>With this in place, here’s a quick and dirty example of how to start a TLS session with an SMTP server:</P>
<DIV style="FONT-FAMILY: courier new; BACKGROUND: white; COLOR: black; FONT-SIZE: 10pt">
<P style="MARGIN: 0px"><SPAN style="COLOR: blue">string</SPAN> SmtpServer = <SPAN style="COLOR: #a31515">"smtp.live.com"</SPAN>;</P>
<P style="MARGIN: 0px"><SPAN style="COLOR: blue">int</SPAN> port = 587;</P>
<P style="MARGIN: 0px"><SPAN style="COLOR: #2b91af">IPHostEntry</SPAN> IPhst = <SPAN style="COLOR: #2b91af">Dns</SPAN>.GetHostEntry(SmtpServer);</P>
<P style="MARGIN: 0px"><SPAN style="COLOR: #2b91af">IPEndPoint</SPAN> endPt = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">IPEndPoint</SPAN>(IPhst.AddressList[0], port);</P>
<P style="MARGIN: 0px"><SPAN style="COLOR: #2b91af">Socket</SPAN> s = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">Socket</SPAN>(endPt.AddressFamily, <SPAN style="COLOR: #2b91af">SocketType</SPAN>.Stream, <SPAN style="COLOR: #2b91af">ProtocolType</SPAN>.Tcp);</P>
<P style="MARGIN: 0px"><SPAN style="COLOR: blue">using</SPAN> (<SPAN style="COLOR: #2b91af">SslHelper</SPAN> helper = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">SslHelper</SPAN>(s, SmtpServer, <STRONG><SPAN style="COLOR: blue">true</SPAN>)</STRONG>)</P>
<P style="MARGIN: 0px">{</P>
<P style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">string</SPAN> line = <SPAN style="COLOR: blue">null</SPAN>;</P>
<P style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; s.Connect(endPt);</P>
<P style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">StreamReader</SPAN> sr = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">StreamReader</SPAN>(<SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">NetworkStream</SPAN>(s), <SPAN style="COLOR: #2b91af">Encoding</SPAN>.ASCII);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; s.Send(<SPAN style="COLOR: #2b91af">Encoding</SPAN>.ASCII.GetBytes(<SPAN style="COLOR: #a31515">"EHLO\r\n"</SPAN>));</P>
<P style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">while</SPAN> ((line = sr.ReadLine()) != <SPAN style="COLOR: #a31515">"250 OK"</SPAN>) ;</P>
<P style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; s.Send(<SPAN style="COLOR: #2b91af">Encoding</SPAN>.ASCII.GetBytes(<SPAN style="COLOR: #a31515">"STARTTLS\r\n"</SPAN>));</P>
<P style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">while</SPAN> (!(line = sr.ReadLine()).StartsWith(<SPAN style="COLOR: #a31515">"220"</SPAN>)) ;</P>
<P style="MARGIN: 0px"><STRONG>&nbsp;&nbsp;&nbsp; helper.DoHandshake();</STRONG></P>
<P style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">Debug</SPAN>.WriteLine(<SPAN style="COLOR: #a31515">"success"</SPAN>);</P>
<P style="MARGIN: 0px">}</P></DIV>
<P>Again, thanks to João for the question and for helping me validate the approach.</P>]]></content:encoded>
    </item>
    <item>
      <title>Handling DOM events in a C# ActiveX</title>
      <link>https://docs.microsoft.com/archive/blogs/cgarcia/handling-dom-events-in-a-c-activex</link>
      <pubDate>Fri, 28 Aug 2009 19:38:35 GMT</pubDate>
      <dc:creator><![CDATA[Carlos [MSR]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cgarcia/2009/08/28/handling-dom-events-in-a-c-activex/</guid>
      <description><![CDATA[I recently had to implement an ActiveX object (or I guess just a COM object since it had no UI) in...]]></description>
      <content:encoded><![CDATA[<p>I recently had to implement an ActiveX object (or I guess just a COM object since it had no UI) in C#. There are several articles out there about how to do this (<a href="https://www.bing.com/search?q=activex+in+C%23">bing it</a>), but I couldn’t find much information about handling DOM events in managed code. After some digging and experimentation I found a way to make this work, which is the subject of this post. I’ll assume you have some familiarity with getting a basic managed object running in IE and calling its methods from Javascript.</p>  <p>&#160;</p>  <p>The first step in handling a DOM event is getting a pointer to the <a href="https://msdn.microsoft.com/en-us/library/aa752574(VS.85).aspx">IHtmlDocument2</a> interface in your C# code. The easiest way is to just pass it in explicitly from Javascript. To do this first you’ll need to add a COM reference to mshtml:</p>  <p>&#160;</p>  <p>&#160;<a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/cgarcia/WindowsLiveWriter/HandlingDOMeventsinaCActiveX_ADA4/mshtmlref.png" original-url="https://blogs.msdn.com/blogfiles/cgarcia/WindowsLiveWriter/HandlingDOMeventsinaCActiveX_ADA4/mshtmlref.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="mshtmlref" border="0" alt="mshtmlref" src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/cgarcia/WindowsLiveWriter/HandlingDOMeventsinaCActiveX_ADA4/mshtmlref_thumb.png" original-url="https://blogs.msdn.com/blogfiles/cgarcia/WindowsLiveWriter/HandlingDOMeventsinaCActiveX_ADA4/mshtmlref_thumb.png" width="324" height="265" /></a> </p>  <p>&#160;</p>  <p>Then you add a method&#160; to your COM object to receive this pointer (I decided to call it Initialize):</p>  <p>&#160;</p>  <div style="font-family: courier new; background: white; color: black; font-size: 10pt">   <p style="margin: 0px"><span style="color: blue">using</span> System;</p>    <p style="margin: 0px"><span style="color: blue">using</span> System.Runtime.InteropServices;</p>    <p style="margin: 0px"><span style="color: blue">using</span> mshtml;</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px"><span style="color: blue">namespace</span> ManagedActiveX</p>    <p style="margin: 0px">{ </p> </div>  <div style="font-family: courier new; background: white; color: black; font-size: 10pt">   <p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">ITestObject</span></p>    <p style="margin: 0px">&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Initialize(<span style="color: #2b91af">IHTMLDocument2</span> doc);</p>    <p style="margin: 0px">&#160;&#160;&#160; }</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160; [<span style="color: #2b91af">ComVisible</span>(<span style="color: blue">true</span>)]</p>    <p style="margin: 0px">&#160;&#160;&#160; [<span style="color: #2b91af">ClassInterface</span>(<span style="color: #2b91af">ClassInterfaceType</span>.None)]&#160;&#160;&#160; </p>    <p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">TestObject</span> : <span style="color: #2b91af">ITestObject</span></p>    <p style="margin: 0px">&#160;&#160;&#160; {</p>    <p style="margin: 0px"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Initialize(<span style="color: #2b91af">IHTMLDocument2</span> doc)</strong></p>    <p style="margin: 0px"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </strong></p>    <p style="margin: 0px"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</strong></p>    <p style="margin: 0px">&#160;&#160;&#160; }</p>    <p style="margin: 0px">}</p> </div>  <p>And you can call this from your Javascript code to pass in the document:</p>  <p>&#160;</p>  <div style="background: white" class="Section1">   <p style="mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"><span style="mso-spacerun: yes">&#160;&#160;&#160; </span><span style="color: blue">&lt;</span><span style="color: #a31515">script</span> <span style="color: red">language</span><span style="color: blue">=&quot;javascript&quot;</span> <span style="color: red">type</span><span style="color: blue">=&quot;text/javascript&quot;&gt;<o:p></o:p></span></span></p>    <p style="mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"><span style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: blue">var</span> testObj = <span style="color: blue">new</span> ActiveXObject(<span style="color: #a31515">&quot;ManagedActiveX.TestObject&quot;</span>);<o:p></o:p></span></p>    <p style="mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"><o:p>&#160;</o:p></span></p>    <p style="mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"><span style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: blue">function</span> Init() {<span style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><o:p></o:p></span></p>    <p style="mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"><span style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>testObj.Initialize(document);<o:p></o:p></span></p>    <p style="mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"><span style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>}<span style="mso-spacerun: yes">&#160;&#160;&#160; </span><o:p></o:p></span></p>    <p class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"><span style="mso-spacerun: yes">&#160;&#160;&#160; </span><span style="color: blue">&lt;/</span><span style="color: #a31515">script</span><span style="color: blue">&gt;</span></span></p> </div>  <p>At this point your C# object has a pointer to the document and can get to the various events the DOM exposes, but how do you register a callback? Let’s take window.onunload for example:</p>  <p>&#160;</p>  <div style="font-family: courier new; background: white; color: black; font-size: 10pt">   <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Initialize(<span style="color: #2b91af">IHTMLDocument2</span> doc)</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; doc.parentWindow.onunload = ??</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p> </div>  <p>The onunload member is of type <em>object</em>, which doesn’t give much of a clue about what needs to go in there. I tried a delegate but that didn’t work. Then I looked at the <a href="https://msdn.microsoft.com/en-us/library/aa741488(VS.85).aspx">documentation</a> for this member which describes what you need to pass as:</p>  <p>&#160;</p>  <blockquote>   <p>&#160;<strong>VARIANT</strong> of type <strong>VT_DISPATCH</strong> that specifies the <strong>IDispatch</strong> interface of an object with a default method that is invoked when the event occurs. </p>    <p>&#160;</p> </blockquote>  <p>So how do you do that from C#? After doing a little reading I found all <a href="https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.comvisibleattribute.aspx">COMVisible</a> managed objects automatically implement <a href="https://msdn.microsoft.com/en-us/library/ms221608.aspx">IDispatch</a>. That is, the <a href="https://msdn.microsoft.com/en-us/library/f07c8z1c.aspx">COM Callable Wrapper</a> generated by .NET to expose your object to COM creates an implementation of IDispatch for you. Now we need to figure out how to tell the wrapper which method is the default IDispatch method. I found you specify this by marking the method with a Dispatch Id of 0:</p>  <div style="font-family: courier new; background: white; color: black; font-size: 10pt">   <p style="margin: 0px">&#160;&#160;&#160; [<span style="color: #2b91af">ComVisible</span>(<span style="color: blue">true</span>)]</p>    <p style="margin: 0px">&#160;&#160;&#160; [<span style="color: #2b91af">ClassInterface</span>(<span style="color: #2b91af">ClassInterfaceType</span>.None)]&#160;&#160;&#160; </p>    <p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">OnUnloadHandler</span> </p>    <p style="margin: 0px">&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IHTMLDocument2</span> doc;</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> OnUnloadHandler(<span style="color: #2b91af">IHTMLDocument2</span> document)</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; doc = document;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">DispId</span>(0)]</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">object</span> DefaultMethod()</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; doc.parentWindow.alert(<span style="color: #a31515">&quot;onunload event handled in managed code&quot;</span>);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">null</span>;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>    <p style="margin: 0px">&#160;&#160;&#160; }</p> </div>  <p>Then you simply create an instance of this type and set it as the onunload handler:</p>  <p>&#160;</p>  <div style="font-family: courier new; background: white; color: black; font-size: 10pt">   <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Initialize(<span style="color: #2b91af">IHTMLDocument2</span> doc)</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; doc.parentWindow.onunload = <span style="color: blue">new</span> <span style="color: #2b91af">OnUnloadHandler</span>(doc);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p> </div>  <p>If you host this object on a page with the script code shown above and then navigate away from it you should get the alert to pop up. Drop me a line if this doesn’t (or does :)) work for you.</p>]]></content:encoded>
    </item>
    <item>
      <title>New name: Random.Next()</title>
      <link>https://docs.microsoft.com/archive/blogs/cgarcia/new-name-random-next</link>
      <pubDate>Mon, 24 Aug 2009 00:50:45 GMT</pubDate>
      <dc:creator><![CDATA[Carlos [MSR]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cgarcia/2009/08/24/new-name-random-next/</guid>
      <description><![CDATA[I decided to rename the blog. Given that it just started I figured this wouldn’t be too bad. Plus...]]></description>
      <content:encoded><![CDATA[<p>I decided to rename the blog. Given that it just started I figured this wouldn’t be too bad. Plus “Carlos’” blog was lacking some creativity.</p>  <p>So why “Random.Next()”? Well, I don’t work on any particular Microsoft product or research area so the blog content will largely be dictated by the projects that get thrown my way. As such I feel there will be a little bit of randomness to the technologies I’ll cover here.</p>  <p>Plus I like the fact that Random.Next() always returns something positive :)</p>]]></content:encoded>
    </item>
    <item>
      <title>Getting a managed socket to talk SSL</title>
      <link>https://docs.microsoft.com/archive/blogs/cgarcia/getting-a-managed-socket-to-talk-ssl</link>
      <pubDate>Sun, 23 Aug 2009 00:05:40 GMT</pubDate>
      <dc:creator><![CDATA[Carlos [MSR]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cgarcia/2009/08/23/getting-a-managed-socket-to-talk-ssl/</guid>
      <description><![CDATA[This is a follow up to my previous post, where I showed a helper class I wrote to enable a managed...]]></description>
      <content:encoded><![CDATA[<p>This is a follow up to my <a href="https://blogs.msdn.com/cgarcia/archive/2009/08/21/enable-ssl-for-managed-socket-on-windows-mobile.aspx">previous post</a>, where I showed a helper class I wrote to enable a managed socket to communicate via SSL on windows mobile. It was my first technical blog post and a friend gave me some very good feedback: blogs can be more interesting if they not only show the solution to a problem, but also how the solution was found. So here I’d like to tell you a little bit of what I tried before finally making SSL work on my mobile device.</p>  <p>I started searching MSDN and came across this <a href="https://msdn.microsoft.com/en-us/library/aa916117.aspx">article</a>. The article talks about a native implementation using winsock and basically talks about calling <a title="setsockopt" href="https://msdn.microsoft.com/en-us/library/aa926453.aspx">setsockopt</a> and <a title="WSAIoctl" href="https://msdn.microsoft.com/en-us/library/aa916335.aspx">WSAIoctl</a> on the socket to enable SSL and register a certificate validation callback, respectively. I found the managed Socket class has corresponding methods: <a title="SetSocketOption" href="https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.setsocketoption.aspx">SetSocketOption</a> and <a title="IOControl" href="https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.iocontrol.aspx">IOControl</a> and at this point I thought this would be straightforward. To a certain extent it was, but I still had some work to do.</p>  <p>My first surprise was that SetSocketOption takes a SocketOptionName enum value as the second parameter, but this enum doesn’t have the equivalent of SO_SECURE. However, C# was nice enough to let me cast an arbitrary integer value to the enum I needed:</p>  <p>&#160;</p>  <div style="font-family: courier new; background: white; color: black; font-size: 10pt">   <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//The managed SocketOptionName enum doesn't have SO_SECURE so here we cast the integer value</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; socket.SetSocketOption(<span style="color: #2b91af">SocketOptionLevel</span>.Socket, (<span style="color: #2b91af">SocketOptionName</span>)SO_SECURE, SO_SEC_SSL);</p> </div>  <p>&#160;</p>  <p>Next in line was calling socket.IOControl to register a certificate validation callback. Turns out this method is a very general purpose one that takes a control code and then simply a buffer whose interpretation depends on such code. For SO_SSL_SET_VALIDATE_CERT_HOOK the first four bytes in this buffer are a function pointer to the certificate validation callback and the next 4 bytes are a pointer to a string containing the name of the host. Since we want to implement the validation callback in C# (more on why later) we need to first get a pointer to a delegate that we can pass to unmanaged code:</p>  <p>&#160;</p>  <div style="font-family: courier new; background: white; color: black; font-size: 10pt">   <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; hookFunc = <span style="color: #2b91af">Marshal</span>.GetFunctionPointerForDelegate(<span style="color: blue">new</span> <span style="color: #2b91af">SSLVALIDATECERTFUNC</span>(ValidateCert));</p>    <p style="margin: 0px">&#160;</p> </div>  <p>Note that it is important to maintain a reference to this IntPtr, otherwise the GC could collect it while the unmanaged code still holds a reference to it. I missed this detail the first time around and it seemed to work fine. But this was just because the GC was not reclaiming that pointer right away and that could change for any number of reasons so I was risking running into a very hard to find bug later on.</p>  <p>Now we need to write the host name into an unmanaged buffer, which we first need to allocate:</p>  <p>&#160;</p>  <div style="font-family: courier new; background: white; color: black; font-size: 10pt">   <p style="margin: 0px"><span style="color: green">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; //Allocate the buffer for the string</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ptrHost = <span style="color: #2b91af">Marshal</span>.AllocHGlobal(host.Length + 1);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WriteASCIIString(ptrHost, host);</p>    <p style="margin: 0px">&#160;</p> </div>  <p>This buffer is freed when the object is disposed since it needs to stick around until the certificate validation happens. Finally, we put these 8 bytes into a buffer and call socket.IOControl:</p>  <p>&#160;</p>  <div style="font-family: courier new; background: white; color: black; font-size: 10pt">   <p style="margin: 0px"><span style="color: green">//Now put both pointers into a byte[]</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> inBuffer = <span style="color: blue">new</span> <span style="color: blue">byte</span>[8];</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> hookFuncBytes = <span style="color: #2b91af">BitConverter</span>.GetBytes(hookFunc.ToInt32());</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> hostPtrBytes = <span style="color: #2b91af">BitConverter</span>.GetBytes(ptrHost.ToInt32());</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Array</span>.Copy(hookFuncBytes, inBuffer, hookFuncBytes.Length);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Array</span>.Copy(hostPtrBytes, 0, inBuffer, hookFuncBytes.Length, hostPtrBytes.Length);</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">unchecked</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; socket.IOControl((<span style="color: blue">int</span>)SO_SSL_SET_VALIDATE_CERT_HOOK, inBuffer, <span style="color: blue">null</span>);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p> </div>  <p>&#160;</p>  <p>That’s it as far as enabling SSL over the socket. All that remains is writing the ValidateCert function. This turns out to be relatively simple because .NET has a X509Certificate2 class that can parse the certificate blob passed to us. You can see the implementation of this function in the <a href="https://blogs.msdn.com/cgarcia/archive/2009/08/21/enable-ssl-for-managed-socket-on-windows-mobile.aspx">previous post</a>, where I do the required validations. But I want to make a comment about a particular piece of code that had me scratch my head for a little while. This is the code that takes the unmanaged pointer to the certificate passed in and reads the data into managed code:</p>  <p>&#160;</p>  <div style="font-family: courier new; background: white; color: black; font-size: 10pt">   <p style="margin: 0px"><span style="color: green">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; //NOTE: The documentation says pCertChain is a pointer to a LPBLOB struct:</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// {ulong size, byte* data} </span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//in reality the size is a 32 bit integer (not 64).</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">int</span> certSize = <span style="color: #2b91af">Marshal</span>.ReadInt32(pCertChain);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IntPtr</span> pData = <span style="color: #2b91af">Marshal</span>.ReadIntPtr(<span style="color: blue">new</span> <span style="color: #2b91af">IntPtr</span>(pCertChain.ToInt32() + <span style="color: blue">sizeof</span>(<span style="color: blue">int</span>)));</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">byte</span>[] certData = <span style="color: blue">new</span> <span style="color: blue">byte</span>[certSize];</p> </div>  <p>&#160;</p>  <p>When I read in the documentation that <em>size </em>was a long, I was thinking C#, so I was trying to read the size as a 64 bit integer which gave back a huge number. Needles to say the allocation of the byte[] threw an OutOfMemoryException. What I was missing was the fact that while a C# long is 64 bits, a C++ long is only 32 bits. And now that I think about it, it wasn’t just the C++ side of my brain that was turned off at that point, but also my common sense. Why on earth would you need 64 bits to indicate the size of the certificate? :)</p>  <p>Lucky for me I decided to just try reading 32 bits instead and then it all worked. However, I came to the wrong conclusion that the docs were mistaken when in fact I was failing to think about the differences in data types across managed and unmanaged. It wasn’t until I was re-reading the code for this post and I realized the mistake I had made.</p>  <p>&#160;</p>  <p>Here are links to some of the material I read when putting this together:</p>  <p><a title="https://msdn.microsoft.com/en-us/library/system.net.sockets.socket_members.aspx" href="https://msdn.microsoft.com/en-us/library/system.net.sockets.socket_members.aspx">https://msdn.microsoft.com/en-us/library/system.net.sockets.socket_members.aspx</a></p>  <p><a title="https://msdn.microsoft.com/en-us/library/aa916117.aspx" href="https://msdn.microsoft.com/en-us/library/aa916117.aspx">https://msdn.microsoft.com/en-us/library/aa916117.aspx</a></p>  <p><a title="https://windowsmobilepro.blogspot.com/2006/03/windows-mobile-secure-socket_25.html" href="https://windowsmobilepro.blogspot.com/2006/03/windows-mobile-secure-socket_25.html">https://windowsmobilepro.blogspot.com/2006/03/windows-mobile-secure-socket_25.html</a></p>  <p>   <p><a title="https://msdn.microsoft.com/en-us/library/aa916117.aspx" href="https://msdn.microsoft.com/en-us/library/aa916117.aspx"></a></p></p>]]></content:encoded>
    </item>
    <item>
      <title>Enable SSL for managed socket on windows mobile</title>
      <link>https://docs.microsoft.com/archive/blogs/cgarcia/enable-ssl-for-managed-socket-on-windows-mobile</link>
      <pubDate>Fri, 21 Aug 2009 22:33:10 GMT</pubDate>
      <dc:creator><![CDATA[Carlos [MSR]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cgarcia/2009/08/21/enable-ssl-for-managed-socket-on-windows-mobile/</guid>
      <description><![CDATA[I recently had to implement an application on windows mobile that would talk to a service via an...]]></description>
      <content:encoded><![CDATA[<strike></strike>  <p>I recently had to implement an application on windows mobile that would talk to a service via an SSL socket. This wasn’t just doing https requests so HttpWebRequest was not an option. And unfortunately the compact framework doesn’t include the <a href="https://msdn.microsoft.com/en-us/library/system.net.security.sslstream.aspx">SslStream</a> class (which would have made my life easier). </p>  <p>I found windows mobile sockets support SSL through winsock so I decided to explore this route. Since my application was mostly managed it took a little interop dancing but I finally got it to work. What I came up with is a helper class that can take an existing socket and will enable SSL on it. For example:</p>  <p>&#160;</p>  <div style="font-family: courier new; background: white; color: black; font-size: 10pt">   <p style="margin: 0px"><span style="color: blue">var</span> socket = <span style="color: blue">new</span> <span style="color: #2b91af">Socket</span>(ipaddr, <span style="color: #2b91af">SocketType</span>.Stream, <span style="color: #2b91af">ProtocolType</span>.IP);</p>    <p style="margin: 0px"><span style="color: blue">using</span> (<span style="color: blue">var</span> sslHelper = <span style="color: blue">new</span> <span style="color: #2b91af">SslHelper</span>(socket, host))</p>    <p style="margin: 0px">{</p>    <p style="margin: 0px">socket.Connect(ep);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160; /<span style="color: green">/at this point the SSL handshake has occurred and you can</span></p>    <p style="margin: 0px">&#160;&#160;&#160; <span style="color: green">//send and receive normally</span></p>    <p style="margin: 0px">} </p> </div>  <p>&#160;</p>  <p>Below is the code for this class. This is not meant to be production ready but can serve as an illustration on how to hook things up. In my case this was used as part of a proof of concept prototype.</p>  <p>It took me some time to figure out how all the pieces work together since the documentation is a little scattered but hopefully if you have to do something similar it can save you some time.</p>  <p>Another option might be to use <a href="https://msdn.microsoft.com/en-us/library/aa380493(VS.85).aspx">SSPI</a>, although I haven’t looked into that yet.</p>  <p>&#160;</p>  <div style="font-family: courier new; background: white; color: black; font-size: 10pt">   <p style="margin: 0px"><span style="color: blue">using</span> System;</p>    <p style="margin: 0px"><span style="color: blue">using</span> System.Diagnostics;</p>    <p style="margin: 0px"><span style="color: blue">using</span> System.Net.Sockets;</p>    <p style="margin: 0px"><span style="color: blue">using</span> System.Runtime.InteropServices;</p>    <p style="margin: 0px"><span style="color: blue">using</span> System.Security.Cryptography;</p>    <p style="margin: 0px"><span style="color: blue">using</span> System.Security.Cryptography.X509Certificates;</p>    <p style="margin: 0px"><span style="color: blue">using</span> System.Text;</p>    <p style="margin: 0px"><span style="color: blue">using</span> System.Globalization;</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px"><span style="color: blue">namespace</span> SslTest</p>    <p style="margin: 0px">{</p>    <p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">SslHelper</span> : <span style="color: #2b91af">IDisposable</span></p>    <p style="margin: 0px">&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">ushort</span> SO_SECURE = 0x2001;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">ushort</span> SO_SEC_SSL = 0x2004;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> _SO_SSL_FLAGS = 0x02;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> _SO_SSL_VALIDATE_CERT_HOOK = 0x08;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> SO_SSL_FAMILY = 0x00730000;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">long</span> _SO_SSL = ((2L &lt;&lt; 27) | SO_SSL_FAMILY);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">uint</span> IOC_IN = 0x80000000;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">long</span> SO_SSL_SET_VALIDATE_CERT_HOOK = (IOC_IN | _SO_SSL | _SO_SSL_VALIDATE_CERT_HOOK);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">long</span> SO_SSL_SET_FLAGS = (IOC_IN | _SO_SSL | _SO_SSL_FLAGS);</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> SSL_CERT_X59&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; = 1;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> SSL_ERR_OKAY&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; = 0;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> SSL_ERR_FAILED&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; = 2;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> SSL_ERR_BAD_LEN&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; = 3;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> SSL_ERR_BAD_TYPE&#160;&#160;&#160;&#160;&#160;&#160;&#160; = 4;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> SSL_ERR_BAD_DATA&#160;&#160;&#160;&#160;&#160;&#160;&#160; = 5;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> SSL_ERR_NO_CERT&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; = 6;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> SSL_ERR_BAD_SIG&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; = 7;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> SSL_ERR_CERT_EXPIRED&#160;&#160;&#160; = 8;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> SSL_ERR_CERT_REVOKED&#160;&#160;&#160; = 9;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> SSL_ERR_CERT_UNKNOWN&#160;&#160;&#160; = 10;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> SSL_ERR_SIGNATURE&#160;&#160;&#160;&#160;&#160;&#160; = 11;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">int</span> SSL_CERT_FLAG_ISSUER_UNKNOWN = 0x0001;</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">delegate</span> <span style="color: blue">int</span> <span style="color: #2b91af">SSLVALIDATECERTFUNC</span>(<span style="color: blue">uint</span> dwType, <span style="color: #2b91af">IntPtr</span> pvArg, <span style="color: blue">uint</span> dwChainLen, <span style="color: #2b91af">IntPtr</span> pCertChain, <span style="color: blue">uint</span> dwFlags);&#160;&#160;&#160;&#160;&#160;&#160;&#160; </p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: #2b91af">IntPtr</span> ptrHost;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: #2b91af">IntPtr</span> hookFunc;</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> SslHelper(<span style="color: #2b91af">Socket</span> socket, <span style="color: blue">string</span> host)</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//The managed SocketOptionName enum doesn't have SO_SECURE so here we cast the integer value</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; socket.SetSocketOption(<span style="color: #2b91af">SocketOptionLevel</span>.Socket, (<span style="color: #2b91af">SocketOptionName</span>)SO_SECURE, SO_SEC_SSL);</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//We need to pass a function pointer and a pointer to a string containing the host</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//to unmanaged code</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; hookFunc = <span style="color: #2b91af">Marshal</span>.GetFunctionPointerForDelegate(<span style="color: blue">new</span> <span style="color: #2b91af">SSLVALIDATECERTFUNC</span>(ValidateCert));</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Allocate the buffer for the string</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ptrHost = <span style="color: #2b91af">Marshal</span>.AllocHGlobal(host.Length + 1);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WriteASCIIString(ptrHost, host);</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Now put both pointers into a byte[]</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> inBuffer = <span style="color: blue">new</span> <span style="color: blue">byte</span>[8];</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> hookFuncBytes = <span style="color: #2b91af">BitConverter</span>.GetBytes(hookFunc.ToInt32());</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> hostPtrBytes = <span style="color: #2b91af">BitConverter</span>.GetBytes(ptrHost.ToInt32());</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Array</span>.Copy(hookFuncBytes, inBuffer, hookFuncBytes.Length);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Array</span>.Copy(hostPtrBytes, 0, inBuffer, hookFuncBytes.Length, hostPtrBytes.Length);</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">unchecked</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; socket.IOControl((<span style="color: blue">int</span>)SO_SSL_SET_VALIDATE_CERT_HOOK, inBuffer, <span style="color: blue">null</span>);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">void</span> WriteASCIIString(<span style="color: #2b91af">IntPtr</span> basePtr, <span style="color: blue">string</span> s)</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">byte</span>[] bytes = <span style="color: #2b91af">Encoding</span>.ASCII.GetBytes(s);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">for</span> (<span style="color: blue">int</span> i = 0; i &lt; bytes.Length; i++)</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Marshal</span>.WriteByte(basePtr, i, bytes[i]);</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//null terminate the string</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Marshal</span>.WriteByte(basePtr, bytes.Length, 0);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160; #region</span> IDisposable Members</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; ~SslHelper()</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ReleaseHostPointer();</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Dispose()</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">GC</span>.SuppressFinalize(<span style="color: blue">this</span>);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ReleaseHostPointer();</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">void</span> ReleaseHostPointer()</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (ptrHost != <span style="color: #2b91af">IntPtr</span>.Zero)</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Marshal</span>.FreeHGlobal(ptrHost);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ptrHost = <span style="color: #2b91af">IntPtr</span>.Zero;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160; #endregion</span></p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">int</span> ValidateCert(<span style="color: blue">uint</span> dwType, <span style="color: #2b91af">IntPtr</span> pvArg, <span style="color: blue">uint</span> dwChainLen, <span style="color: #2b91af">IntPtr</span> pCertChain, <span style="color: blue">uint</span> dwFlags)</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {&#160; </p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//According to https://msdn.microsoft.com/en-us/library/ms940451.aspx:</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//- dwChainLen is always 1</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//- Windows CE performs the cert chain validation</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//- pvArg is the context data we passed into the SO_SSL_SET_VALIDATE_CERT_HOOK call so in our</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//- case is the host name</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//So here we are responsible for validating the dates on the certificate and the CN</span></p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (dwType != SSL_CERT_X59)</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> SSL_ERR_BAD_TYPE;</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//When in debug mode let self-signed certificates through ...</span></p>    <p style="margin: 0px"><span style="color: blue">#if</span> !DEBUG</p>    <p style="margin: 0px"><span style="color: gray">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if ((dwFlags &amp; SSL_CERT_FLAG_ISSUER_UNKNOWN) != 0)</span></p>    <p style="margin: 0px"><span style="color: gray">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return SSL_ERR_CERT_UNKNOWN;</span></p>    <p style="margin: 0px"><span style="color: blue">#endif</span></p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Debug</span>.Assert(dwChainLen == 1);</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px"><span style="color: green">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; //Note about the note: an unmanaged long is 32 bits, unlike a managed long which is 64. I was missing </span></p>    <p style="margin: 0px"><span style="color: green">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; //this fact when I wrote the comment. So the docs are accurate.</span></p>    <p style="margin: 0px"><span style="color: green"></span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//NOTE: The documentation says pCertChain is a pointer to a LPBLOB struct:</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// {ulong size, byte* data} </span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//in reality the size is a 32 bit integer (not 64).</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">int</span> certSize = <span style="color: #2b91af">Marshal</span>.ReadInt32(pCertChain);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IntPtr</span> pData = <span style="color: #2b91af">Marshal</span>.ReadIntPtr(<span style="color: blue">new</span> <span style="color: #2b91af">IntPtr</span>(pCertChain.ToInt32() + <span style="color: blue">sizeof</span>(<span style="color: blue">int</span>)));</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">byte</span>[] certData = <span style="color: blue">new</span> <span style="color: blue">byte</span>[certSize];</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">for</span> (<span style="color: blue">int</span> i = 0; i &lt; certSize; i++)</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; certData[i] = <span style="color: #2b91af">Marshal</span>.ReadByte(pData, (<span style="color: blue">int</span>)i);</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">X509Certificate2</span> cert;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">try</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cert = <span style="color: blue">new</span> <span style="color: #2b91af">X509Certificate2</span>(certData);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">catch</span> (<span style="color: #2b91af">ArgumentException</span>) { <span style="color: blue">return</span> SSL_ERR_BAD_DATA; }</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">catch</span> (<span style="color: #2b91af">CryptographicException</span>) { <span style="color: blue">return</span> SSL_ERR_BAD_DATA; }</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Validate the expiration date</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (<span style="color: #2b91af">DateTime</span>.Now &gt; <span style="color: #2b91af">DateTime</span>.Parse(cert.GetExpirationDateString(), <span style="color: #2b91af">CultureInfo</span>.CurrentCulture))</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> SSL_ERR_CERT_EXPIRED;</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Validate the effective date</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (<span style="color: #2b91af">DateTime</span>.Now &lt; <span style="color: #2b91af">DateTime</span>.Parse(cert.GetEffectiveDateString(), <span style="color: #2b91af">CultureInfo</span>.CurrentCulture))</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> SSL_ERR_FAILED;</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">string</span> certName = cert.GetName();</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Debug</span>.WriteLine(certName);</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">//Validate the CN</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">string</span> host = ReadAnsiString(pvArg);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!certName.Contains(<span style="color: #a31515">&quot;CN=&quot;</span> + host))</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> SSL_ERR_FAILED;</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> SSL_ERR_OKAY;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>    <p style="margin: 0px">&#160;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">string</span> ReadAnsiString(<span style="color: #2b91af">IntPtr</span> pvArg)</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">byte</span>[] buffer = <span style="color: blue">new</span> <span style="color: blue">byte</span>[1024];</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">int</span> j = 0;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">do</span></p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; buffer[j] = <span style="color: #2b91af">Marshal</span>.ReadByte(pvArg, j);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; j++;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } <span style="color: blue">while</span> (buffer[j - 1] != 0);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">string</span> host = <span style="color: #2b91af">Encoding</span>.ASCII.GetString(buffer, 0, j - 1);</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> host;</p>    <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>    <p style="margin: 0px">&#160;&#160;&#160; }</p>    <p style="margin: 0px">}</p> </div>]]></content:encoded>
    </item>
    <item>
      <title>New to the blogosphere</title>
      <link>https://docs.microsoft.com/archive/blogs/cgarcia/new-to-the-blogosphere</link>
      <pubDate>Fri, 21 Aug 2009 14:00:19 GMT</pubDate>
      <dc:creator><![CDATA[Carlos [MSR]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cgarcia/2009/08/21/new-to-the-blogosphere/</guid>
      <description><![CDATA[Finally, I decided to start a blog. This is something I’ve been thinking about for a while but kept...]]></description>
      <content:encoded><![CDATA[<p>Finally, I decided to start a blog. This is something I’ve been thinking about for a while but kept putting off. This week I attended <a href="https://devscovery.com">Devscovery</a> and the keynote by <a href="https://www.hanselman.com/blog/">Scott Hanselman</a> was all about social networking for developers and I guess it gave me the nudge I needed to get started. By the way, if you can attend one of this events by Wintellect I’d recommend it.</p>  <p>So who am I? My name is Carlos Garcia Jurado Suarez and I’m a developer in <a href="https://research.microsoft.com/en-us/people/cgarcia">Microsoft Research</a>. I’ve been with Microsoft for 9 years and I spent the first half in Visual Studio working on things like the Class Designer and Application Designer. Nowadays I work on a variety of projects from prototypes to tech transfers as part of Research’s <a href="https://research.microsoft.com/en-us/groups/adt/">Advanced Development Team</a>. It’s a lot of fun as I get to explore a very wide range of technologies and work with some very interesting people.</p>  <p>I expect most of my posts to be about interesting technical problems I run into. To get started I’ll be posting shortly about some fun I had with SSL on windows mobile which I hope people will find useful. </p>]]></content:encoded>
    </item>
  </channel>
</rss>