<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>The Brain Dump : CLI</title><link>http://blogs.msdn.com/benwilli/archive/tags/CLI/default.aspx</link><description>Tags: CLI</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Meshing in C++ (CLI)</title><link>http://blogs.msdn.com/benwilli/archive/2008/12/08/meshing-in-c-cli.aspx</link><pubDate>Mon, 08 Dec 2008 22:54:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9185336</guid><dc:creator>benwilli</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/benwilli/comments/9185336.aspx</comments><wfw:commentRss>http://blogs.msdn.com/benwilli/commentrss.aspx?PostID=9185336</wfw:commentRss><description>&lt;p&gt;I recently was asked about accessing the Live Framework from C++. Taking this from the standpoint of “Hey, I have a regular old C++ application. How can I start putting my stuff into Live Services using Live fx?” there are a couple of options.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;REST/XML &lt;/li&gt;    &lt;li&gt;C++ CLI Interop &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;These are reasonable alternatives depending on your particular team and your business needs. And yes, there are a few other unreasonable options such as rewriting your whole app in managed code, using .NET/COM interop to talk to the .NET toolkit, or writing your own JSON parser while heavily using the __asm keyword just for fun, but I’ll just forget you mentioned that and move along.&lt;/p&gt;  &lt;h4&gt;REST/XML&lt;/h4&gt;  &lt;p&gt;If you have an application which is already doing XML manipulation, then you may already have the skills and libraries in place to just start using Live fx directly. This would be just the same as if you were using java, python, php or whatever language currently doesn’t have a toolkit available. You just grab an XML library you are comfortable with and starting &lt;a href="http://msdn.microsoft.com/en-us/library/dd199240.aspx"&gt;PUTing and GETing&lt;/a&gt; your way into your user’s Mesh.&lt;/p&gt;  &lt;h4&gt;C++ CLI Interop &lt;/h4&gt;  &lt;p&gt;This is where I think I would end up – mainly because I’m an old C++ hack who lives in a C# world now. With C++ CLI available, you can easily take just a single file or your whole project and build it with the capability to interact natively with .NET assemblies. Since the Live Framework SDK comes with a .NET toolkit, your application can call into the managed Live fx classes directly from C++.&lt;/p&gt;  &lt;p&gt;In C++ you could list all the meshObjects like this&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;LiveOperatingEnvironment^ loe = &lt;span style="color: #0000ff"&gt;gcnew&lt;/span&gt; LiveOperatingEnvironment(); 
loe-&amp;gt;ConnectLocal(); 
Mesh^ mesh = loe-&amp;gt;Mesh; 
IQueryable&amp;lt;MeshObject^&amp;gt;^ meshObjects = mesh-&amp;gt;CreateQuery&amp;lt;MeshObject^&amp;gt;(); 
&lt;span style="color: #0000ff"&gt;for each&lt;/span&gt; (MeshObject ^ mo &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; meshObjects) 
{ 
  Console::WriteLine(mo-&amp;gt;Resource-&amp;gt;Title); 
} &lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;There is fairly straightforward and not too much different than C#, right? Well now comes the fun part – LINQ queries. &lt;/p&gt;

&lt;p&gt;LINQ queries are very useful in the Live fx toolkits, because the toolkit can examine the queries and potentially send it over the wire to run on the server instead of running locally. The problem is that C++ doesn’t natively support LINQ. It can of course call the underlying classes and interfaces that actually run LINQ under the covers but it becomes pretty ugly when trying to do it directly.&lt;/p&gt;

&lt;p&gt;Lets add a “simple” LINQ query to just find all the Mesh Objects that are titled “Documents”.&amp;#160; For those Lambda expression guys out there this is ‘mo =&amp;gt; mo.Resource.Title == “Documents”’&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;LiveOperatingEnvironment^ loe = &lt;span style="color: #0000ff"&gt;gcnew&lt;/span&gt; LiveOperatingEnvironment(); &lt;br /&gt;loe-&amp;gt;ConnectLocal(); &lt;br /&gt;Mesh^ mesh = loe-&amp;gt;Mesh; &lt;br /&gt;IQueryable&amp;lt;MeshObject^&amp;gt;^ baseQuery = mesh-&amp;gt;CreateQuery&amp;lt;MeshObject ^&amp;gt;();

&lt;span style="color: #008000"&gt;// Define the left side of the lambda expression&lt;/span&gt;
ParameterExpression^ meshObj = &lt;br /&gt;    Expression::Parameter(baseQuery-&amp;gt;ElementType, &amp;quot;&lt;span style="color: #8b0000"&gt;meshObj&lt;/span&gt;&amp;quot;);&lt;br /&gt;
&lt;span style="color: #008000"&gt;// Define the right side of the lambda expression&lt;/span&gt;
&lt;span style="color: #008000"&gt;// Need to use the PropertyInfo because of ambiguous &lt;br /&gt;// reflection of derived types&lt;/span&gt;
System::Reflection::PropertyInfo^ propInfo = &lt;br /&gt;    baseQuery-&amp;gt;ElementType-&amp;gt;GetProperty(&lt;br /&gt;        &amp;quot;&lt;span style="color: #8b0000"&gt;Resource&lt;/span&gt;&amp;quot;, &lt;br /&gt;        Microsoft::LiveFX::ResourceModel::MeshObjectResource::typeid&lt;br /&gt;    );
MemberExpression^ resourceProp = &lt;br /&gt;    Expression::Property(meshObj, propInfo);

&lt;span style="color: #008000"&gt;// retrieve Title property of Resource property&lt;/span&gt;
MemberExpression^ titleProp = &lt;br /&gt;    Expression::Property(resourceProp, &amp;quot;&lt;span style="color: #8b0000"&gt;Title&lt;/span&gt;&amp;quot;);

