<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Damir's Corner - Development|C++</title>
    <link>http://www.damirscorner.com/</link>
    <description>Notes from Daily Encounters with Technology</description>
    <language>en-us</language>
    <copyright>Damir Arh, M. Sc.</copyright>
    <lastBuildDate>Sat, 11 Feb 2012 19:25:45 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>damir.arh@gmail.com</managingEditor>
    <webMaster>damir.arh@gmail.com</webMaster>
    <item>
      <trackback:ping>http://www.damirscorner.com/Trackback.aspx?guid=9121bd6b-876c-4051-8487-de1b95a7e919</trackback:ping>
      <pingback:server>http://www.damirscorner.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.damirscorner.com/PermaLink,guid,9121bd6b-876c-4051-8487-de1b95a7e919.aspx</pingback:target>
      <dc:creator>Damir Arh</dc:creator>
      <wfw:comment>http://www.damirscorner.com/CommentView,guid,9121bd6b-876c-4051-8487-de1b95a7e919.aspx</wfw:comment>
      <wfw:commentRss>http://www.damirscorner.com/SyndicationService.asmx/GetEntryCommentsRss?guid=9121bd6b-876c-4051-8487-de1b95a7e919</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Last week I was configuring the build server to compile its first Visual C++ 2010
project. I took the approach of using <a href="http://msdn.microsoft.com/en-us/library/dd393574.aspx">MsBuild</a> on
the project file (.vcxproj) as I am already doing it with the .NET projects. This
worked just fine on my development machine with Visual Studio 2010 installed. It soon
turned out not to be as easy as I expected it to be. The build failed as follows:
</p>
        <pre class="brush: plain;">C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets(297,5): 
warning MSB8003: Could not find WindowsSDKDir variable from the registry. 
TargetFrameworkVersion or PlatformToolset may be set to an invalid version number.

C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\Microsoft.Cpp.Win32.Targets(57,5): 
error MSB6006: "CL.exe" exited with code -1073741515.

</pre>
        <p>
My first thought was that there were some files missing since there was no Visual
Studio installed on the build machine. Running the build command in a regular command
prompt resulted in the same error after an additional message box popped up:
</p>
        <p>
          <img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="The program can't start because mspdb100.dll is missing from your computer. Try reinstalling the program to fix this problem." border="0" alt="The program can't start because mspdb100.dll is missing from your computer. Try reinstalling the program to fix this problem." src="http://www.damirscorner.com/content/binary/Windows-Live-Writer/dd9f23781885_ED4F/image_3.png" width="480" height="175" />
        </p>
        <p>
This was to be expected. Even on my development machine it is necessary to start the
build from the Visual Studio Command Prompt for it to work. Its replacement on the
build machine is Windows SDK 7.1 Command Prompt, part of <a href="http://www.microsoft.com/download/en/details.aspx?id=8279">Microsoft
Windows SDK for Windows 7 and .NET Framework 4</a>. From this command prompt the build
worked flawlessly on the build machine as well. It seems we’re getting closer to the
source of the problem… The command prompt is initialized by first calling SetEnv.cmd
from the Windows SDK installation (by default in C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin)
to set the required environment variables. By doing the same in the build script before
calling MsBuild should do the trick for the continuous integration server as well.
</p>
        <p>
Well, here lied the real problem. To simplify <a href="http://cruisecontrolnet.org/projects/ccnet">CruiseControl.net</a> project
configuration the build scripts are written in <a href="http://nant.sourceforge.net/">NAnt</a>.
Calling a batch script from NAnt is not a problem of course, there is the <a href="http://nant.sourceforge.net/release/latest/help/tasks/exec.html">exec</a> task
for doing that, but any changes to the environment variables are preserved only for
the duration of this task. This means we haven’t achieved anything by calling SetEnv.cmd
before the <a href="http://nantcontrib.sourceforge.net/release/latest/help/tasks/msbuild.html">msbuild</a> task
since any changes made by it are lost. The only way to make this work is by creating
another batch script which calls both SetEnv.cmd and MSBuild immediately after it:
</p>
        <pre class="brush: plain;">call "C:\Program Files\Microsoft SDKs\Windows\v7.1\bin\Setenv.cmd" /Release /x86
msbuild myproj.vcxproj /verbosity:minimal

</pre>
        <p>
