REVIEW - C++/CLI in Action


C++/CLI in Action


Nishant Sivakumar



Manning Publications (2007)




Seb Rose


December 2009



Comparative review with Expert Visual C++/CLI by Marcus Heege.

Both books are targeted at experienced Visual C++ developers and they both have extensive coverage of getting native and managed code to interoperate. This is no surprise, since it's unlikely that many people will use C++/CLI to write new code that targets .NET, due to the added complexity and (relatively) poor support from the Visual Studio IDE.

I read Heege's book first and found it clear, concise and very dry. There aren't many jokes in the 352 pages, but I don't think I found a redundant paragraph either.

The first chapter gives an overview of the .NET landscape and how C++/CLI fits into it. He gets right into details of source file and object file compatibility and gives you a flavour of the power and flexibility of this binding.

The next 5 chapters cover all aspects of the managed environment. It's not rocket science, but is necessary to give an old-school C++ programmer enough knowledge to see what's going on. At the end of these chapters you will be able to write managed C++/CLI programs.

Chapter 7 shows how to extend native Visual C++ projects with managed code. There's in depth coverage of compilation models, compiler switches and exception handling. There's also a handy step-by-step guide to modifying your Visual Studio project to get it all working, which includes trouble shooting tips that really help. (Try finding Configuration Properties|Linker|Advanced|CLR Thread Attribute on your own when the CLR initializes the wrong COM apartment!)

Chapter 8 shows you how to create hybrid types that (appear to) have native and managed members. It's all smoke and mirrors (until/unless the unification hinted at in Herb Sutter's Design Rationale gets implemented), but is fairly simple to grasp. There are even some lovely MFC macros that allow your native types to handle managed events.

Chapter 9 dives deep into the mechanics of interoperation, covering thunks, double thunking and calling conventions (among others). There are quite a lot of pretty pictures of managed-unmanaged transitions and another hefty dose of Common Intermediate Language (CIL, latterly known as MSIL). He walks through each transition type and, though you'll need to go back to it several times before it sinks in, he makes it clear and comprehensible.

Chapter 10 covers wrapping native libraries so that they can be accessed by managed callers. He discusses design concerns that aren't strictly anything to do with C++/CLI or even .NET, and touches on CLS compliance. There's clearly going to be a lot of marshalling going on as objects make their way across the managed/unmanaged boundaries and he covers the library facilities that make this easier. This has all got a lot neater in Visual Studio 2008 with the template driven marshall_as library.

Chapter 11 introduces some techniques and library facilities for managing resources including IDisposable, Finalizers and the hugely useful SafeHandle. He also covers how to handle the various .NET asynchronous exceptions in an approved way, which is probably more than most developers need to know.

By the time you get to chapter 12 (the last chapter) you'd hope things were getting so esoteric that you could just skim over it, but you'd be wrong. Here he covers application startup and describes the various idiosyncracies of initializing the CRT depending on compilation model and whether you've built a DLL or an EXE.

The first appendix is a really useful utility that helps you modify machine settings so that you can run mixed-mode applications from network shares. The second appendix details a small app he wrote to measure the performance of thunks.

Finally, the index is adequate, and APress offer a PDF version of the book for US$10.

Having read Heege's book a couple of times I dived in Sivakumar's. The style couldn't be more different. It's conversational and repetitive. Each section ends by summarising what the next section is going to tell you. The use of similes is (to my mind) excessive. I don't want to be told that "Doing X is like putting a Chrysler Fender on a Ford Escort", but that's exactly the sort of thing I was told - over and over again.

The first chapter starts by setting the C++/CLI scene but doesn't really get into object/source file compatibility and jumps into the syntax on page 13, with the ubiquitous "Hello, world". The rest of chapter 1, and chapters 2 and 3 stick with the C++/CLI extensions.

Chapter 4 tackles mixed mode programming and mixed types. It also covers part of the marshalling library that Heege omitted - marshalling between function pointer and delegates.

Chapter 5 continues covering marshalling and talks about thunking, but not in any great detail. It then goes on to cover wrapping a native library and accessing COM objects. It's all quite brief, but you do feel that the ground has been covered.

Chapter 6 details mixing Windows Forms with MFC.

Chapter 7 explains how to target WPF with C++/CLI. It then goes on to show how you can host WPF in a native C++ app and use a native control in a WPF app.

Chapter 8 gives a brief tour of using the Windows Communication Foundation (WCF) from C++/CLI. It goes into some detail about how to migrate a DCOM app to WCF.

The appendix gives a concise introduction to the .NET framework.

As you'll have gathered by now, I found Heege's book to be well worth reading and a valuable resource. Sivakumar's book, by contrast, is a looser, lighter book, that seems more interested in the latest 'cool' technologies, rather than a deep understanding of what is going on, but it does cover some material that isn't in Heege's book.

Book cover image courtesy of Open Library.

Your Privacy

By clicking "Accept Non-Essential Cookies" you agree ACCU can store non-essential cookies on your device and disclose information in accordance with our Privacy Policy and Cookie Policy.

Current Setting: Non-Essential Cookies REJECTED

By clicking "Include Third Party Content" you agree ACCU can forward your IP address to third-party sites (such as YouTube) to enhance the information presented on this site, and that third-party sites may store cookies on your device.

Current Setting: Third Party Content EXCLUDED

Settings can be changed at any time from the Cookie Policy page.