Logo
blank Skip to main content

Using STL C++ for Driver Development

C++

For many years, Windows drivers were traditionally written in the C language. But with the spread of the Kernel-Mode Driver Framework (KMDF), this tradition has changed.

The general understanding of the C++ usage and its limitations for kernel mode can be found in the C++ for Kernel Mode Drivers: Pros and Cons guideline from Microsoft [DOC]. However, this document contains a lot of outdated information, and it’s better to search for current limitations applicable to kernel drivers written in C++ on the Microsoft website.

Also, some language tools are not supported by the standard:

  • new and delete global statements are not presented here (this doesn’t cause problems as new and delete statements can be implemented very easy)
  • constructors and destructors of global objects are not called (it’s not a big problem either, as it’s possible to create/initialize global objects with new statement )
  • there is no support for C++ exceptions
  • there is no support for Run-Time Type Information (RTTI) (as a rule, it doesn’t cause problems because of doubtful usefulness of RTTI in drivers)

The most critical and unpleasant limitation is that C++ exceptions aren’t supported – after all, exceptions are an exceptionally convenient mechanism for handling errors.

Need expert help with C, C++, or C# development?

Accelerate your project with Apriorit’s proven expertise in low-level programming and system-level solutions.

There is a kind of substitution for C++ exceptions – it’ SEH (Structured Exception Handling), the standard Windows exception handling mechanism. This mechanism is less convenient than C++ exceptions and its main disadvantage is the following: when throwing an exception from a function, the destructors of the local objects constructed in this function are not called.

However, the limitations mentioned above are not fundamental for drivers. The part of C++ standard library ensures the functioning of the indicated language tools. There is no such library for the kernel mode of the operating system, but nothing prevents us from implementing this missing part of the standard library, and, to be honest, it was done by some unrecognized hero on the Internet.

Read also

Linux Device Drivers: Tutorial for Linux Driver Development

Explore this practical guide to writing a simple Linux kernel driver. Understand the essentials of module creation, loading, and debugging with Apriorit’s help.

Learn more
linux device driver development

When functioning of all language tools is provided, STL, the most powerful standard library of C++, can be used the most valuably, and also porting of other libraries from user-mode to kernel-mode is substantially simplified.

We must also mention that except for the listed advantages of using C++, there are some disadvantages:

  • Exceptions processing takes a lot of space in stack and this can cause its overflow as stack size in kernel mode is very small (about 12 Kb). That’s why one should work with exceptions very carefully.
  • STL library often allocates a lot of small areas in dynamic memory that causes its fragmentation.
  • The architecture of exceptions was rebuilt much in 64-bit version of Windows and the 64-bit version of libcpp library does not exist at the moment. As a result, it’s impossible to port the drivers which use it to Windows x64. Now we are doing research of this point and apparently we’ll have to rewrite some parts of this library ourselves.

Have a question?

Ask our expert!

Michael-Teslia
Michael Teslia

Program Manager

Tell us about
your project

...And our team will:

  • Process your request within 1-2 business days.
  • Get back to you with an offer based on your project's scope and requirements.
  • Set a call to discuss your future project in detail and finalize the offer.
  • Sign a contract with you to start working on your project.

Do not have any specific task for us in mind but our skills seem interesting? Get a quick Apriorit intro to better understand our team capabilities.

* By sending us your request you confirm that you read and accepted our Terms & Conditions and Privacy Policy.