It’s not ideal but it’s the only way I could make it work. Fortunately this batch
script shouldn’t be changing much since everything else is configured inside the project
file. I’d still prefer a solution which would allow calling MsBuild directly from
NAnt though. 
</p>
        <img width="0" height="0" src="http://www.damirscorner.com/aggbug.ashx?id=9121bd6b-876c-4051-8487-de1b95a7e919" />
      </body>
      <title>Compiling Visual C++ 2010 Projects on a Build Server</title>
      <guid isPermaLink="false">http://www.damirscorner.com/PermaLink,guid,9121bd6b-876c-4051-8487-de1b95a7e919.aspx</guid>
      <link>http://www.damirscorner.com/CompilingVisualC2010ProjectsOnABuildServer.aspx</link>
      <pubDate>Sat, 11 Feb 2012 19:25:45 GMT</pubDate>
      <description>&lt;p&gt;
Last week I was configuring the build server to compile its first Visual C++ 2010
project. I took the approach of using &lt;a href="http://msdn.microsoft.com/en-us/library/dd393574.aspx"&gt;MsBuild&lt;/a&gt; on
the project file (.vcxproj) as I am already doing it with the .NET projects. This
worked just fine on my development machine with Visual Studio 2010 installed. It soon
turned out not to be as easy as I expected it to be. The build failed as follows:
&lt;/p&gt;
&lt;pre class="brush: plain;"&gt;C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets(297,5): 
warning MSB8003: Could not find WindowsSDKDir variable from the registry. 
TargetFrameworkVersion or PlatformToolset may be set to an invalid version number.

C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\Microsoft.Cpp.Win32.Targets(57,5): 
error MSB6006: "CL.exe" exited with code -1073741515.

&lt;/pre&gt;
&lt;p&gt;
My first thought was that there were some files missing since there was no Visual
Studio installed on the build machine. Running the build command in a regular command
prompt resulted in the same error after an additional message box popped up:
&lt;/p&gt;
&lt;p&gt;
&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="The program can't start because mspdb100.dll is missing from your computer. Try reinstalling the program to fix this problem." border="0" alt="The program can't start because mspdb100.dll is missing from your computer. Try reinstalling the program to fix this problem." src="http://www.damirscorner.com/content/binary/Windows-Live-Writer/dd9f23781885_ED4F/image_3.png" width="480" height="175"&gt;
&lt;/p&gt;
&lt;p&gt;
This was to be expected. Even on my development machine it is necessary to start the
build from the Visual Studio Command Prompt for it to work. Its replacement on the
build machine is Windows SDK 7.1 Command Prompt, part of &lt;a href="http://www.microsoft.com/download/en/details.aspx?id=8279"&gt;Microsoft
Windows SDK for Windows 7 and .NET Framework 4&lt;/a&gt;. From this command prompt the build
worked flawlessly on the build machine as well. It seems we’re getting closer to the
source of the problem… The command prompt is initialized by first calling SetEnv.cmd
from the Windows SDK installation (by default in C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin)
to set the required environment variables. By doing the same in the build script before
calling MsBuild should do the trick for the continuous integration server as well.
&lt;/p&gt;
&lt;p&gt;
Well, here lied the real problem. To simplify &lt;a href="http://cruisecontrolnet.org/projects/ccnet"&gt;CruiseControl.net&lt;/a&gt; project
configuration the build scripts are written in &lt;a href="http://nant.sourceforge.net/"&gt;NAnt&lt;/a&gt;.
Calling a batch script from NAnt is not a problem of course, there is the &lt;a href="http://nant.sourceforge.net/release/latest/help/tasks/exec.html"&gt;exec&lt;/a&gt; task
for doing that, but any changes to the environment variables are preserved only for
the duration of this task. This means we haven’t achieved anything by calling SetEnv.cmd
before the &lt;a href="http://nantcontrib.sourceforge.net/release/latest/help/tasks/msbuild.html"&gt;msbuild&lt;/a&gt; task
since any changes made by it are lost. The only way to make this work is by creating
another batch script which calls both SetEnv.cmd and MSBuild immediately after it:
&lt;/p&gt;
&lt;pre class="brush: plain;"&gt;call "C:\Program Files\Microsoft SDKs\Windows\v7.1\bin\Setenv.cmd" /Release /x86
msbuild myproj.vcxproj /verbosity:minimal