&lt;span style="color: #008000"&gt;// do the compare&lt;/span&gt;
Expression^ leftSide = titleProp;
Expression^ rightSide = Expression::Constant(&amp;quot;&lt;span style="color: #8b0000"&gt;Documents&lt;/span&gt;&amp;quot;);
Expression^ finalExpression = Expression::Equal(leftSide, rightSide);
Expression&amp;lt;Func&amp;lt;MeshObject^,&lt;span style="color: #0000ff"&gt;bool&lt;/span&gt;&amp;gt;^&amp;gt;^ whereExpression = &lt;br /&gt;    Expression::Lambda&amp;lt;Func&amp;lt;MeshObject^,&lt;span style="color: #0000ff"&gt;bool&lt;/span&gt;&amp;gt;^&amp;gt;(finalExpression, meshObj);

IQueryable&amp;lt;MeshObject^&amp;gt;^ whereQuery = &lt;br /&gt;    Queryable::Where&amp;lt;MeshObject^&amp;gt;(baseQuery, whereExpression);&lt;br /&gt;
&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; each (MeshObject ^ mo in whereQuery)
{
    Console::WriteLine(mo-&amp;gt;Resource-&amp;gt;Title);
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;That little query just added a whole lot of complexity because we have to build the expression tree by hand. Again, C++ doesn’t support LINQ syntax – but the queries are extremely useful and you are probably going to end up using them quite a bit. So how do we simplify this? We interop to our own C# assembly instead. In C# this same code collapses to&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;LiveOperatingEnvironment^ loe = gcnew LiveOperatingEnvironment(); 
loe-&amp;gt;ConnectLocal(); 
Mesh^ mesh = loe-&amp;gt;Mesh; &lt;br /&gt;
var meshObjects = from mo &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; mesh.CreateQuery&amp;lt;MeshObject&amp;gt;()
    where mo.Resource.Title == &amp;quot;&lt;span style="color: #8b0000"&gt;Documents&lt;/span&gt;&amp;quot;
    select mo;

&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (MeshObject mo &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; meshObjects)
{
    Console.WriteLine(mo.Resource.Title);
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Using C++ CLI, you can continue to work your main application logic in C++ if you want, and then just call into a separate DLL which talks to the Mesh in C# (or VB) to greatly simplify your development effort. &lt;/p&gt;

&lt;p&gt;The point is, C++ is not left out in the cold; but for any LINQish query filtering/ordering you do, I would do it in a separate .NET assembly to handle it in a language that handles the LINQ syntax naturally. Unless of course you enjoy building your own expression trees. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9185336" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/benwilli/archive/tags/Live+Mesh/default.aspx">Live Mesh</category><category domain="http://blogs.msdn.com/benwilli/archive/tags/Live+Framework/default.aspx">Live Framework</category><category domain="http://blogs.msdn.com/benwilli/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/benwilli/archive/tags/CLI/default.aspx">CLI</category></item><item><title>.NET Extension methods from C++</title><link>http://blogs.msdn.com/benwilli/archive/2008/12/03/net-extension-methods-from-c.aspx</link><pubDate>Wed, 03 Dec 2008 19:08:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9170603</guid><dc:creator>benwilli</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/benwilli/comments/9170603.aspx</comments><wfw:commentRss>http://blogs.msdn.com/benwilli/commentrss.aspx?PostID=9170603</wfw:commentRss><description>&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx"&gt;Extension methods&lt;/a&gt; are a nice little feature now available in C# and VB.NET. They allow you to tack on new methods to existing classes for which you don’t own the original code. Now C++ does not support this natively and so I recently got bit when I was translating some C# code into C++ CLI. &lt;/p&gt;  &lt;p&gt;While C# gave me nice Intellisense&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/benwilli/WindowsLiveWriter/4f97e126b998.NETExtensionmethodsfromC_8A6D/image_2.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="219" alt="image" src="http://blogs.msdn.com/blogfiles/benwilli/WindowsLiveWriter/4f97e126b998.NETExtensionmethodsfromC_8A6D/image_thumb.png" width="553" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;C++ did not&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/benwilli/WindowsLiveWriter/4f97e126b998.NETExtensionmethodsfromC_8A6D/image_4.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="228" alt="image" src="http://blogs.msdn.com/blogfiles/benwilli/WindowsLiveWriter/4f97e126b998.NETExtensionmethodsfromC_8A6D/image_thumb_1.png" width="554" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The method I was calling on the NetworkCredential class did not exist in C++, only in C#. Intellisense had failed me!&lt;/p&gt;  &lt;p&gt;The answer was simple once I realized that this was actually an extension method and how they are implemented. They are static methods on a different class which C# hooks up for you automagically. In C++ we need to call it directly, and pass in the “this” reference as the first parameter.&lt;/p&gt;  &lt;p&gt;So a quick search in reflector or the online help turned up the actual class that implements GetWindowsAuthenticationToken(). It is WindowsLiveIdentityExtension.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/benwilli/WindowsLiveWriter/4f97e126b998.NETExtensionmethodsfromC_8A6D/image_6.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="220" alt="image" src="http://blogs.msdn.com/blogfiles/benwilli/WindowsLiveWriter/4f97e126b998.NETExtensionmethodsfromC_8A6D/image_thumb_2.png" width="545" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;And voila! working code again.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9170603" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/benwilli/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://blogs.msdn.com/benwilli/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/benwilli/archive/tags/CLI/default.aspx">CLI</category></item></channel></rss>