<?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>Practice &amp; Share</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/practice-sharing/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/practice-sharing/feed.xml</link>
    <description>Practice &amp;amp; Share</description>
    <lastBuildDate>Tue, 16 Apr 2019 05:24:18 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Introduce the SSO in Office 365 Applications</title>
      <link>https://docs.microsoft.com/archive/blogs/practice-sharing/introduce-the-sso-in-office-365-applications</link>
      <pubDate>Thu, 31 Mar 2016 02:36:56 GMT</pubDate>
      <dc:creator><![CDATA[Jeffrey_Chen_]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/practice-sharing/?p=102</guid>
      <description><![CDATA[Overview of Single Sign-on



The whole process of SSO solutions is above. When a user access...]]></description>
      <content:encoded><![CDATA[Overview of Single Sign-on

<a href="https://msdnshared.blob.core.windows.net/media/2016/03/sso-slution-for-office365-apps.png"><img class="alignnone  wp-image-104" src="https://msdnshared.blob.core.windows.net/media/2016/03/sso-slution-for-office365-apps.png" alt="sso-slution-for-office365-apps" width="716" height="547" /></a>

The whole process of SSO solutions is above. When a user access another web app using the same Identity Provider, the step 4 and step 5 will not be required (user do not need to fill the user name and password again).

Let’s take the SharePoint Online as an example, if a user wants to access the resource on <a href="https://domain-name.sharepoint.com/officedev">https://domain-name.sharepoint.com/officedev</a>.

<a href="https://msdnshared.blob.core.windows.net/media/2016/03/sharepoint-signin-process.png"><img class="alignnone  wp-image-114" src="https://msdnshared.blob.core.windows.net/media/2016/03/sharepoint-signin-process-1024x606.png" alt="sharepoint-signin-process" width="794" height="470" /></a>

[1] go to https://domain-name.sharepoint.com/officedev

[2] redirect to /Authenticate.aspx?Source=/officedev

[3] go to /Authenticate.aspx?Source=/officedev

[4] redirect to /_forms/default.aspx?ReturnUrl=/officedev/_layouts/15/Authenticate.aspx?Source=/officedev&amp;Source=cookie

[5] go to /_forms/default.aspx?ReturnUrl=/officedev/_layouts/15/Authenticate.aspx?Source=/officedev&amp;Source=cookie

[6] redirect to https://login.microsoftonline.com/login.srf?wa=wsignin1.0&amp;rpsnv=4&amp;ct=1459338000&amp;rver=6.1.6206.0&amp;wp=MBI&amp;wreply=https://domain-name.sharepoint.com/_forms/default.aspx?apr=1&amp;lc=1033&amp;id=500046&amp;guests=1&amp;client-request-id=f9976d9d-601d-3000-1639-df8af2637d47

[7] go to https://login.microsoftonline.com/login.srf?wa=wsignin1.0&amp;rpsnv=4&amp;ct=1459338000&amp;rver=6.1.6206.0&amp;wp=MBI&amp;wreply=https://domain-name.sharepoint.com/_forms/default.aspx?apr=1&amp;lc=1033&amp;id=500046&amp;guests=1&amp;client-request-id=f9976d9d-601d-3000-1639-df8af2637d47

[8] response with the login page

[9] submit the user name and password to login

[10] response a html page which will post a request in javascript.

[11] post to https://domain-name.sharepoint.com/_forms/default.aspx?apr=1&amp;wa=wsignin1.0 [body: signed ticket]

[12] response to set the auth cookie [FedAuth] and redirect to /officedev/_layouts/15/Authenticate.aspx?Source=%2Fofficedev

[13] go to /officedev/_layouts/15/Authenticate.aspx?Source=/officedev [with cookie FedAuth]

[14] response: redirect to <a href="https://domain-name.sharepoint.com/officedev">https://domain-name.sharepoint.com/officedev</a>

[15] go to <a href="https://domain-name.sharepoint.com/officedev">https://domain-name.sharepoint.com/officedev</a>

&nbsp;

&nbsp;]]></content:encoded>
    </item>
    <item>
      <title>[Office 365 Dev] How to Validate the Access Token Issued by Microsoft Azure AD</title>
      <link>https://docs.microsoft.com/archive/blogs/practice-sharing/office-365-dev-how-to-validate-the-access-token-issued-by-microsoft-azure-ad</link>
      <pubDate>Tue, 22 Mar 2016 05:57:53 GMT</pubDate>
      <dc:creator><![CDATA[Jeffrey_Chen_]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/practice-sharing/?p=81</guid>
      <description><![CDATA[By following the document Authorization Code Grant Flow, we can get the access token to access the...]]></description>
      <content:encoded><![CDATA[By following the document <a href="https://msdn.microsoft.com/en-us/library/azure/dn645542.aspx">Authorization Code Grant Flow</a>, we can get the access token to access the declared resources.

This article will introduce how is the access token being validated on the <strong>Resource Web API</strong> side.

<a href="https://msdnshared.blob.core.windows.net/media/2016/03/web-resource-api.png"><img class="alignnone wp-image-83 " src="https://msdnshared.blob.core.windows.net/media/2016/03/web-resource-api.png" alt="web-resource-api" width="720" height="424" /></a>

The access token from the Azure AD is a JSON Web Token(JWT) which is signed by Security Token Service in private key.

The JWT includes 3 parts: header, data and signature. Technically, we can use the public key to validate the access token.

First step – retrieve and cache the singing tokens (public key)

Endpoint: <a href="https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration">https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration</a>

<a href="https://msdnshared.blob.core.windows.net/media/2016/03/open-connectid-config.png"><img class="alignnone size-full wp-image-93" src="https://msdnshared.blob.core.windows.net/media/2016/03/open-connectid-config.png" alt="open-connectid-config" width="1088" height="723" /></a>

Code:

<!-- HTML generated using hilite.me -->
<div style="background: #ffffff;overflow: auto;width: auto;border: solid gray;border-width: .1em .1em .1em .8em;padding: .2em .6em">
<pre style="margin: 0;line-height: 125%"><span style="color: #333399;font-weight: bold">string</span> stsDiscoveryEndpoint = <span style="background-color: #fff0f0">"https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration"</span>;

ConfigurationManager&lt;OpenIdConnectConfiguration&gt; configManager = <span style="color: #008800;font-weight: bold">new</span> ConfigurationManager&lt;OpenIdConnectConfiguration&gt;(stsDiscoveryEndpoint);

OpenIdConnectConfiguration config = configManager.GetConfigurationAsync().Result;
</pre>
</div>
&nbsp;

Second step – validate the token via JwtSecurityTokenHandler

Code:
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff;overflow: auto;width: auto;border: solid gray;border-width: .1em .1em .1em .8em;padding: .2em .6em">
<pre style="margin: 0;line-height: 125%">TokenValidationParameters validationParameters = <span style="color: #008800;font-weight: bold">new</span> TokenValidationParameters
            {
                ValidateAudience = <span style="color: #008800;font-weight: bold">false</span>,
                ValidateIssuer = <span style="color: #008800;font-weight: bold">false</span>,
                IssuerSigningTokens = config.SigningTokens,
                ValidateLifetime = <span style="color: #008800;font-weight: bold">false</span>
            };

JwtSecurityTokenHandler tokendHandler = <span style="color: #008800;font-weight: bold">new</span> JwtSecurityTokenHandler();

SecurityToken jwt;

            <span style="color: #333399;font-weight: bold">var</span> result = tokendHandler.ValidateToken(token, validationParameters, <span style="color: #008800;font-weight: bold">out</span> jwt);

<span style="color: #008800;font-weight: bold">return</span> jwt <span style="color: #008800;font-weight: bold">as</span> JwtSecurityToken;
</pre>
</div>
Complete code on GitHub: <a href="https://github.com/dream-365/OfficeDev-Samples/blob/master/samples/Office365DevQuickStart/OAuth2-basic/JsonWebTokenValidator.cs">https://github.com/dream-365/OfficeDev-Samples/blob/master/samples/Office365DevQuickStart/OAuth2-basic/JsonWebTokenValidator.cs</a>]]></content:encoded>
    </item>
    <item>
      <title>[Bluetooth] - How to Host a RFCOMM Service in C++</title>
      <link>https://docs.microsoft.com/archive/blogs/practice-sharing/bluetooth-how-to-host-a-rfcomm-service-in-c</link>
      <pubDate>Sun, 16 Aug 2015 17:20:00 GMT</pubDate>
      <dc:creator><![CDATA[Jeffrey_Chen_]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/practice-sharing/2015/08/16/bluetooth-how-to-host-a-rfcomm-service-in-c/</guid>
      <description><![CDATA[Introduction
Bluetooth is an industry-standard protocol that enables wireless connectivity for...]]></description>
      <content:encoded><![CDATA[<h2>Introduction</h2>
<p>Bluetooth is an industry-standard protocol that enables wireless connectivity for computers, handheld devices, mobile phones, and other devices.</p>
<p>RFCOMM emulates RS-232 (ITU-T V.24) serial ports. The emulation includes transfer of the state of the non-data circuits. RFCOMM has a built-in scheme for null modem emulation.</p>
<h2>Service Definition Model</h2>
<p>&nbsp;<a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/4530.7.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/4530.7.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/4530.7.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/4530.7.png" alt="" border="0" /></a></p>
<p>&nbsp;</p>
<p>This blog will introduce how to use Windows Sockets functions to host a Bluetooth RFCOMM service, and use the Windows Runtime API to consume this service.</p>
<p><span style="font-size: large;"><strong>Steps:</strong></span></p>
<p style="padding-left: 30px;">#1 ask for Windows Sockets API (WSA) 2.2</p>
<p style="padding-left: 30px;">The socket initialization only be required once for one process, so I put the initialization code in a static function.</p>
<p style="padding-left: 30px;">#2 create a Windows socket</p>
<p style="padding-left: 30px;">#3 create a Bluetooth address and bind to Windows socket</p>
<p style="padding-left: 30px;">#4 register a Bluetooth service (SDP)</p>
<p style="padding-left: 30px;">#5 listen to the Windows socket</p>
<p>&nbsp;</p>
<p><span style="font-size: medium;"><strong># CODE #</strong></span></p>
<p>RFCOMM Server (C++):</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #557799;">#include "stdafx.h"</span>
<span style="color: #557799;">#include "Winsock.h"</span>
<span style="color: #557799;">#include &lt;WinSock2.h&gt;</span>
<span style="color: #557799;">#include &lt;ws2bth.h&gt;</span>
<span style="color: #557799;">#include &lt;bthsdpdef.h&gt;</span>
<span style="color: #557799;">#include &lt;bluetoothapis.h&gt;</span>

<span style="color: #557799;">#pragma comment(lib, "ws2_32.lib")</span>
<span style="color: #557799;">#pragma comment(lib, "irprops.lib")</span>

<span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">main</span>()
{
	<span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #333333;">!</span>Winsock<span style="color: #333333;">::</span>InitialzedForCurrentProcess())
	{
		<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0000dd; font-weight: bold;">1</span>;
	}

	Winsock winsock <span style="color: #333333;">=</span> Winsock(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);

	<span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #333333;">!</span>winsock.IsValid())
	{
		<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0000dd; font-weight: bold;">1</span>;
	}

	winsock.Bind();

	winsock.Listen();

	winsock.RegisterServcie(_T(<span style="background-color: #fff0f0;">"RFCOMM Server Demo Instance"</span>), _T(<span style="background-color: #fff0f0;">"Pushing data to PC"</span>), OBEXObjectPushServiceClass_UUID);

	winsock.OnConnected([](SOCKET s){
		<span style="color: #333399; font-weight: bold;">char</span> buffer[<span style="color: #0000dd; font-weight: bold;">1024</span>] <span style="color: #333333;">=</span> { <span style="color: #0000dd; font-weight: bold;">0</span> };

		memset(buffer, <span style="color: #0000dd; font-weight: bold;">0</span>, <span style="color: #008800; font-weight: bold;">sizeof</span>(buffer));

		<span style="color: #333399; font-weight: bold;">int</span> r <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;

		<span style="color: #008800; font-weight: bold;">do</span>
		{
			r <span style="color: #333333;">=</span> recv(s, (<span style="color: #333399; font-weight: bold;">char</span><span style="color: #333333;">*</span>)buffer, <span style="color: #008800; font-weight: bold;">sizeof</span>(buffer), <span style="color: #0000dd; font-weight: bold;">0</span>);

			printf(<span style="background-color: #fff0f0;">"result:%d, %s</span><span style="color: #666666; font-weight: bold; background-color: #fff0f0;">\n</span><span style="background-color: #fff0f0;">"</span>, r, buffer);

		} <span style="color: #008800; font-weight: bold;">while</span> (r <span style="color: #333333;">!=</span> <span style="color: #0000dd; font-weight: bold;">0</span>);

		closesocket(s);
	});

	winsock.Accept();

	WSACleanup();

    <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
}
</pre>
</div>
<p>Code on Github:&nbsp;https://github.com/dream-365/winapp/tree/master/BluetoothPlay</p>]]></content:encoded>
    </item>
    <item>
      <title>[XAML] - Create Your Own Number Picker Control for Windows Phone</title>
      <link>https://docs.microsoft.com/archive/blogs/practice-sharing/xaml-create-your-own-number-picker-control-for-windows-phone</link>
      <pubDate>Sat, 15 Aug 2015 18:59:00 GMT</pubDate>
      <dc:creator><![CDATA[Jeffrey_Chen_]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/practice-sharing/2015/08/15/xaml-create-your-own-number-picker-control-for-windows-phone/</guid>
      <description><![CDATA[This is the screenshot of the number picker control I created.

&nbsp;
Steps:
#1 in Visual...]]></description>
      <content:encoded><![CDATA[<p>This is the screenshot of the number picker control I created.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/1030.6.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/1030.6.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/1030.6.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/1030.6.png" alt="" border="0" /></a></p>
<p>&nbsp;</p>
<p><span style="text-decoration: underline; font-size: large;"><strong>Steps:</strong></span></p>
<p><strong>#1 in Visual Studio, Add -&gt; New Item -&gt; User Control, change the <span style="background-color: #ffff00;">d:DesignHeight</span>&nbsp; to 50 and <span style="background-color: #ffff00;">d:DesignWidth</span> to 300</strong></p>
<p>To have some shadow effect, we need a border to wrap and the border need to have the same width and height with the user control.</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #007700;">&lt;UserControl</span>
 <span style="color: #0000cc;">x:Name=</span><span style="background-color: #fff0f0;">"control"</span>
 <span style="color: #0000cc;">x:Class=</span><span style="background-color: #fff0f0;">"WP81ControlsDemo.NumberPicker"</span>
 <span style="color: #0000cc;">xmlns=</span><span style="background-color: #fff0f0;">"https://schemas.microsoft.com/winfx/2006/xaml/presentation"</span>
 <span style="color: #0000cc;">xmlns:x=</span><span style="background-color: #fff0f0;">"https://schemas.microsoft.com/winfx/2006/xaml"</span>
 <span style="color: #0000cc;">xmlns:local=</span><span style="background-color: #fff0f0;">"using:WP81ControlsDemo"</span>
 <span style="color: #0000cc;">xmlns:d=</span><span style="background-color: #fff0f0;">"https://schemas.microsoft.com/expression/blend/2008"</span>
 <span style="color: #0000cc;">xmlns:mc=</span><span style="background-color: #fff0f0;">"https://schemas.openxmlformats.org/markup-compatibility/2006"</span>
 <span style="color: #0000cc;">mc:Ignorable=</span><span style="background-color: #fff0f0;">"d"</span>
 <span style="color: #0000cc;">d:DesignHeight=</span><span style="background-color: #fff0f0;">"50"</span>
 <span style="color: #0000cc;">d:DesignWidth=</span><span style="background-color: #fff0f0;">"300"</span><span style="color: #007700;">&gt;</span>
 <span style="color: #007700;">&lt;Border</span>
 <span style="color: #0000cc;">BorderBrush=</span><span style="background-color: #fff0f0;">"#FFF2F2F2"</span> 
 <span style="color: #0000cc;">BorderThickness=</span><span style="background-color: #fff0f0;">"2"</span>
 <span style="color: #0000cc;">Width=</span><span style="background-color: #fff0f0;">"{Binding ElementName=control, Path=Width}"</span>
 <span style="color: #0000cc;">Height=</span><span style="background-color: #fff0f0;">"{Binding ElementName=control, Path=Height}"</span>
 <span style="color: #007700;">&gt;</span>
 <span style="color: #007700;">&lt;/Border&gt;</span>
 <span style="color: #007700;">&lt;/UserControl&gt;</span>
</pre>
</div>
<p><strong>#2 Layout the UI elements using Grid control</strong></p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #007700;">&lt;Grid</span> <span style="color: #0000cc;">HorizontalAlignment=</span><span style="background-color: #fff0f0;">"Stretch"</span> <span style="color: #0000cc;">Background=</span><span style="background-color: #fff0f0;">"White"</span><span style="color: #007700;">&gt;</span>
 <span style="color: #007700;">&lt;Grid.ColumnDefinitions&gt;</span>
 <span style="color: #007700;">&lt;ColumnDefinition</span> <span style="color: #0000cc;">Width=</span><span style="background-color: #fff0f0;">"50"</span><span style="color: #007700;">&gt;&lt;/ColumnDefinition&gt;</span>
 <span style="color: #007700;">&lt;ColumnDefinition</span> <span style="color: #0000cc;">Width=</span><span style="background-color: #fff0f0;">"*"</span><span style="color: #007700;">&gt;&lt;/ColumnDefinition&gt;</span>
 <span style="color: #007700;">&lt;ColumnDefinition</span> <span style="color: #0000cc;">Width=</span><span style="background-color: #fff0f0;">"50"</span><span style="color: #007700;">&gt;&lt;/ColumnDefinition&gt;</span>
 <span style="color: #007700;">&lt;/Grid.ColumnDefinitions&gt;</span>
 <span style="color: #007700;">&lt;Border</span> 
 <span style="color: #0000cc;">BorderBrush=</span><span style="background-color: #fff0f0;">"#FFF2F2F2"</span> 
 <span style="color: #0000cc;">BorderThickness=</span><span style="background-color: #fff0f0;">"0,0,3,0"</span>
 <span style="color: #0000cc;">Tapped=</span><span style="background-color: #fff0f0;">"Reduce_Tapped"</span>
 <span style="color: #007700;">&gt;</span>
 <span style="color: #007700;">&lt;TextBlock</span> 
 <span style="color: #0000cc;">FontSize=</span><span style="background-color: #fff0f0;">"25"</span> 
 <span style="color: #0000cc;">Grid.Column=</span><span style="background-color: #fff0f0;">"0"</span>
 <span style="color: #0000cc;">HorizontalAlignment=</span><span style="background-color: #fff0f0;">"Center"</span>
 <span style="color: #0000cc;">VerticalAlignment=</span><span style="background-color: #fff0f0;">"Center"</span>
 <span style="color: #0000cc;">Foreground=</span><span style="background-color: #fff0f0;">"#FF72D3FF"</span>
 
 <span style="color: #007700;">&gt;</span>-<span style="color: #007700;">&lt;/TextBlock&gt;</span>
 <span style="color: #007700;">&lt;/Border&gt;</span>
 <span style="color: #007700;">&lt;TextBlock</span>
 <span style="color: #0000cc;">x:Name=</span><span style="background-color: #fff0f0;">"content"</span>
 <span style="color: #0000cc;">FontSize=</span><span style="background-color: #fff0f0;">"25"</span> 
 <span style="color: #0000cc;">Grid.Column=</span><span style="background-color: #fff0f0;">"1"</span>
 <span style="color: #0000cc;">HorizontalAlignment=</span><span style="background-color: #fff0f0;">"Center"</span>
 <span style="color: #0000cc;">VerticalAlignment=</span><span style="background-color: #fff0f0;">"Center"</span>
 <span style="color: #0000cc;">Foreground=</span><span style="background-color: #fff0f0;">"#FF72D3FF"</span>
 <span style="color: #007700;">&gt;</span>0<span style="color: #007700;">&lt;/TextBlock&gt;</span>
 <span style="color: #007700;">&lt;Border</span> 
 <span style="color: #0000cc;">BorderBrush=</span><span style="background-color: #fff0f0;">"#FFF2F2F2"</span> 
 <span style="color: #0000cc;">BorderThickness=</span><span style="background-color: #fff0f0;">"2,0,0,0"</span>
 <span style="color: #0000cc;">Grid.Column=</span><span style="background-color: #fff0f0;">"2"</span>
 <span style="color: #0000cc;">Tapped=</span><span style="background-color: #fff0f0;">"Increase_Tapped"</span>
 <span style="color: #007700;">&gt;</span>
 <span style="color: #007700;">&lt;TextBlock</span> 
 <span style="color: #0000cc;">FontSize=</span><span style="background-color: #fff0f0;">"25"</span> 
 <span style="color: #0000cc;">HorizontalAlignment=</span><span style="background-color: #fff0f0;">"Center"</span>
 <span style="color: #0000cc;">VerticalAlignment=</span><span style="background-color: #fff0f0;">"Center"</span>
 <span style="color: #0000cc;">Foreground=</span><span style="background-color: #fff0f0;">"#FF72D3FF"</span>
 <span style="color: #007700;">&gt;</span>+<span style="color: #007700;">&lt;/TextBlock&gt;</span>
 <span style="color: #007700;">&lt;/Border&gt;</span>
 <span style="color: #007700;">&lt;/Grid&gt;</span>
 
</pre>
</div>
<p><strong>#3 code behind</strong></p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">sealed</span> <span style="color: #008800; font-weight: bold;">partial</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">NumberPicker</span> : UserControl
 {
 <span style="color: #008800; font-weight: bold;">public</span> <span style="color: #0066bb; font-weight: bold;">NumberPicker</span>()
 {
 <span style="color: #008800; font-weight: bold;">this</span>.InitializeComponent();
 }
 <span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">Reduce_Tapped</span>(<span style="color: #333399; font-weight: bold;">object</span> sender, TappedRoutedEventArgs e)
 {
 <span style="color: #333399; font-weight: bold;">var</span> currentValue = <span style="color: #333399; font-weight: bold;">int</span>.Parse(content.Text);
 
 <span style="color: #008800; font-weight: bold;">if</span>(currentValue == <span style="color: #6600ee; font-weight: bold;">0</span>)
 {
 <span style="color: #008800; font-weight: bold;">return</span>;
 }
 content.Text = (currentValue - <span style="color: #6600ee; font-weight: bold;">1</span>).ToString();
 }
 
 <span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">Increase_Tapped</span>(<span style="color: #333399; font-weight: bold;">object</span> sender, TappedRoutedEventArgs e)
 {
 <span style="color: #333399; font-weight: bold;">var</span> currentValue = <span style="color: #333399; font-weight: bold;">int</span>.Parse(content.Text);
 content.Text = (currentValue + <span style="color: #6600ee; font-weight: bold;">1</span>).ToString();
 }
 }
</pre>
</div>]]></content:encoded>
    </item>
    <item>
      <title>[Universal Windows App] - CalendarView Customization</title>
      <link>https://docs.microsoft.com/archive/blogs/practice-sharing/universal-windows-app-calendarview-customization</link>
      <pubDate>Wed, 12 Aug 2015 01:58:00 GMT</pubDate>
      <dc:creator><![CDATA[Jeffrey_Chen_]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/practice-sharing/2015/08/12/universal-windows-app-calendarview-customization/</guid>
      <description><![CDATA[The Universal Windows Platform introduces a new calendar control named &ldquo;CalendarView&rdquo;,...]]></description>
      <content:encoded><![CDATA[<p>The Universal Windows Platform introduces a new calendar control named &ldquo;CalendarView&rdquo;, but the default customization is very limited, to achieve some deep customization, you may consider overriding the default system styles.</p>
<p>In this blog, we will try to customize the UI to make it looks like the calendar in Calendar App.</p>
<p>&nbsp;&ldquo;Default CalendarView&rdquo; vs. &ldquo;calendar in Calendar App&rdquo;</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/7245.1.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/7245.1.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/7245.1.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/7245.1.png" alt="" border="0" /></a></p>
<p>Frist, let&rsquo;s start from the background color</p>
<p>&nbsp;<a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/6406.2.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/6406.2.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/6406.2.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/6406.2.png" alt="" border="0" /></a></p>
<p>It looks better, but the font size still does not match, change the font size to 11.</p>
<p>&nbsp;<a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/6567.3.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/6567.3.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/6567.3.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/6567.3.png" alt="" border="0" /></a></p>
<p>The font size was changed, but, the size of the item is still not changed, it is because in the definition of default style for CalendarViewDayItem, the MinWidth and MinHeight is set to 40. So, let&rsquo;s override the default setting.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/8228.4.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/8228.4.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/8228.4.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/8228.4.png" alt="" border="0" /></a></p>
<p>Looks better, but there are some borders, to hide them, set margin to 0.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/2072.5.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/2072.5.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/2072.5.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/2072.5.png" alt="" border="0" /></a>&nbsp;</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>[Universal Windows App] - How to Add Handler to Windows Runtime Event Dynamiclly</title>
      <link>https://docs.microsoft.com/archive/blogs/practice-sharing/universal-windows-app-how-to-add-handler-to-windows-runtime-event-dynamiclly</link>
      <pubDate>Mon, 10 Aug 2015 00:18:00 GMT</pubDate>
      <dc:creator><![CDATA[Jeffrey_Chen_]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/practice-sharing/2015/08/10/universal-windows-app-how-to-add-handler-to-windows-runtime-event-dynamiclly/</guid>
      <description><![CDATA[&nbsp;
Purpose: add a click event handler to all of the buttons in main page
&nbsp;
#1 Iterate...]]></description>
      <content:encoded><![CDATA[<p>&nbsp;</p>
<h1>Purpose: add a click event handler to all of the buttons in main page</h1>
<p>&nbsp;</p>
<p><strong>#1 Iterate all of the button elements in the main page.</strong></p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">bool</span> Is&lt;T&gt;(UIElement obj) <span style="color: #008800; font-weight: bold;">where</span> T : UIElement

{

    <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0066bb; font-weight: bold;">typeof</span>(T).IsAssignableFrom(obj.GetType());

}

 

<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">void</span> FindElement&lt;T&gt;(Panel parent, ICollection&lt;T&gt; collection) <span style="color: #008800; font-weight: bold;">where</span> T : UIElement

{

    <span style="color: #008800; font-weight: bold;">if</span> (parent == <span style="color: #008800; font-weight: bold;">null</span>)

    {

        <span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">ArgumentNullException</span>(<span style="background-color: #fff0f0;">"Augument parent can not be null!"</span>);

    }

    <span style="color: #008800; font-weight: bold;">foreach</span> (<span style="color: #333399; font-weight: bold;">var</span> ele <span style="color: #008800; font-weight: bold;">in</span> parent.Children)

    {

        <span style="color: #008800; font-weight: bold;">if</span>(Is&lt;T&gt;(ele))

        {

            collection.Add((T)ele);

        }<span style="color: #008800; font-weight: bold;">else</span> <span style="color: #008800; font-weight: bold;">if</span>(Is&lt;Panel&gt;(ele))

        {

            FindElement(ele <span style="color: #008800; font-weight: bold;">as</span> Panel, collection);

        }

    }

}
</pre>
</div>
<p>#2 Add handler to Windows Runtime Event</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">AddEventHandler</span>(Object target, <span style="color: #333399; font-weight: bold;">string</span> name, Delegate handler)

{

    <span style="color: #333399; font-weight: bold;">var</span> runtimeEvent = target.GetType().GetRuntimeEvent(<span style="background-color: #fff0f0;">"Click"</span>);

 

    Func&lt;Delegate, EventRegistrationToken&gt; <span style="color: #008800; font-weight: bold;">add</span> = (a) =&gt; {

        <span style="color: #008800; font-weight: bold;">return</span> (EventRegistrationToken)runtimeEvent.AddMethod.Invoke(target, <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">object</span>[] { a });

    };

 

    Action&lt;EventRegistrationToken&gt; <span style="color: #008800; font-weight: bold;">remove</span> = (a) =&gt; {

        runtimeEvent.RemoveMethod.Invoke(runtimeEvent, <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">object</span>[] { a });

    };

 

    WindowsRuntimeMarshal.AddEventHandler(<span style="color: #008800; font-weight: bold;">add</span>, <span style="color: #008800; font-weight: bold;">remove</span>, handler);

}
</pre>
</div>
<p>#3 Initialization</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;">List&lt;Button&gt; list = <span style="color: #008800; font-weight: bold;">new</span> List&lt;Button&gt;();

 

FindElement(<span style="color: #008800; font-weight: bold;">this</span>.Content <span style="color: #008800; font-weight: bold;">as</span> Panel, list);

 

<span style="color: #333399; font-weight: bold;">var</span> handlerMethodInfo = <span style="color: #008800; font-weight: bold;">this</span>.GetType().GetMethod(<span style="background-color: #fff0f0;">"OnClick"</span>);

 

<span style="color: #333399; font-weight: bold;">var</span> delegateHandler = handlerMethodInfo.CreateDelegate(<span style="color: #008800; font-weight: bold;">typeof</span>(RoutedEventHandler), <span style="color: #008800; font-weight: bold;">this</span>);

 

<span style="color: #008800; font-weight: bold;">foreach</span>(<span style="color: #333399; font-weight: bold;">var</span> button <span style="color: #008800; font-weight: bold;">in</span> list)

{

    AddEventHandler(button, <span style="background-color: #fff0f0;">"Click"</span>, delegateHandler);

}
</pre>
</div>]]></content:encoded>
    </item>
    <item>
      <title>[Universal Windows App] - How to Get the Last Write Time of a File From StorageFile</title>
      <link>https://docs.microsoft.com/archive/blogs/practice-sharing/universal-windows-app-how-to-get-the-last-write-time-of-a-file-from-storagefile</link>
      <pubDate>Fri, 07 Aug 2015 02:06:00 GMT</pubDate>
      <dc:creator><![CDATA[Jeffrey_Chen_]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/practice-sharing/2015/08/07/universal-windows-app-how-to-get-the-last-write-time-of-a-file-from-storagefile/</guid>
      <description><![CDATA[The build-in StorageFile class does not provide any property to get the last write time of a file...]]></description>
      <content:encoded><![CDATA[<p>The build-in StorageFile class does not provide any property to get the last write time of a file explicitly, but you can get it through GetBasicPropertiesAsync method. This blog just describe how to use the underlying Windows API to achieve it.</p>
<p>In this scenario, we need to use the underlying Windows API: CreateFile2 and GetFileTime.</p>
<p>In C++:</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;">create_task(Windows<span style="color: #333333;">::</span>ApplicationModel<span style="color: #333333;">::</span>Package<span style="color: #333333;">::</span>Current<span style="color: #333333;">-&gt;</span>InstalledLocation<span style="color: #333333;">-&gt;</span>GetFileAsync(<span style="background-color: #fff0f0;">"Assets</span><span style="color: #666666; font-weight: bold; background-color: #fff0f0;">\\</span><span style="background-color: #fff0f0;">StoreLogo.png"</span>)).then([<span style="color: #008800; font-weight: bold;">this</span>](StorageFile<span style="color: #333333;">^</span> file) {              

              HANDLE hFile <span style="color: #333333;">=</span> CreateFile2(file<span style="color: #333333;">-&gt;</span>Path<span style="color: #333333;">-&gt;</span>Data(), <span style="color: #888888;">// lpFileName</span>

                     GENERIC_READ,                        <span style="color: #888888;">// dwDesiredAccess</span>

                     FILE_SHARE_READ,                     <span style="color: #888888;">// dwShareMode</span>

                     OPEN_EXISTING,                       <span style="color: #888888;">//</span>

                     <span style="color: #007020;">NULL</span>);

              FILETIME lastModify;

              SYSTEMTIME systemTime;

              GetFileTime(hFile, <span style="color: #007020;">NULL</span>, <span style="color: #007020;">NULL</span>, <span style="color: #333333;">&amp;</span>lastModify);

              FileTimeToSystemTime(<span style="color: #333333;">&amp;</span>lastModify, <span style="color: #333333;">&amp;</span>systemTime);

              CloseHandle(hFile);

       });

 
</pre>
</div>
<p>In C#:</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;">        <span style="color: #008800; font-weight: bold;">const</span> <span style="color: #333399; font-weight: bold;">uint</span> GENERIC_READ             = <span style="color: #6600ee; font-weight: bold;">0</span>x80000000;

        <span style="color: #008800; font-weight: bold;">const</span> <span style="color: #333399; font-weight: bold;">uint</span> FILE_SHARE_READ          = <span style="color: #6600ee; font-weight: bold;">0</span>x00000001;

        <span style="color: #008800; font-weight: bold;">const</span> <span style="color: #333399; font-weight: bold;">uint</span> OPEN_EXISTING            = <span style="color: #6600ee; font-weight: bold;">3</span>;



<span style="color: #0000cc;"> [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]</span>

        <span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">extern</span> SafeFileHandle <span style="color: #0066bb; font-weight: bold;">CreateFile2</span>(

            <span style="color: #333399; font-weight: bold;">string</span> lpFileName,

            <span style="color: #333399; font-weight: bold;">uint</span> dwDesiredAccess,

            <span style="color: #333399; font-weight: bold;">uint</span> dwShareMode,

            <span style="color: #333399; font-weight: bold;">uint</span> dwCreationDisposition,

            IntPtr pCreateExParams);



<span style="color: #0000cc;"> [DllImport("kernel32.dll")]</span>

        <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">extern</span> <span style="color: #333399; font-weight: bold;">bool</span> <span style="color: #0066bb; font-weight: bold;">GetFileTime</span>(

            IntPtr hFile,

            <span style="color: #008800; font-weight: bold;">ref</span> FILETIME lpCreationTime,

            <span style="color: #008800; font-weight: bold;">ref</span> FILETIME lpLastAccessTime,

            <span style="color: #008800; font-weight: bold;">ref</span> FILETIME lpLastWriteTime);





<span style="color: #0000cc;"> [DllImport("kernel32.dll", SetLastError = true)]</span>

<span style="color: #0000cc;"> [return: MarshalAs(UnmanagedType.Bool)]</span>

        <span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">extern</span> <span style="color: #333399; font-weight: bold;">bool</span> <span style="color: #0066bb; font-weight: bold;">CloseHandle</span>(IntPtr hObject);



<span style="color: #0000cc;"> [StructLayout(LayoutKind.Sequential)]</span>

        <span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">struct</span> <span style="color: #bb0066; font-weight: bold;">FILETIME</span>

        {

            <span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">uint</span> dwLowDateTime;

            <span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">uint</span> dwHighDateTime;

        }

 

        <span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">async</span> <span style="color: #008800; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">Button_Click</span>(<span style="color: #333399; font-weight: bold;">object</span> sender, RoutedEventArgs e)

        {

            <span style="color: #333399; font-weight: bold;">var</span> file = <span style="color: #008800; font-weight: bold;">await</span> Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(<span style="background-color: #fff0f0;">@"Assets\StoreLogo.png"</span>);

 

            FILETIME fCreationTime = <span style="color: #008800; font-weight: bold;">new</span> FILETIME();

            FILETIME fLastAccessTime = <span style="color: #008800; font-weight: bold;">new</span> FILETIME();

            FILETIME fLastWriteTime = <span style="color: #008800; font-weight: bold;">new</span> FILETIME();

 

            <span style="color: #333399; font-weight: bold;">var</span> hFile = CreateFile2(file.Path, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, IntPtr.Zero);

 

            GetFileTime(hFile.DangerousGetHandle(), <span style="color: #008800; font-weight: bold;">ref</span> fCreationTime, <span style="color: #008800; font-weight: bold;">ref</span> fLastAccessTime, <span style="color: #008800; font-weight: bold;">ref</span> fLastWriteTime);

 

            <span style="color: #333399; font-weight: bold;">var</span> lastWriteTime = DateTime.FromFileTimeUtc((((<span style="color: #333399; font-weight: bold;">long</span>)fLastWriteTime.dwHighDateTime) &lt;&lt; <span style="color: #6600ee; font-weight: bold;">32</span>) | ((<span style="color: #333399; font-weight: bold;">uint</span>)fLastWriteTime.dwLowDateTime));

 

            hFile.DangerousRelease();

        }
</pre>
</div>]]></content:encoded>
    </item>
    <item>
      <title>How to Obtain the Data Pointer From the IMemoryBufferReference (C# &amp; C++)</title>
      <link>https://docs.microsoft.com/archive/blogs/practice-sharing/how-to-obtain-the-data-pointer-from-the-imemorybufferreference-c-c</link>
      <pubDate>Thu, 06 Aug 2015 01:21:00 GMT</pubDate>
      <dc:creator><![CDATA[Jeffrey_Chen_]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/practice-sharing/2015/08/06/how-to-obtain-the-data-pointer-from-the-imemorybufferreference-c-c/</guid>
      <description><![CDATA[The&nbsp;IMemoryBufferReference&nbsp;interface allows a client to retrieve the&nbsp;Capacity&nbsp;of...]]></description>
      <content:encoded><![CDATA[<p>The&nbsp;<strong>IMemoryBufferReference</strong>&nbsp;interface allows a client to retrieve the&nbsp;<a href="https://msdn.microsoft.com/en-us/library/windows.foundation.imemorybufferreference.capacity.aspx"><strong>Capacity</strong></a>&nbsp;of the memory buffer. The same object identity must also implement the COM interface&nbsp;<strong>IMemoryBufferByteAccess</strong>. A client retrieves the&nbsp;<strong>IMemoryBufferByteAccess</strong>&nbsp;interface pointer via a&nbsp;<strong>QueryInterface</strong>&nbsp;from the&nbsp;<strong>IMemoryBufferReference</strong>&nbsp;object.</p>
<p>To access a the data pointer in <strong>IMemoryBufferReference</strong>, we first need to get the pointer to <strong>IMemoryBufferByteAccess</strong>&nbsp;interface.</p>
<p>In C#, it will be very simple.</p>
<p>#1 Declare the COM interface</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #0000cc;">[ComImport]</span>

<span style="color: #0000cc;">[Guid("5b0d3235-4dba-4d44-865e-8f1d0e4fd04d")]</span>

<span style="color: #0000cc;">[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]</span>

<span style="color: #008800; font-weight: bold;">unsafe</span> <span style="color: #008800; font-weight: bold;">interface</span> IMemoryBufferByteAccess

{

  <span style="color: #008800; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">GetBuffer</span>(<span style="color: #008800; font-weight: bold;">out</span> <span style="color: #333399; font-weight: bold;">byte</span>* buffer, <span style="color: #008800; font-weight: bold;">out</span> <span style="color: #333399; font-weight: bold;">uint</span> capacity);

}
</pre>
</div>
<pre><br />#2 Get the underlying interface</pre>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #008800; font-weight: bold;">if</span> (reference <span style="color: #008800; font-weight: bold;">is</span> IMemoryBufferByteAccess)

{

  <span style="color: #888888;">// Get a pointer to the pixel buffer</span>

  <span style="color: #333399; font-weight: bold;">byte</span>* data;

  <span style="color: #333399; font-weight: bold;">uint</span> capacity;

 

  ((IMemoryBufferByteAccess)reference).GetBuffer(<span style="color: #008800; font-weight: bold;">out</span> data, <span style="color: #008800; font-weight: bold;">out</span> capacity);

  ********

}
</pre>
</div>
<p>CLR will handle the underlying things to get the <strong>IMemoryBufferByteAccess</strong>&nbsp;interface from reference object.</p>
<p>In C++, you may need to do all of these by yourself.</p>
<p>#1 Declare the COM interface</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;">MIDL_INTERFACE(<span style="background-color: #fff0f0;">"5b0d3235-4dba-4d44-865e-8f1d0e4fd04d"</span>)

IMemoryBufferByteAccess <span style="color: #333333;">:</span> IUnknown

{

       <span style="color: #008800; font-weight: bold;">virtual</span> HRESULT STDMETHODCALLTYPE GetBuffer(

              BYTE   <span style="color: #333333;">**</span>value,

              UINT32 <span style="color: #333333;">*</span>capacity

              );

};
</pre>
</div>
<p>#2 Get the underlying interface</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;">ComPtr<span style="color: #333333;">&lt;</span>IMemoryBufferByteAccess<span style="color: #333333;">&gt;</span> bufferByteAccess;

HRESULT result <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">reinterpret_cast</span><span style="color: #333333;">&lt;</span>IInspectable<span style="color: #333333;">*&gt;</span>(reference)<span style="color: #333333;">-&gt;</span>QueryInterface(IID_PPV_ARGS(<span style="color: #333333;">&amp;</span>bufferByteAccess));

<span style="color: #008800; font-weight: bold;">if</span> (result <span style="color: #333333;">==</span> S_OK)

{

  WriteLine(<span style="background-color: #fff0f0;">"Get interface successfully"</span>);

  BYTE<span style="color: #333333;">*</span> data <span style="color: #333333;">=</span> nullptr;

  UINT32 capacity <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;

  result <span style="color: #333333;">=</span> bufferByteAccess<span style="color: #333333;">-&gt;</span>GetBuffer(<span style="color: #333333;">&amp;</span>data, <span style="color: #333333;">&amp;</span>capacity);

  <span style="color: #008800; font-weight: bold;">if</span> (result <span style="color: #333333;">==</span> S_OK)

  {

    WriteLine(<span style="background-color: #fff0f0;">"get data access successfully, capacity: "</span> <span style="color: #333333;">+</span> capacity);

  }

}
</pre>
</div>]]></content:encoded>
    </item>
    <item>
      <title>How to Call the Windows Runtime Component in Standard C++</title>
      <link>https://docs.microsoft.com/archive/blogs/practice-sharing/how-to-call-the-windows-runtime-component-in-standard-c</link>
      <pubDate>Tue, 04 Aug 2015 23:37:00 GMT</pubDate>
      <dc:creator><![CDATA[Jeffrey_Chen_]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/practice-sharing/2015/08/04/how-to-call-the-windows-runtime-component-in-standard-c/</guid>
      <description><![CDATA[&nbsp;
#1 Create a new Win32 Console Project (named it Win32DesktopApp)
&nbsp;
#2 Change the...]]></description>
      <content:encoded><![CDATA[<p>&nbsp;</p>
<p>#1 Create a new Win32 Console Project (named it Win32DesktopApp)</p>
<p>&nbsp;</p>
<p>#2 Change the target platform to Window 10</p>
<p>&nbsp;</p>
<p>&nbsp;<a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/6215.blog-pic1.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/6215.blog_2D00_pic1.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/6215.blog-pic1.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/6215.blog_2D00_pic1.png" alt="" border="0" /></a></p>
<p>&nbsp;</p>
<p>#3 Add &ldquo;WindowsApp.lib&rdquo; to &ldquo;Additional Dependencies&rdquo; in Linker -&gt; Input.</p>
<p>&nbsp;</p>
<p>&nbsp;<a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/5342.blog-pic2.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/5342.blog_2D00_pic2.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/71/57/5342.blog-pic2.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-57/5342.blog_2D00_pic2.png" alt="" border="0" /></a></p>
<p>&nbsp;</p>
<p>Code:</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #557799;">#include "stdafx.h"</span>
<span style="color: #557799;">#include &lt;Windows.Foundation.h&gt;</span>
<span style="color: #557799;">#include &lt;wrl\wrappers\corewrappers.h&gt;</span>
<span style="color: #557799;">#include &lt;wrl\client.h&gt;</span>
<span style="color: #557799;">#include &lt;iostream&gt;</span>
<span style="color: #557799;">#include &lt;sstream&gt;</span>

using namespace std;
using namespace ABI<span style="color: #333333;">::</span>Windows<span style="color: #333333;">::</span>Foundation;
using namespace Microsoft<span style="color: #333333;">::</span>WRL;
using namespace Microsoft<span style="color: #333333;">::</span>WRL<span style="color: #333333;">::</span>Wrappers;

<span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">main</span>()
{
	<span style="color: #888888;">// Initialize the Windows Runtime.</span>
	RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);

	<span style="color: #008800; font-weight: bold;">if</span> (FAILED(initialize))
	{
		cout <span style="color: #333333;">&lt;&lt;</span> <span style="background-color: #fff0f0;">"Failed to initialize"</span>;
	}

	<span style="color: #888888;">// Get the activation factory for the IUriRuntimeClass interface.</span>
	ComPtr<span style="color: #333333;">&lt;</span>IUriRuntimeClassFactory<span style="color: #333333;">&gt;</span> uriFactory;

	HRESULT hr <span style="color: #333333;">=</span> GetActivationFactory(HStringReference(RuntimeClass_Windows_Foundation_Uri).Get(), <span style="color: #333333;">&amp;</span>uriFactory);

	<span style="color: #008800; font-weight: bold;">if</span> (FAILED(hr))
	{
		cout <span style="color: #333333;">&lt;&lt;</span> <span style="background-color: #fff0f0;">"failed to activate the Windows Runtime Class Factory"</span>;

		<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
	}

	<span style="color: #888888;">// Create a string that represents a URI.</span>

	HString uriHString;

	hr <span style="color: #333333;">=</span> uriHString.Set(<span style="background-color: #fff0f0;">L"https://www.microsoft.com"</span>);

	<span style="color: #008800; font-weight: bold;">if</span> (FAILED(hr))
	{
		cout <span style="color: #333333;">&lt;&lt;</span> <span style="background-color: #fff0f0;">"failed to wrapper the string"</span>;

		<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
	}

	<span style="color: #888888;">// Create the IUriRuntimeClass object.</span>
	ComPtr<span style="color: #333333;">&lt;</span>IUriRuntimeClass<span style="color: #333333;">&gt;</span> uri;

	hr <span style="color: #333333;">=</span> uriFactory<span style="color: #333333;">-&gt;</span>CreateUri(uriHString.Get(), <span style="color: #333333;">&amp;</span>uri);

	<span style="color: #008800; font-weight: bold;">if</span> (FAILED(hr))
	{
		cout <span style="color: #333333;">&lt;&lt;</span> <span style="background-color: #fff0f0;">"failed to create a runtime instance"</span>;

		<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
	}

	<span style="color: #888888;">// Get the domain part of the URI.</span>
	HString domainName;

	hr <span style="color: #333333;">=</span> uri<span style="color: #333333;">-&gt;</span>get_Domain(domainName.GetAddressOf());

	<span style="color: #008800; font-weight: bold;">if</span> (FAILED(hr))
	{
		cout <span style="color: #333333;">&lt;&lt;</span> <span style="background-color: #fff0f0;">"failed to call method get_Domain"</span>;

		<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
	}

	wprintf_s(<span style="background-color: #fff0f0;">L"Domain name: %s</span><span style="color: #666666; font-weight: bold; background-color: #fff0f0;">\n</span><span style="background-color: #fff0f0;">"</span>, domainName.GetRawBuffer(nullptr));

    <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
}
</pre>
</div>]]></content:encoded>
    </item>
  </channel>
</rss>