&lt;/pre&gt;
&lt;p&gt;
It’s not ideal but it’s the only way I could make it work. Fortunately this batch
script shouldn’t be changing much since everything else is configured inside the project
file. I’d still prefer a solution which would allow calling MsBuild directly from
NAnt though. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.damirscorner.com/aggbug.ashx?id=9121bd6b-876c-4051-8487-de1b95a7e919" /&gt;</description>
      <comments>http://www.damirscorner.com/CommentView,guid,9121bd6b-876c-4051-8487-de1b95a7e919.aspx</comments>
      <category>Development</category>
      <category>Development/C++</category>
      <category>Development/NAnt</category>
      <category>Software</category>
      <category>Software/CruiseControl</category>
    </item>
    <item>
      <trackback:ping>http://www.damirscorner.com/Trackback.aspx?guid=330dfd2e-d438-41fb-ae74-1bafc8ca9076</trackback:ping>
      <pingback:server>http://www.damirscorner.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.damirscorner.com/PermaLink,guid,330dfd2e-d438-41fb-ae74-1bafc8ca9076.aspx</pingback:target>
      <dc:creator>Damir Arh</dc:creator>
      <wfw:comment>http://www.damirscorner.com/CommentView,guid,330dfd2e-d438-41fb-ae74-1bafc8ca9076.aspx</wfw:comment>
      <wfw:commentRss>http://www.damirscorner.com/SyndicationService.asmx/GetEntryCommentsRss?guid=330dfd2e-d438-41fb-ae74-1bafc8ca9076</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
By switching from <a href="http://www.damirscorner.com/blog/UsefulPInvokeResources.aspx">C#
with P/Invoke calls</a> to <a href="http://www.damirscorner.com/blog/ImplementingAManagedInterfaceInC.aspx">Managed
C++</a> when implementing a managed wrapper for the ANSI C style library I stumbled
upon, I wanted to avoid the tedious and error-prone task of writing the P/Invoke signatures
for function calls and user-defined types for the structures they used. As a side
result I also managed to avoid most of the advanced marshalling issues with complex
data structures.
</p>
        <p>
With simple value types not needing any explicit marshalling only strings need special
attention since <font face="Courier New">char*</font> and <font face="Courier New">System::String</font> can’t
be implicitly converted between. The <font face="Courier New">Marshal</font> class
implements the methods necessary for doing this as demonstrated in the following snippet:
</p>
        <p>
          <font face="Courier New">String^ MCPP::Together(String^ first, String^ second)<br />
{<br />
   // marshal managed strings to unamanged memory 
<br />
   IntPtr firstPtr = Marshal::StringToHGlobalAnsi(first);<br />
   IntPtr secondPtr = Marshal::StringToHGlobalAnsi(second);</font>
        </p>
        <p>
          <font face="Courier New">   // cast unmanaged buffer to a characer array<br />
   char* firstNative = static_cast&lt;char*&gt;(firstPtr.ToPointer());<br />
   char* secondNative = static_cast&lt;char*&gt;(secondPtr.ToPointer());</font>
        </p>
        <p>
          <font face="Courier New">   // perform some unmanaged calls<br />
   int bufferSize = strlen(firstNative) + strlen(secondNative) + 4;<br />
   char* resultBuffer = new char[bufferSize];<br />
   sprintf_s(resultBuffer, bufferSize, "%s + %s", firstNative, secondNative);<br />
   
<br />
   // marshal unmanaged character array to managed string<br />
   String^ result = Marshal::PtrToStringAnsi(static_cast&lt;IntPtr&gt;(resultBuffer));<br />
   
<br />
   // free all unmanaged buffers<br />
   delete[] resultBuffer;<br />
   Marshal::FreeHGlobal(firstPtr);<br />
   Marshal::FreeHGlobal(secondPtr);<br />
   
<br />
   // return managed string<br />
   return result;<br />
}</font>
        </p>
        <p>
It should be noted that it wouldn’t make any sense switching to unmanaged code to
do some string operations only as shown above. This is for demonstration purposes
only and you would usually call some unmanaged libraries or the like instead of it.
But it is a nice demonstration how cumbersome string operations were in C. If you
were doing this once, the <font face="Courier New">sprintf_s</font> function call
might have caught your attention. It’s a <a href="http://msdn2.microsoft.com/en-us/library/ms235384.aspx">safe
implementation</a> of <font face="Courier New">sprintf</font> which prevents buffer
overflows.
</p>
        <p>
Another point worth mentioning is that you have to take care of allocated memory for
the conversion since your character array is now allocated on the unmanaged heap.
Make sure you use the <font face="Courier New">FreeHGlobal</font> method to do it,
not the <font face="Courier New">free</font> (used for freeing memory allocated by <font face="Courier New">*alloc</font> calls
– ANSI C style) or <font face="Courier New">delete</font> (used for freeing memory
allocated by <font face="Courier New">new</font> calls – C++ style) functions.
</p>
        <p>
