devjeetroy
devjeetroy

Reputation: 1945

Kernel mode programming using simplistic c++?

I am about to delve into kernel land. My question relates to the programming language. I have seen most tutorials to be written in C. I currently program in C++ and Assembly. I also studied C before C++, but I didn't use it a lot. Would it be possible to program in kernel mode using simplistic C++without using any advanced constructs? Basically I am trying to avoid the minor differences that exist between the two languages(like no bool in C, no automatic returning of 0 from main, really minor differences). I won't be using templates, classes and the like. So would it be possible to program in kernel mode using simplistic C++ without any major annoyances?

Upvotes: 12

Views: 9929

Answers (4)

Andreas Magnusson
Andreas Magnusson

Reputation: 7434

Microsoft has written a guide. Basically they tell us to steer clear of anything but using C++'s relaxed rules of variable declarations...sigh. Anything else and you're on your own. Anyway it can't be all that bad but here are some examples of what you need to remember:

  • Memory allocated in the paged pool can get paged out. If you try to access it when IRQL is above PASSIVE_LEVEL you're screwed (or at least you will be every once in a while when your customer complains about your driver BSODding their system)! Test your driver on a low memory system under load!
  • The non-paged pool is limited, you most likely cannot allocate all your needs from it.
  • Stack is much smaller than in user mode ~12-24K.
  • Anything you do involving floating point path in the kernel must be protected by KeSaveFloatingPointState and KeRestoreFloatingPointState
  • C++ exceptions: No

Read the guide for more. Now if you can make sure that the generated code follows the rules, go ahead and use C++.

Upvotes: 3

Peter
Peter

Reputation: 1068

Add two links if you want to do C++ in WDK. It's a one time setup effort.

The NT Insider:Guest Article: C++ in an NT Driver

The NT Insider:Global Relief Effort - C++ Runtime Support for the NT DDK

Have seen kernel codes use lots of auto-locks/smart-pointers; although they make the code neat, I feel it has a learning curve for beginner to fully understand, and if abused, lots of construct/destruct codes slow things down.

Upvotes: 4

Thierry Franzetti
Thierry Franzetti

Reputation: 1863

Even if not officially supported, you can use C++ as the development language for Windows kernel development. You should be aware of the following things :

  • you MUST define the new and delete operator to map to ExAllocatePoolWithTag and ExFreePool.

  • try to avoid virtual functions. It seems not possible to control the location of the vtable of the object and this may have unexpected results if it is in a pageable portion and you code is called with IRQL >= DISPATCH_LEVEL.

  • if you still need to use virtual methods table than lock .rdata segment before using it on IRQL >= DISPATCH_LEVEL.

Apart from these kinds of limitations, you can use C++ for your driver development.

Upvotes: 19

Alexey Frunze
Alexey Frunze

Reputation: 62048

If you write your code carefully, knowing what exactly stands behind each definition, operator, call, etc, then there should be no problem writing kernel code in C++. The Microsoft document mentioned in the comments above is a good reading precisely because it describes situations in which C++ isn't as transparent as C or doesn't provide similar important guarantees and from that you know what to avoid.

Upvotes: 3

Related Questions