<?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>alexpetr</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/alexpetr/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/alexpetr/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 03:48:03 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Creating a PowerPoint add-in that uses Kinect to move through slides</title>
      <link>https://docs.microsoft.com/archive/blogs/alexpetr/creating-a-powerpoint-add-in-that-uses-kinect-to-move-through-slides</link>
      <pubDate>Sun, 10 Mar 2013 12:03:00 GMT</pubDate>
      <dc:creator><![CDATA[Alex Petrescu]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/alexpetr/2013/03/10/creating-a-powerpoint-add-in-that-uses-kinect-to-move-through-slides/</guid>
      <description><![CDATA[If you are interested in developing Kinect applications, you can start with the latest version of...]]></description>
      <content:encoded><![CDATA[<p>If you are interested in developing Kinect applications, you can start with the latest version of Kinect SDK, available here:</p>
<p><a href="https://www.microsoft.com/en-us/kinectforwindows/develop/developer-downloads.aspx">https://www.microsoft.com/en-us/kinectforwindows/develop/developer-downloads.aspx</a></p>
<p>After installing the SDK, you might want to install the Developer Toolkit also, in order to study the source code for different functionalities. The Toolkit is available here:</p>
<p><a href="https://www.microsoft.com/en-us/download/details.aspx?id=34807">https://www.microsoft.com/en-us/download/details.aspx?id=34807</a></p>
<p>&nbsp;</p>
<p>You have plenty of sample projects for the various things that&nbsp;you can program for Kinect, from gesture recognition to face or voice recognition.</p>
<p>&nbsp;</p>
<p>Once you installed those two things, you can start developing something interesting. For example, in the sample projects you have an application that demonstrates how to use swipe gestures to browse through a photo archive. In this tutorial, we will use the same functionality to create a more useful application: an add-in that allows a presenter to swipe through the slides of his PowerPoint presentation.</p>
<p>In order to do that, we will use a few elements:</p>
<ol>
<li>The Kinect drivers and SDK. Be aware that the Kinect drivers are not available separately, but only inside the SDK, and if you will want to install your final application on another machine, you will need<br />to install the SDK on that machine also</li>
<li>The Kinect sensor</li>
<li>An add-in containing code for gesture recognition and for PowerPoint slide change</li>
<li>Visual Studio 2010</li>
</ol>
<p>&nbsp;</p>
<p>First, create a PowerPoint 2010 Add-in in VS 2010, by choosing the standard project template.</p>
<p>In the Kinect Developer Toolkit, you will find a sample application named &ldquo;Slideshow Gestures &ndash; WPF&rdquo;. Install the project and open it with VS 2010.<br />Look at the references and you will see that it uses a .dll named Microsoft.Samples.Kinect.SwipeGestureRecognizer.dll.<br />Copy this dll to your Add-in project and add it to references, as it will be in charge of the swipe gesture recognition.</p>
<p>Add these two using directives to the main page of your project:</p>
<p>&nbsp;</p>
<pre class="scroll"><code class="csharp">using Microsoft.Samples.Kinect.SwipeGestureRecognizer;<br />using Microsoft.Kinect;<br /> </code></pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>And then, in the AddIn class, declare the Kinect sensor and the Recognizer private members:</p>
<p><span style="font-family: courier new,courier;">&nbsp;&nbsp; </span></p>
<pre class="scroll"><code class="csharp">private KinectSensor nui;<br />private Recognizer activeRecognizer;<br /> </code></pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>In the Startup method of the AddIn application, we initialize these variables:</p>
<p>&nbsp;</p>
<pre class="scroll"><code class="csharp">private void ThisAddIn_Startup(object sender, System.EventArgs e) <br /> {<br /> this.activeRecognizer = his.CreateRecognizer();<br /> this.InitializeNui();<br /> }</code></pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>In the CreateRecognizer method, we hook to the swipe events of the gesture recognizer:</p>
<p>&nbsp;</p>
<p><span style="font-family: courier new,courier;">&nbsp;</span></p>
<pre class="scroll"><code class="csharp"> private Recognizer CreateRecognizer()<br /> {<br /> // Instantiate a recognizer.<br /> var recognizer = new Recognizer();<br /> // Wire-up swipe right to manually advance picture.<br /> recognizer.SwipeRightDetected += (s, e) =&gt;<br /> {<br /> try<br /> {<br /> this.Application.ActivePresentation.SlideShowWindow.View.Next();<br /> }<br /> catch (Exception ex)<br /> {<br /> //&hellip;<br /> }<br /> };</code></pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<pre class="scroll"><code class="csharp"> recognizer.SwipeLeftDetected += (s, e) =&gt;<br /> {<br /> try<br /> {<br /> this.Application.ActivePresentation.SlideShowWindow.View.Previous();<br /> }<br /> catch (Exception ex)<br /> {<br /> //&hellip;<br /> }<br /> };<br /> return recognizer;<br /> }</code></pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>In the InitializeNui() method, we are checking for all the available Kinect sensors:</p>
<p>&nbsp;<span style="font-family: courier new,courier;">&nbsp;</span></p>
<pre class="scroll"><code class="csharp"> private void InitializeNui()<br /> {<br /> this.UninitializeNui();<br /> var index = 0;<br /> while (this.nui == null &amp;&amp; index &lt; KinectSensor.KinectSensors.Count)<br /> {<br /> try<br /> {<br /> this.nui = KinectSensor.KinectSensors[index]; <br /> this.nui.Start(); <br /> this.IsDisconnected = false;</code></pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>You will find all this code in the &ldquo;Slideshow Gestures&rdquo; application from the Kinect Developer Toolkit. In our PowerPoint Add-in application, we are only using a part of that code, to achieve the intended functionality.</p>
<p>As you can see, the main actions are sliding through the PowerPoint presentation when the Recognizer is capturing a swipe left/right event.</p>
<p>Next, you only need to create an installation package to distribute this add-in. In the Solution Explorer, right-click on the project and select Publish. Specify a location on the disk and click Next. Choose &ldquo;From a CD-ROM&hellip;&rdquo; and click Next, then Finish.</p>
<p>Install the add-in on a presentation machine and check these steps to make sure it is properly installed: In PowerPoint, to File-&gt;Options-&gt;Add-Ins. In the &ldquo;Active Application Add-ins&rdquo;, you should see the add-in. If the add-in is not present, verify the &ldquo;Manage: &ldquo; list at the bottom of the page: select COM add-ins in the list, click Go, check the &lt;name of your addin&gt; option in the list, click OK.</p>
<p>&nbsp;</p>
<p>To connect the Kinect sensor to your PC, you need a Kinect adapter, as the connector of Kinect doesn&rsquo;t match the USB of your PC, and they also need an external power source. Once the Kinect sensor is connecter and the PowerPoint presentation is started, the red light on the sensor should be on, which indicates that the sensor is recognized by the application.</p>
<p>Start the PowerPoint slideshow.</p>
<p>Stand in front of the Kinect sensor at a distance between 1.5 meters and 4 meters</p>
<p>To move to the next slide, do a swiping movement with your right hand, from right to left. The hand should be at chest-level and the movement should be continuous</p>
<p>To move to the previous slide, do a swiping movement with your left hand, from left to right</p>
<p>&nbsp;</p>
<p>That would be all. You can use this sample in your team presentations, to leverage the Kinect technology and demonstrate that it can have a multitude of uses, outside the gaming world.</p>
<p>You can download the sample from here: <a href="https://sdrv.ms/13PMDX0">https://sdrv.ms/13PMDX0</a></p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Creating an editable control on Custom Designers</title>
      <link>https://docs.microsoft.com/archive/blogs/alexpetr/creating-an-editable-control-on-custom-designers</link>
      <pubDate>Fri, 16 Nov 2012 03:23:00 GMT</pubDate>
      <dc:creator><![CDATA[Alex Petrescu]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/alexpetr/2012/11/16/creating-an-editable-control-on-custom-designers/</guid>
      <description><![CDATA[A very interesting problem was proposed to me by Microsoft Partner, Dmitry Grigansky. The goal was...]]></description>
      <content:encoded><![CDATA[<p>A very interesting problem was proposed to me by Microsoft Partner, <a href="https://social.msdn.microsoft.com/profile/dmitriy%20griganskiy/?ws=usercard-mini">Dmitry Grigansky</a>. The goal was to create a host designer and have an editable label control placed on it, having this article as a starting base:</p>
<p><a href="https://msdn.microsoft.com/en-us/magazine/cc163634.aspx">https://msdn.microsoft.com/en-us/magazine/cc163634.aspx</a></p>
<p>With help from Dmitry, who relentlessly investigated the matter, here are the main steps for creating this project.</p>
<p>First of all, we create a VS 2010 Win forms project and make sure we set the project for .NET 4 , not .NET 4 Client profile.</p>
<p>Add a reference to System.Design and then create a form named ParentForm. This will be the form on which our Designer will be place.</p>
<p>Next, we create a form named DesignForm, which will be the actual area for the controls to be placed.</p>
<p>On the ParentForm we add this code:</p>
<pre class="scroll"><code class="csharp">public ParentForm()<br /> { <br /> InitializeComponent(); <br /> System.ComponentModel.Design.DesignSurface<br /> ds = new System.ComponentModel.Design.DesignSurface(); <br /> ds.BeginLoad(typeof(DesignForm));<br /> Control c = ds.View as Control; <br /> c.Parent = this; <br /> c.Dock = DockStyle.Fill;<br /> }</code></pre>
<p>&nbsp;</p>
<p>Now, the DesignForm is a child of ParentForm.</p>
<p>We will now proceed to the creation of class EditableLabelDesigner derived from ControlDesigner</p>
<p>&nbsp;</p>
<pre class="scroll"><code class="csharp">class EditableLabelDesigner : ControlDesigner <br /> { <br /> private const int WM_LBUTTONDBLCLK = 0x0203;<br /> <br /> public override void Initialize(IComponent<br /> component) <br /> { <br /> base.Initialize(component); <br /> }<br /> </code>&nbsp;</pre>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<pre class="scroll"><code class="csharp"> protected override void WndProc(ref<br /> System.Windows.Forms.Message m)<br /> {<br /> if (m.Msg == WM_LBUTTONDBLCLK)<br /> { <br /> ((EditableLabel)Control).ShowInplaceEditor();<br /> }<br /> base.WndProc(ref m);<br /> }<br /> }</code></pre>
<p>&nbsp;</p>
<p>The WndProc method will catch the WM_LBUTTONDBLCLK event and show the editable area on the label.</p>
<p>&nbsp;</p>
<p>Next, create the custom control EditableLabel:</p>
<p>Add new user control EditableLabel</p>
<p>&nbsp;</p>
<pre class="scroll"><code class="csharp">[Designer(typeof(EditableLabelDesigner))] <br /> <br /> public partial class EditableLabel : Label<br /> {<br /> public EditableLabel()<br /> { <br /> InitializeComponent();<br /> } <br /> <br /> private TextBox inplaceEditor;<br /> private bool isRedrawn = false;<br /> public TextBox InplaceEditor<br /> {<br /> get { return inplaceEditor; }<br /> }<br /> <br /> public bool IsRedrawn<br /> {<br /> get { return isRedrawn; }<br /> set<br /> {<br /> if (value == true) <br /> {<br /> if (inplaceEditor != null) <br /> {<br /> Text = inplaceEditor.Text;<br /> inplaceEditor.Visible = false;<br /> }<br /> } <br /> isRedrawn = value;<br /> }<br /> }<br /> <br /> <br /> <br /> protected override void OnPaint(PaintEventArgs e)<br /> {<br /> if (!isRedrawn)<br /> { <br /> IsRedrawn = true;<br /> }<br /> base.OnPaint(e);<br /> }<br /> <br /> public void ShowInplaceEditor()<br /> { <br /> isRedrawn = false;<br /> if (inplaceEditor == null)<br /> { <br /> this.inplaceEditor = new TextBox(); <br /> inplaceEditor.Parent = this.Parent.Parent; <br /> inplaceEditor.Multiline = true;<br /> }<br /> <br /> <br /> inplaceEditor.HideSelection = false; <br /> inplaceEditor.AcceptsTab = true; <br /> inplaceEditor.Text = this.Text; <br /> inplaceEditor.Visible = true; <br /> inplaceEditor.Location = new Point(this.Parent.Location.X<br /> + this.Location.X + 10, this.Parent.Location.Y + this.Location.Y<br /> + 32); <br /> inplaceEditor.Size = new Size(this.ClientRectangle.Width<br /> - 4, this.ClientRectangle.Height - 4); <br /> inplaceEditor.Font = this.Font; <br /> inplaceEditor.SelectAll(); <br /> inplaceEditor.BringToFront(); <br /> inplaceEditor.Focus();<br /> }<br /> }<br /> </code></pre>
<p class="scroll"><code class="csharp"></code>In the ShowInplaceEditor, the TextBox is added to the DesignForm&rsquo;s parent, so it will be editable.&nbsp;</p>
<p>Finally, in the ParentForm constructor, we add the EditableLabel on the DesignFom:</p>
<pre class="scroll"><code class="csharp"> IDesignerHost idh = (IDesignerHost)ds.GetService(typeof(IDesignerHost));<br /> IToolboxUser itu = (IToolboxUser)idh.GetDesigner(idh.RootComponent); <br /> itu.ToolPicked(new ToolboxItem(typeof(EditableLabel)));</code></pre>
<p>&nbsp;</p>
<p>Make sure to put the ParentForm as the staring form of your project, and you&rsquo;re done.</p>
<p>You can find the sample project <a href="https://sdrv.ms/Sw8GYG">here</a>.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>CString and strcpy in VS 2008 and VC++ 6.0</title>
      <link>https://docs.microsoft.com/archive/blogs/alexpetr/cstring-and-strcpy-in-vs-2008-and-vc-6-0</link>
      <pubDate>Thu, 23 Aug 2012 08:06:00 GMT</pubDate>
      <dc:creator><![CDATA[Alex Petrescu]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/alexpetr/2012/08/23/cstring-and-strcpy-in-vs-2008-and-vc-6-0/</guid>
      <description><![CDATA[I was faced with an interesting problem recently, which originated from a mix between the CString...]]></description>
      <content:encoded><![CDATA[<p>I was faced with an interesting problem recently, which originated from a mix between the CString class and the C-style char* strings.</p>
<p>An ocx (yes, apparently they are not extinct) component written in C++ was receiving a string from a .NET application. After executing a bunch of procedures on this string, it was supposed to return it to the calling .NET application, to be displayed. The ocx&rsquo;s solution was VS 2008.</p>
<p>The string was at some point going to the component as &lsquo;99&rsquo; and it was receiving an initial &lsquo;0&rsquo;, so the expected result was &lsquo;099&rsquo;. Instead, we were receiving only &lsquo;09&rsquo;.</p>
<p>In the component&rsquo;s code we had a CString object holding the string value and passing it to various methods and functions. Some of these functions were old C functions, and they were working on the string held in the CString object.</p>
<p>What we had could be reduced to this:</p>
<p><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CString test;</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test = "99";</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char* pchar = (char*)test.GetBuffer(0);</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strcpy(pchar, "099");</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test.ReleaseBuffer();</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _tprintf(_T(&ldquo;%s&rdquo;), test);</span></p>
<p>&nbsp;</p>
<p>The strcpy part was coming from one of the C-style functions, and was supposed to add the extra &lsquo;0&rsquo; to the beginning of the string.</p>
<p>GetBuffer() is returning a pointer to the object&rsquo;s null terminated character buffer. So we had a pointer to a string with 2 characters, and then we copied 3 characters at this address: strcpy(pchar, "099");</p>
<p>&nbsp;</p>
<p>We were lucky enough not to corrupt any other program variable, but we ran into trouble a little bit later, when we called test.ReleaseBuffer():</p>
<p><span style="font-family: courier new,courier;">void ReleaseBuffer( _In_ int nNewLength = -1 )</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( nNewLength == -1 )</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int nAlloc = GetData()-&gt;nAllocLength;</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nNewLength = StringLengthN( m_pszData, nAlloc);</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetLength( nNewLength );</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p>&nbsp;</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/54/42/3162.p1.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-54-42/3162.p1.png"><img border="0" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/54/42/3162.p1.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-54-42/3162.p1.png" /></a></p>
<p>As you can see, we already have a nAllocLength member variable of CString holding the string&rsquo;s length. And that is 2, even after we copied the 3-characters string over the buffer. So, we will be calling SetLength(nNewLength) with a value of 2:</p>
<p><span style="font-family: courier new,courier;">void SetLength( _In_ int nLength )</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( nLength &lt; 0 || nLength &gt; GetData()-&gt;nAllocLength)</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AtlThrow(E_INVALIDARG);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p>
<p><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GetData()-&gt;nDataLength = nLength;</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_pszData[nLength] = 0;</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p>So, in the final line of SetLength, the last character of &lsquo;099&rsquo; is set to 0, and the string becomes &lsquo;09&rsquo;.</p>
<p>As the developer of the control&nbsp;confirmed that this problem was not present&nbsp;in the previous versions, I checked this behavior on VC++ 6.0 also.</p>
<p>Here, the definition of ReleaseBuffer is different:</p>
<p><span style="font-family: courier new,courier;">void CString::ReleaseBuffer(int nNewLength)</span><br /><span style="font-family: courier new,courier;">{</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;CopyBeforeWrite();&nbsp; // just in case GetBuffer was not called</span></p>
<p><span style="font-family: courier new,courier;">&nbsp;&nbsp; if (nNewLength == -1)</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nNewLength = lstrlen(m_pchData); // zero terminated</span></p>
<p><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;ASSERT(nNewLength &lt;= GetData()-&gt;nAllocLength);</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;GetData()-&gt;nDataLength = nNewLength;</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;m_pchData[nNewLength] = '\0';</span><br /><span style="font-family: courier new,courier;">}</span></p>
<p>We don&rsquo;t have a member variable in CString to hold the string&rsquo;s length anymore, but we take the length directly from the string&rsquo;s buffer, with lstrlen(m_pchData). This will allow us to write any string, with no regards to the initial size held in the CString class, and we will still get the length. Then the string will be nicely ended with &lsquo;\0&rsquo;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_pchData[nNewLength] = '\0';<br /><br /></p>
<p>This will make the code &lsquo;work&rsquo; in VC++ 6.0 and it will cause the unexpected behavior once we move to newer versions.</p>
<p>To fix this, we need to pass the length of the incoming string to GetBuffer. Instead of</p>
<p><span style="font-family: courier new,courier;">char* pchar = (char*)test.GetBuffer(0);</span></p>
<p>we will have</p>
<p><span style="font-family: courier new,courier;">char* pchar = (char*)test.GetBuffer(3);</span></p>
<p>This way, there will be no going over the boundaries of the existing string, and the length will also be properly defined, so ReleaseBuffer and SetLength will not mess with the string anymore. <br /><br /></p>
<p>&nbsp;</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Windows 8 – style applications and WCF services</title>
      <link>https://docs.microsoft.com/archive/blogs/alexpetr/windows-8-style-applications-and-wcf-services</link>
      <pubDate>Tue, 14 Aug 2012 07:43:00 GMT</pubDate>
      <dc:creator><![CDATA[Alex Petrescu]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/alexpetr/2012/08/14/windows-8-style-applications-and-wcf-services/</guid>
      <description><![CDATA[If you get yourself a developer account, you will be able to create Windows 8 &ndash; style...]]></description>
      <content:encoded><![CDATA[<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">If you get yourself a developer account, you will be able to create Windows 8 &ndash; style applications with Visual Studio 2012. One of the first things I tried was to create a client for a WCF service. <br /></span></span><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">I created a simple application, with a command button and a textbox, and then a new project of type WCF Application. Using just the default contract of the WCF service template, I added a reference to the service, instantiated the proxy in the client application and then tried to call the GetData method. <br /></span></span><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">First surprise, the proxy only has async methods: </span></span></p>
<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/54/42/2577.p1.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-54-42/2577.p1.png"><img border="0" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/54/42/2577.p1.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-54-42/2577.p1.png" /></a></span></span></p>
<p></p>
<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">As per the method&rsquo;s documentation, it can be used as: </span></span></p>
<p></p>
<pre class="scroll"><code class="csharp">str x = await sc.GetDataAsync(int value) <br /> <br /></code></pre>
<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri"></span></span><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">And the method itself, if we look at the declaration, returns</span></span></p>
<pre class="scroll"><code class="cplusplus">System.Threading.Tasks.Task&lt;string&gt; <br /></code><span style="font-family: Calibri; font-size: small;" size="3" face="Calibri">&nbsp;</span></pre>
<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">In order to use the new await keyword, introduced in .NET 4.5, we need to be in an async method. You can find more information about asynchronous programming in VS 2012 on MSDN: <br /></span></span><a href="https://msdn.microsoft.com/en-us/library/hh191443(v=VS.110).aspx"><span style="font-family: Calibri; font-size: small;" size="3" face="Calibri">https://msdn.microsoft.com/en-us/library/hh191443(v=VS.110).aspx</span></a></p>
<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">So, assuming I am doing this proxy initialization and method call in Button_Click, the code needs to look like this:&nbsp;</span></span>&nbsp;</p>
<pre class="scroll"><code class="csharp">private async void Button_Click_1(object sender, RoutedEventArgs e) <br /> { <br /> ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client(); <br /> string str = await sc.GetDataAsync(10); <br /></code></pre>
<p><span style="font-family: Calibri; font-size: small;" size="3" face="Calibri">This will prevent the application from freezing until the method returns from the service. You can test this by adding a </span>System.Threading.Thread.Sleep(5000); <span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">in the GetData method of the service, and you will see that the other controls of your application remain responsive. </span></span></p>
<p><span style="font-family: Calibri; font-size: small;" size="3" face="Calibri">Further on, let&rsquo;s say that you don&rsquo;t want to have an async method, and you insist on your application remaining frozen until the work on the service side is done. Maybe you secretly believe that this approach will give you special super-powers, or maybe you have some other valid arguments for doing so. Anyway, if you want to do that, you only have to look at the method&rsquo;s signature and see the return type: </span></p>
<pre class="scroll"><code class="csharp">System.Threading.Tasks.Task&lt;string&gt;</code></pre>
<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">You can then go on like this, retrieving a Task&lt;string&gt; object from the proxy method and then reading the Result property, which will contain the actual string returned by the service&rsquo;s GetData. </span></span></p>
<pre class="scroll"><code class="csharp">Task&lt;string&gt; s = sc.GetDataAsync(10);<br /></code><code class="csharp">this.TextBox1.Text = s.Result;</code><span style="font-family: Calibri; font-size: small;" size="3" face="Calibri">&nbsp;</span></pre>
<p><span style="font-family: Calibri; font-size: small;" size="3" face="Calibri">Sure enough, you will be able to enjoy your popcorn while staring at the locked screen for the next 5 seconds, while the WCF service does it&rsquo;s Sleep(5000) routine. </span></p>]]></content:encoded>
    </item>
    <item>
      <title>Visual Studio 2012 and BuildEvents in addins</title>
      <link>https://docs.microsoft.com/archive/blogs/alexpetr/visual-studio-2012-and-buildevents-in-addins</link>
      <pubDate>Tue, 14 Aug 2012 04:02:59 GMT</pubDate>
      <dc:creator><![CDATA[Alex Petrescu]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/alexpetr/2012/08/14/visual-studio-2012-and-buildevents-in-addins/</guid>
      <description><![CDATA[While testing the functionalities of the new Visual Studio 2012, I came across a scenario that...]]></description>
      <content:encoded><![CDATA[<p>While testing the functionalities of the new Visual Studio 2012, I came across a scenario that seemed a little ambiguous and, at first glance, pointed to a bug.</p>
<p>Let&rsquo;s say that you create an add-in in VS 2010 and you need to have it perform different actions on the build events.<br />You will add the</p>
<p><span style="font-family: courier new,courier;">_applicationObject.Events.BuildEvents.OnBuildBegin += new _dispBuildEvents_OnBuildBeginEventHandler(BuildEvents_OnBuildBegin);</span></p>
<p>instruction in the OnConnection() method and then add the BuildEvents_OnBuildBegin method:</p>
<p><span style="font-family: courier new,courier;">&nbsp; void BuildEvents_OnBuildBegin(vsBuildScope Scope, vsBuildAction Action)</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;Console.WriteLine("onbuildbegin");</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p>Build the add-in, go to Debug-&gt;Start Debugging, which will open a new instance of VS 2010. Create a new application which will use the add-in, load the add-in with Add-in Manager and put a breakpoint in the BuildEvents_OnBuildBegin method. Build the new application and you will see the execution stopping at the breakpoint, as expected.</p>
<p>If you do the same scenario in VS 2012, you can see that the OnBuildBegin event seems not to be raised anymore and the BuildEvents_OnBuildBegin method is no longer being called.<br />The apparent &lsquo;workaround&rsquo; for this is to add a reference to _applicationObject.Events.BuildEvents in the OnConnection method of the add-in Connect class:&nbsp;</p>
<p><span style="font-family: courier new,courier;">private _BuildEvents _buildEvents;</span><br /><span style="font-family: courier new,courier;">public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _buildEvents = _applicationObject.Events.BuildEvents;</span></p>
<p>Curious to see if this is a bug in VS 2012, I looked for previous scenarios and bugs referring to this issue. I could find a reference to a similar situation in VS 2003, which was signaled as a bug and seemingly fixed. I also found this KB, which explains pretty much the same thing and offers the same solution:<a href="https://support.microsoft.com/kb/555102">https://support.microsoft.com/kb/555102</a></p>
<p>It all pointed towards a bug which existed in earlier versions, had been fixed and the fix wasn&rsquo;t ported to VS 2012. It turned out that this wasn&rsquo;t the case.</p>
<p>When the developers of VS 2012 analyzed the call stack, it could be seen that the RCW for the BuildEvents object was finalized by the GC and the event is unadvised. Adding a reference to BuildEvents in the class saves the object from being GC-ed and is actually the correct way to do it. The reasons why this used to work on previous versions are not quite clear, but it has probably something to do with a very short cycle for subscribe and receive event, in which the GC didn&rsquo;t intervene and the event could be caught before finalizing the object.</p>
<p>Also, this wasn&rsquo;t always true, as we can see if we dig deeper and find these articles and discussions which mention the problem for VS 2005, VS 2008 and VS 2010:<br /><a href="https://www.mztools.com/articles/2005/MZ2005012.aspx">https://www.mztools.com/articles/2005/MZ2005012.aspx</a><br /><a href="https://social.msdn.microsoft.com/Forums/en/vsx/thread/d48448d1-b00e-4520-bdf7-685640b0f072">https://social.msdn.microsoft.com/Forums/en/vsx/thread/d48448d1-b00e-4520-bdf7-685640b0f072</a><br /><a href="https://social.msdn.microsoft.com/Forums/en/vsx/thread/ee827d4f-db11-497b-9135-bb344a144d52">https://social.msdn.microsoft.com/Forums/en/vsx/thread/ee827d4f-db11-497b-9135-bb344a144d52</a></p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
  </channel>
</rss>