To reduce the code overhead of string conversions between managed and unmanaged world
you might consider wrapping it into a <a href="http://blogs.msdn.com/jeremykuhne/archive/2005/06/11/428363.aspx">helper
class</a>. This should also help preventing unwanted memory leaks during the conversions.
</p>
        <img width="0" height="0" src="http://www.damirscorner.com/aggbug.ashx?id=330dfd2e-d438-41fb-ae74-1bafc8ca9076" />
      </body>
      <title>Marshalling System.String to char* and vice-versa</title>
      <guid isPermaLink="false">http://www.damirscorner.com/PermaLink,guid,330dfd2e-d438-41fb-ae74-1bafc8ca9076.aspx</guid>
      <link>http://www.damirscorner.com/MarshallingSystemStringToCharAndViceversa.aspx</link>
      <pubDate>Sun, 28 May 2006 12:41:03 GMT</pubDate>
      <description>&lt;p&gt;
By switching from &lt;a href="http://www.damirscorner.com/blog/UsefulPInvokeResources.aspx"&gt;C#
with P/Invoke calls&lt;/a&gt;&amp;nbsp;to &lt;a href="http://www.damirscorner.com/blog/ImplementingAManagedInterfaceInC.aspx"&gt;Managed
C++&lt;/a&gt;&amp;nbsp;when implementing a managed wrapper for the ANSI C style library I stumbled
upon, I wanted to avoid the tedious and error-prone task of writing the P/Invoke signatures
for function calls and user-defined types for the structures they used. As a side
result I also managed to avoid most of the advanced marshalling issues with complex
data structures.
&lt;/p&gt;
&lt;p&gt;
With simple value types not needing any explicit marshalling only strings need special
attention since &lt;font face="Courier New"&gt;char*&lt;/font&gt; and &lt;font face="Courier New"&gt;System::String&lt;/font&gt; can’t
be implicitly converted between. The &lt;font face="Courier New"&gt;Marshal&lt;/font&gt; class
implements the methods necessary for doing this as demonstrated in the following snippet:
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;String^ MCPP::Together(String^ first, String^ second)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp; // marshal managed strings to unamanged memory 
&lt;br&gt;
&amp;nbsp;&amp;nbsp; IntPtr firstPtr = Marshal::StringToHGlobalAnsi(first);&lt;br&gt;
&amp;nbsp;&amp;nbsp; IntPtr secondPtr = Marshal::StringToHGlobalAnsi(second);&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp; // cast unmanaged buffer to a characer array&lt;br&gt;
&amp;nbsp;&amp;nbsp; char* firstNative = static_cast&amp;lt;char*&amp;gt;(firstPtr.ToPointer());&lt;br&gt;
&amp;nbsp;&amp;nbsp; char* secondNative = static_cast&amp;lt;char*&amp;gt;(secondPtr.ToPointer());&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp; // perform some unmanaged calls&lt;br&gt;
&amp;nbsp;&amp;nbsp; int bufferSize = strlen(firstNative) + strlen(secondNative) + 4;&lt;br&gt;
&amp;nbsp;&amp;nbsp; char* resultBuffer = new char[bufferSize];&lt;br&gt;
&amp;nbsp;&amp;nbsp; sprintf_s(resultBuffer, bufferSize, "%s + %s", firstNative, secondNative);&lt;br&gt;
&amp;nbsp;&amp;nbsp; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp; // marshal unmanaged character array to managed string&lt;br&gt;
&amp;nbsp;&amp;nbsp; String^ result = Marshal::PtrToStringAnsi(static_cast&amp;lt;IntPtr&amp;gt;(resultBuffer));&lt;br&gt;
&amp;nbsp;&amp;nbsp; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp; // free all unmanaged buffers&lt;br&gt;
&amp;nbsp;&amp;nbsp; delete[] resultBuffer;&lt;br&gt;
&amp;nbsp;&amp;nbsp; Marshal::FreeHGlobal(firstPtr);&lt;br&gt;
&amp;nbsp;&amp;nbsp; Marshal::FreeHGlobal(secondPtr);&lt;br&gt;
&amp;nbsp;&amp;nbsp; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp; // return managed string&lt;br&gt;
&amp;nbsp;&amp;nbsp; return result;&lt;br&gt;
}&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
It should be noted that it wouldn’t make any sense switching to unmanaged code to
do some string operations only as shown above. This is for demonstration purposes
only and you would usually call some unmanaged libraries or the like instead of it.
But it is a nice demonstration how cumbersome string operations were in C. If you
were doing this once, the &lt;font face="Courier New"&gt;sprintf_s&lt;/font&gt; function call
might have caught your attention. It’s a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms235384.aspx"&gt;safe
implementation&lt;/a&gt; of &lt;font face="Courier New"&gt;sprintf&lt;/font&gt; which prevents buffer
overflows.
&lt;/p&gt;
&lt;p&gt;
Another point worth mentioning is that you have to take care of allocated memory for
the conversion since your character array is now allocated on the unmanaged heap.
Make sure you use the &lt;font face="Courier New"&gt;FreeHGlobal&lt;/font&gt; method to do it,
not the &lt;font face="Courier New"&gt;free&lt;/font&gt; (used for freeing memory allocated by &lt;font face="Courier New"&gt;*alloc&lt;/font&gt; calls
– ANSI C style) or &lt;font face="Courier New"&gt;delete&lt;/font&gt; (used for freeing memory
allocated by &lt;font face="Courier New"&gt;new&lt;/font&gt; calls – C++ style) functions.
&lt;/p&gt;
&lt;p&gt;
To reduce the code overhead of string conversions between managed and unmanaged world
you might consider wrapping it into a &lt;a href="http://blogs.msdn.com/jeremykuhne/archive/2005/06/11/428363.aspx"&gt;helper
class&lt;/a&gt;. This should also help preventing unwanted memory leaks during the conversions.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.damirscorner.com/aggbug.ashx?id=330dfd2e-d438-41fb-ae74-1bafc8ca9076" /&gt;</description>
      <comments>http://www.damirscorner.com/CommentView,guid,330dfd2e-d438-41fb-ae74-1bafc8ca9076.aspx</comments>
      <category>Development</category>
      <category>Development/.NET</category>
      <category>Development/C++</category>
      <category>Development/Interop</category>
    </item>
    <item>
      <trackback:ping>http://www.damirscorner.com/Trackback.aspx?guid=8a180e88-04f6-4619-983d-055ccb83c536</trackback:ping>
      <pingback:server>http://www.damirscorner.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.damirscorner.com/PermaLink,guid,8a180e88-04f6-4619-983d-055ccb83c536.aspx</pingback:target>
      <dc:creator>Damir Arh</dc:creator>
      <wfw:comment>http://www.damirscorner.com/CommentView,guid,8a180e88-04f6-4619-983d-055ccb83c536.aspx</wfw:comment>
      <wfw:commentRss>http://www.damirscorner.com/SyndicationService.asmx/GetEntryCommentsRss?guid=8a180e88-04f6-4619-983d-055ccb83c536</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Less than a month ago I was certain I’d never have to write managed code in C++. With
