Alexander Shukaev
Alexander Shukaev

Reputation: 17021

stdio's printf and Windows Driver

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?

  1. C functions like printf come from a static cstd.lib or something AFAIK don't they?
  2. Why would WDK provide me with stdio.h then?

Upvotes: 1

Views: 3036

Answers (1)

Jeffrey Tippet
Jeffrey Tippet

Reputation: 3006

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 native RtlCompareMemory is 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:

  1. DbgPrintEx is 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.
  2. WPP is the industrial-strength option. The initial learning curve is pretty steep (although there are samples in the WDK). However, it is very flexible (e.g., you can create your own shrieks, like 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).

Upvotes: 5

Related Questions