I want to use “printf” in driver code (DDK), therefore I’ve included stdio.h. But the compiler says:
error LNK2001: unresolved external symbol __imp__printf
Any ideas? I seen somewhere that it is not possible – but that’s awful – I can’t believe it. Why can’t I use standard C routines in kernel code?
- C functions like printf come from a static cstd.lib or something AFAIK don’t they?
- Why would WDK provide me with stdio.h then?
The Windows kernel only supports part of the standard C runtime. In particular, high-level functionality — like file streams, console I/O, and networking — is not supported. Instead, you need to use native kernel APIs for similar functionality.
The reason that stdio.h is included with the WDK is because some parts of the C runtime are provided for your convenience. For example, you can use
memcmp(although the nativeRtlCompareMemoryis preferred). Microsoft has not picked through the CRT headers to #ifdef out the bits and pieces that are not available in kernel mode. Once you develop some experience writing kernel drivers, you’ll get the hang of what’s possible in the kernel, and what probably won’t work.To address your high-level question: you’re probably looking for some debug/logging mechanism. You really have two options:
DbgPrintExis the easiest to use. It’s basically a drop-in for printf (although you need to be careful about certain types of string inserts when running >=DISPATCH_LEVEL). Output goes to the debugger, or, if you like, to DbgView.Print("My IP address is: %!IPV4!", ip);), and it is very fast (Microsoft ships WPP tracing in the non-debug builds of most Windows components).