such a set of mind I didn’t really care about the new features being added and improvements
being made to managed C++ since the first Visual Studio .NET. Luckily there were people
thinking otherwise and as a result this – often overlooked – member of the .NET family
got better with every release and is actually quite nice in the 2005 version. Don’t
get me wrong – it still is C++ and you probably wouldn’t want to use it if you could
get away with C# or VB. But if <a href="http://www.damirscorner.com/blog/CProjectBuildFromNetworkShareFailsInVS2005.aspx">platform
invoke</a> is giving you too many headaches you might want to take a look at it. It
could be the easiest way to solve your problem.
</p>
        <p>
To make this a little bit easier for those of you who haven’t used C++ for some time
and didn’t bother to check managed C++ at all, I’ll provide a small sample to help
out with the beginner’s problems I had when starting out.
</p>
        <p>
In a situation like mine you’ll usually want to write a class library to wrap the
unmanaged calls for undisturbed use within you managed project. In my case I’ll be
implementing an interface but writing a standalone class is not much different.
</p>
        <p>
Let’s start with the header file. You might remember that C++ requires the separation
of class declaration from method definition, the former being done in a header (<font face="Courier New">*.h</font>)
file and the latter in a code (<font face="Courier New">*.cpp</font>) file. I admit
being a little bit spoiled by not having to do that any more in C#. Let’s name our
header <font face="Courier New">MCPP.h</font> (fell free to guess what it stands for):
</p>
        <p>
          <font face="Courier New">using namespace System;</font>
        </p>
        <p>
          <font face="Courier New">namespace DamirsCorner 
<br />
{<br />
   namespace Samples<br />
   {<br />
      namespace LateBinding<br />
      {<br />
         public ref class MCPP : IBind<br />
         {<br />
         public:<br />
            virtual String^
Together(String^ first, String^ second);<br />
         };<br />
      }<br />
   }<br />
}</font>
        </p>
        <p>
