Search code examples
c++visual-studioc++11driverkmdf

Visual Studio How to use C++ standard library in a kernel mode driver project?


I'm using vs2015 and wdk10, I can use random in an empty project .

#include <random>
std::default_random_engine eng;//works fine .

But when I create an empty kernel mode driver project ,I can't use random in it.

#include <random>
std::default_random_engine eng;//namespace "std" has no member "default_random_engine"

Other standard libraries ,like vector and tuple wouldn't work either , all reminding me that namespace "std" has no member XXX (vector ,tuple ,etc .)

How can I solve this ?


Solution

  • The implementation of the std library requires working exception processing for the code to work correctly. That has stopped a port of the standard library from being performed in kernel.

    Other examples of code which does not work in kernel, is

    • magic statics (thread safe initialization of local variables - requires thread-local-storage, which is not in kernel).
    • static initialization of objects. In a DLL or EXE, the global data of a program is initialized by the runtime before main is called. That code is not present in the kernel
    • stack size. A kernel thread is only 12kb of memory, which makes some algorithms choke, causing exceptions.
    • Memory handling is different in kernel, with memory being allocated with a Tag. That would be lost, or create interfacing issues if you implemented an allocator with a tag.

    As mentioned in the comments

    RtlRandomEx
    

    produces pseudo random numbers, and is available in kernel.

    For cryptographic secure randomness, then this page holds some value.

    MS crypto primatives