Marshaling .NET and STL Collections in C++/CLI

August 25, 2014

When working with C++/CLI, you often have to convert C++ types to CLR types and vice versa. This most commonly happens with strings, but custom types and collections are just as painful. Visual C++ ships with a marshaling library in the msclr::interop namespace, which focuses on marshaling strings. However, it is notably lacking in the ability to marshal collections -- so you're left to your own devices if you have a std::map<std::string, std::vector<double>> that you need to mash into a .NET Dictionary<string, List<double>>. My wife and I spent a weekend writing a template library that marshals .NET and STL collections....
tags: ,
one comment

C++ Sink Parameter Passing

August 21, 2014

C++ is the most complex language I know, and its parameter passing rules are only getting more arcane now that we have rvalue references in C++ 11. In this post I'd like to examine the specific scenario of passing parameters to a sink method, which consumes its parameters. What do I mean by "consume"? If a parameter is movable and safe to move from (i.e. it's an rvalue), it should move from it; if a parameter is only copyable, it should copy it. Here are a couple of sink methods from the C++ Standard Library: std::vector's push_back method will move from...

Make Move Constructors No-Throw

August 8, 2014

tl;dr: It's extremely important to make sure that your move constructor is no-throw and that it's marked noexcept. If you rely on the compiler-generated move constructor, you're good to go. Otherwise, read on.There is an important category of functions in C++ that are not allowed to throw exceptions. You should never allow an exception to escape from a destructor. If your destructor ends up throwing an exception, you will eventually get to a situation where your program's state is indeterminate and all you can do is exit the process. For example, if your destructor throws as part of an...
tags: ,
one comment

A Motivating Example of WinDbg Scripting for .NET Developers

August 5, 2014

WinDbg scripting gets a pretty bad name -- its somewhat contrived syntax, weird limitations, and hard to decipher expressions being the common culprits. In some cases, however, WinDbg scripts can be a very effective and reliable tool for extracting information from memory and processing it in a meaningful way. This post offers a simple example that hopefully will be useful as you begin to explore WinDbg scripts. For a more thorough explanation and more complex scripts, make sure to check my past posts on traversing std::vector and std::map. Let's set the stage with a simple console application that creates a number...