stdio's printf and Windows Driver

后端 未结 1 1218
野性不改
野性不改 2021-01-15 21:20

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__prin         


        
相关标签:
1条回答
  • 2021-01-15 22:05

    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).
    0 讨论(0)
提交回复
热议问题