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?

- 13,644
- 6
- 48
- 59

- 1,855
- 6
- 26
- 43
-
Do you mean Windows kernel development or other OS development ? – Thierry Franzetti Dec 05 '11 at 20:10
-
Windows kernel development, sorry – devjeetroy Dec 05 '11 at 20:10
-
1If you agree that C is simplistic C++... – sehe Dec 05 '11 at 20:12
-
4You might want to read Microsoft's [commentary](http://msdn.microsoft.com/en-us/windows/hardware/gg487420) on the subject. If you have more specific questions after reading that, you might want to ask them in new/separate questions. – Jerry Coffin Dec 05 '11 at 20:14
-
@sehe I don't, I just meant that not to use any major c++ language features – devjeetroy Dec 05 '11 at 20:19
-
2It's pretty difficult to use C++ without getting tempted to use major C++ features. – Dave Rager Dec 05 '11 at 20:26
4 Answers
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.

- 4,217
- 3
- 39
- 54

- 1,763
- 12
- 12
-
1
-
Thanks! So I see that there are some complications involved. Would you advocate going with C or going with C++? I'm kinda confused with this. – devjeetroy Dec 05 '11 at 20:40
-
2Using C++ as a better C will work fine. The complications arise when the compiler is generating code - including stuff like templates, vtables and default constructors and scalar delete stubs and the like. You can't control which section of your executable the C++ compiler decides to emit these in, which means they could be pageable when the code that needs them must not be. That can cause some serious problems. If you're careful you can avoid this though. – Stewart Dec 05 '11 at 21:11
-
3@ThierryFranzetti In WDK 8 it's possible to have paging control at class level thus ensuring the location of vtable. Another thing worth to mention is not to use exceptions. – SomeWittyUsername Oct 25 '12 at 08:23
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.

- 1,048
- 10
- 23
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.

- 61,140
- 12
- 83
- 180
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
andKeRestoreFloatingPointState
- 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++.

- 7,321
- 3
- 31
- 36
-
2This guide is a bit outdated. Max NonPaged pool is half of physical RAM, so how much memory does marking all your code as nonpageable eat up? The other points are right. – Christopher Feb 14 '12 at 22:20