It looks like a hybrid between C++ and C# and one could argue that’s what it actually
is. I’d like to point out the following:
</p>
        <ul>
          <li>
The <font face="Courier New">using</font> directive has an additional keyword <font face="Courier New">namespace</font>.
Also a double colon (static separator in C++) would be used to separate nested namespaces
instead of a dot. You’ll see this in the code file which follows. 
</li>
          <li>
To put your own code into a nested namespace, you’ll have to actually nest the <font face="Courier New">namespace</font> declarations.
Separating several of them with double colons won’t work. 
</li>
          <li>
To declare a managed class, use the additional keyword <font face="Courier New">ref</font>. 
</li>
          <li>
Don’t forget the different use of the <font face="Courier New">public</font> keyword
for the members in C++ compared to C#. 
</li>
          <li>
The <font face="Courier New">^</font> character denotes a reference to a managed object
(stored on the managed heap), separating it from unmanaged pointers (<font face="Courier New">*</font> character). 
</li>
          <li>
Notice the interface <font face="Courier New">IBind</font> being implemented by the
class. A standalone class wouldn’t need this of course. The <font face="Courier New">virtual</font> keyword
in the method declaration would also be skipped in this case.</li>
        </ul>
        <p>
This is it for the header file. Let’s look at the code file now:
</p>
        <p>
          <font face="Courier New">#include "MCPP.h"</font>
        </p>
        <p>
          <font face="Courier New">using namespace System;<br />
using namespace DamirsCorner::Samples::LateBinding;</font>
        </p>
        <p>
          <font face="Courier New">String^ MCPP::Together(String^ first, String^ second)<br />
{<br />
   return String::Empty;<br />
}</font>
        </p>
        <p>
It’s pretty obvious the method doesn’t really do anything but it’s the skeleton we’re
focusing on at the moment. You should turn your attention to the following:
</p>
        <ul>
          <li>
The code file must <font face="Courier New">include</font> the header file it implements
(i.e. defines its methods). To refresh your C++ knowledge: it’s a good habit to use
quotes for internal project header files and pointy brackets for external library
header files. The compiler looks in the project directory first for the former and
in the include directories first for the latter. It checks the other location if it
doesn’t find the file but it will work faster if you use the right one (not to mention
that by doing it you’re avoiding the problem of locating the wrong include with
the same name). 
</li>
          <li>
You can notice the double colons as namespace separators as I mentioned before. 
</li>
          <li>
The methods you’re implementing are not within the class declaration any more. You
already declared the class in the header file therefore you only set the method class
membership here by prefixing its name with the class name and a colon, just like you
were calling a static method.</li>
        </ul>
        <div>There’s a reason I used <font face="Courier New">String</font> for parameter
