Page 6
www.osr.com
The NT Insider Sep - Oct 2020 © OSR Open Systems Resources, Inc.
I
have long held that C++ sucks, and that it sucks really, really, badly. I have been so very vocal about this over the past 20 or so
years, that almost anyone who’s been involved in Windows driver development and has read NTDEV even a lile has seen me
hold forth on this topic.
I am not alone in this belief. In fact, no lesser beings than Mr. Penguin Pants and Elon Musk agree with me. Ken Thompson and
Rob Pike apparently also agree.
But… I like to think that I’ve changed some over the years.
And over me C++ has very much changed. Versions of C++
before C++ 11 are very dierent from versions of C++ since.
We now have a type of C++ that’s generally referred to as
“Modern C++” – If you’re new to this concept, there’s quite a
nice descripon on MSDN.
I have, somewhat reluctantly due to the extreme level of my
previous vociferousness, concluded that maybe there are
some features of Modern C++ that aren’t so bad. In fact, I
think there are some features that even the most
enthusiasc C Language programmer might nd both useful
and (more importantly) helpful in building quality into
Windows drivers.
In this arcle, I’ll describe a few of the C++ features that we
regularly use at OSR in driver development. You’ll see we’re not enrely “bought in” to the world of C++; But, as C++ has evolved
and we’ve evolved, we’ve come a long way from our previous posion which was “If you put any C++ code in one of our drivers,
you will be hanged, drawn and quartered.”
Limitaons on C++ in Windows Drivers
Before we begin, I should probably note that there are many good and useful things in Modern C++ that cannot be used in kernel-
mode. This is because in Windows kernel-mode does not support C++ excepon handling. C++ excepon handling is enrely
dierent from our customary kernel-mode Structured Excepon Handling. As a result of this limitaon, most parts of the C++
Standard Library (std) are o limits, including some of the most pleasant and useful features such as Containers (std::vector, for
example) and string handling. This also means that you can’t use ATL or STL – both of which are prey much superseded by std
anyhow. Unless, that is, you’re using ATL for wring COM programs. And if you are, well… I’m sorry.
So, having menoned some cool stu that we can’t use, let’s have a look at ve of our favorite C++ constructs that do work in
Windows kernel-mode drivers.
#1: Strong Type Checking
Yes, I realize that this isn’t a Modern C++ innovaon. But it’s sll a massive advantage of C++ over plain old C. There can be lile
disagreement that strong type checking, which is inherent in C++, is a good thing. Over the past million or so years that I’ve been
in this business, I’ve inherited numerous drivers that were inially wrien (usually by clients) in “pure” C. And I can truthfully say
that I have never, not once, failed to nd a meaningful type error when I’ve renamed those les with CPP extensions and rebuilt
them. I think that’s a good enough argument for strong type checking’s value.
#2: constexpr instead of #dene
I hope there’s nobody le in the world who doesn’t think that the use of preprocessor macros in C/C++ is, at best, “problemac.”
By way of review: Good old #dene performs a textual substuon at compile me. This can lead to all sorts of unintenonal
problems, given that there’s no type safety in a #dene, arguments passed into the #dene can get evaluated mulple mes, and
more. I’m not saying you should never use #dene. I’m just saying you should use it when no other tool in your toolbox will do.
(CONTINUED ON PAGE 7)