and return value type. While value types can be directly used in unmanaged code, reference
types have to be correctly marshaled. But that’s a complex enough topic to justify
a separate posting. The only thing left for this one is to make the code above compile.
Just start a new <font face="Courier New">Class Library project</font> in C++ (you’ll
find it in the <font face="Courier New">CLR</font> group) and paste the code to the
right files. After you reference the assembly containing the interface used (similar
to doing it in C#) the code should build successfully.
</div>
        <img width="0" height="0" src="http://www.damirscorner.com/aggbug.ashx?id=8a180e88-04f6-4619-983d-055ccb83c536" />
      </body>
      <title>Implementing a managed interface in C++</title>
      <guid isPermaLink="false">http://www.damirscorner.com/PermaLink,guid,8a180e88-04f6-4619-983d-055ccb83c536.aspx</guid>
      <link>http://www.damirscorner.com/ImplementingAManagedInterfaceInC.aspx</link>
      <pubDate>Sun, 14 May 2006 17:11:27 GMT</pubDate>
      <description>&lt;p&gt;
Less than a month ago I was certain I’d never have to write managed code in C++. With
such a set of mind I didn’t really care about the new features being added and improvements
being made to managed C++ since the first Visual Studio .NET. Luckily there were people
thinking otherwise and as a result this – often overlooked – member of the .NET family
got better with every release and is actually quite nice in the 2005 version. Don’t
get me wrong – it still is C++ and you probably wouldn’t want to use it if you could
get away with C# or VB. But if &lt;a href="http://www.damirscorner.com/blog/CProjectBuildFromNetworkShareFailsInVS2005.aspx"&gt;platform
invoke&lt;/a&gt; is giving you too many headaches you might want to take a look at it. It
could be the easiest way to solve your problem.
&lt;/p&gt;
&lt;p&gt;
To make this a little bit easier for those of you who haven’t used C++ for some time
and didn’t bother to check managed C++ at all, I’ll provide a small sample to help
out with the beginner’s problems I had when starting out.
&lt;/p&gt;
&lt;p&gt;
In a situation like mine you’ll usually want to write a class library to wrap the
unmanaged calls for undisturbed use within you managed project. In my case I’ll be
implementing an interface but writing a standalone class is not much different.
&lt;/p&gt;
&lt;p&gt;
Let’s start with the header file. You might remember that C++ requires the separation
of class declaration from method definition, the former being done in a header (&lt;font face="Courier New"&gt;*.h&lt;/font&gt;)
file and the latter in a code (&lt;font face="Courier New"&gt;*.cpp&lt;/font&gt;) file. I admit
being a little bit spoiled by not having to do that any more in C#. Let’s name our
header &lt;font face="Courier New"&gt;MCPP.h&lt;/font&gt; (fell free to guess what it stands for):
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;using namespace System;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;namespace DamirsCorner 
&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp; namespace Samples&lt;br&gt;
&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; namespace LateBinding&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public ref class MCPP : IBind&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; virtual String^
Together(String^ first, String^ second);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp; }&lt;br&gt;
}&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
It looks like a hybrid between C++ and C# and one could argue that’s what it actually
is. I’d like to point out the following:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
The &lt;font face="Courier New"&gt;using&lt;/font&gt; directive has an additional keyword &lt;font face="Courier New"&gt;namespace&lt;/font&gt;.
Also a double colon (static separator in C++) would be used to separate nested namespaces
instead of a dot. You’ll see this in the code file which follows. 
&lt;/li&gt;
&lt;li&gt;
To put your own code into a nested namespace, you’ll have to actually nest the &lt;font face="Courier New"&gt;namespace&lt;/font&gt; declarations.
Separating several of them with double colons won’t work. 
&lt;/li&gt;
&lt;li&gt;
To declare a managed class, use the additional keyword &lt;font face="Courier New"&gt;ref&lt;/font&gt;. 
&lt;/li&gt;
&lt;li&gt;
Don’t forget the different use of the &lt;font face="Courier New"&gt;public&lt;/font&gt; keyword
for the members in C++ compared to C#. 
&lt;/li&gt;
&lt;li&gt;
The &lt;font face="Courier New"&gt;^&lt;/font&gt; character denotes a reference to a managed object
(stored on the managed heap), separating it from unmanaged pointers (&lt;font face="Courier New"&gt;*&lt;/font&gt; character). 
&lt;/li&gt;
&lt;li&gt;
Notice the interface &lt;font face="Courier New"&gt;IBind&lt;/font&gt; being implemented by the
class. A standalone class wouldn’t need this of course. The &lt;font face="Courier New"&gt;virtual&lt;/font&gt; keyword
in the method declaration would also be skipped in this case.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
This is it for the header file. Let’s look at the code file now:
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;#include "MCPP.h"&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;using namespace System;&lt;br&gt;
using namespace DamirsCorner::Samples::LateBinding;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;String^ MCPP::Together(String^ first, String^ second)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp; return String::Empty;&lt;br&gt;
}&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
It’s pretty obvious the method doesn’t really do anything but it’s the skeleton we’re
focusing on at the moment. You should turn your attention to the following:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
The code file must &lt;font face="Courier New"&gt;include&lt;/font&gt; the header file it implements
(i.e. defines its methods). To refresh your C++ knowledge: it’s a good habit to use
quotes for internal project header files and pointy brackets for external library
header files. The compiler looks in the project directory first for the former and
in the include directories first for the latter. It checks the other location if it
doesn’t find the file but it will work faster if you use the right one (not to mention
that by doing it&amp;nbsp;you’re avoiding the problem of locating the wrong include with
the same name). 
&lt;/li&gt;
&lt;li&gt;
You can notice the double colons as namespace separators as I mentioned before. 
&lt;/li&gt;
&lt;li&gt;
The methods you’re implementing are not within the class declaration any more. You
already declared the class in the header file therefore you only set the method class
membership here by prefixing its name with the class name and a colon, just like you
were calling a static method.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;There’s a reason I used &lt;font face="Courier New"&gt;String&lt;/font&gt; for parameter
and return value type. While value types can be directly used in unmanaged code, reference
types have to be correctly marshaled. But that’s a complex enough topic to justify
a separate posting. The only thing left for this one is to make the code above compile.
Just start a new &lt;font face="Courier New"&gt;Class Library project&lt;/font&gt; in C++ (you’ll
find it in the &lt;font face="Courier New"&gt;CLR&lt;/font&gt; group) and paste the code to the
right files. After you reference the assembly containing the interface used (similar
to doing it in C#) the code should build successfully.
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.damirscorner.com/aggbug.ashx?id=8a180e88-04f6-4619-983d-055ccb83c536" /&gt;</description>
      <comments>http://www.damirscorner.com/CommentView,guid,8a180e88-04f6-4619-983d-055ccb83c536.aspx</comments>
      <category>Development</category>
      <category>Development/.NET</category>
      <category>Development/C++</category>
      <category>Development/Interop</category>
    </item>
    <item>
      <trackback:ping>http://www.damirscorner.com/Trackback.aspx?guid=997811d2-a044-48ff-872d-b3fe29812bd4</trackback:ping>
      <pingback:server>http://www.damirscorner.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.damirscorner.com/PermaLink,guid,997811d2-a044-48ff-872d-b3fe29812bd4.aspx</pingback:target>
      <dc:creator>Damir Arh</dc:creator>
      <wfw:comment>http://www.damirscorner.com/CommentView,guid,997811d2-a044-48ff-872d-b3fe29812bd4.aspx</wfw:comment>
      <wfw:commentRss>http://www.damirscorner.com/SyndicationService.asmx/GetEntryCommentsRss?guid=997811d2-a044-48ff-872d-b3fe29812bd4</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Trying to build a C++ project opened from a network share in Visual Studio 2005 might
fail with a strange error: <font face="Courier New">Command line error D8022 : cannot
open '$(OutDir)\RSP00000115642624.rsp'</font>. Double clicking it in the Error List
window crashes the Visual Studio. The issue is reported in the <a href="http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?feedbackid=8fb28ec3-897a-4467-a2ca-6e11d7c324ac">MSDN
Product Feedback Center</a>.
</p>
        <p>
Additional exploration and experimentation revealed that the problem only appears
when the share host can’t authenticate the user reading from and writing to the share.
This means there will be no error when the user and the share hosting computer are
in the same domain. You can find a more thorough explanation <a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=24453&amp;SiteID=1">in
the following MSDN forum post.</a></p>
        <p>
Unfortunately this usually isn’t the case in the home environment. It’s best you just
avoid the problem altogether by opening the project from a local drive. Just use a
version control system or a synchronization tool to assure the same files are on the
local machine and on the server when you need to.
</p>
        <img width="0" height="0" src="http://www.damirscorner.com/aggbug.ashx?id=997811d2-a044-48ff-872d-b3fe29812bd4" />
      </body>
      <title>C++ project build from network share fails in VS2005</title>
      <guid isPermaLink="false">http://www.damirscorner.com/PermaLink,guid,997811d2-a044-48ff-872d-b3fe29812bd4.aspx</guid>
      <link>http://www.damirscorner.com/CProjectBuildFromNetworkShareFailsInVS2005.aspx</link>
      <pubDate>Sun, 14 May 2006 14:05:50 GMT</pubDate>
      <description>&lt;p&gt;
Trying to build a C++ project opened from a network share in Visual Studio 2005 might
fail with a strange error: &lt;font face="Courier New"&gt;Command line error D8022 : cannot
open '$(OutDir)\RSP00000115642624.rsp'&lt;/font&gt;. Double clicking it in the Error List
window crashes the Visual Studio. The issue is reported in the &lt;a href="http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?feedbackid=8fb28ec3-897a-4467-a2ca-6e11d7c324ac"&gt;MSDN
Product Feedback Center&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Additional exploration and experimentation revealed that the problem only appears
when the share host can’t authenticate the user reading from and writing to the share.
This means there will be no error when the user and the share hosting computer are
in the same domain. You can find a more thorough explanation &lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=24453&amp;amp;SiteID=1"&gt;in
the following MSDN forum post.&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Unfortunately this usually isn’t the case in the home environment. It’s best you just
avoid the problem altogether by opening the project from a local drive. Just use a
version control system or a synchronization tool to assure the same files are on the
local machine and on the server when you need to.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.damirscorner.com/aggbug.ashx?id=997811d2-a044-48ff-872d-b3fe29812bd4" /&gt;</description>
      <comments>http://www.damirscorner.com/CommentView,guid,997811d2-a044-48ff-872d-b3fe29812bd4.aspx</comments>
      <category>Development</category>
      <category>Development/C++</category>
      <category>Software</category>
      <category>Software/VisualStudio</category>
    </item>
  </channel>
</rss>