stdio 的 printf 和 Windows 驱动程序

发布于 2024-11-14 15:55:25 字数 309 浏览 2 评论 0原文

我想在驱动程序代码(DDK)中使用“printf”,因此我包含了stdio.h。但编译器说:

error LNK2001: unresolved external symbol __imp__printf

有什么想法吗?我在某个地方看到这是不可能的——但这太糟糕了——我简直不敢相信。为什么我不能在内核代码中使用标准 C 例程?

  1. 像 printf 这样的 C 函数来自静态 cstd.lib 或 AFAIK 的东西,不是吗?
  2. 那为什么 WDK 会向我提供 stdio.h 呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撑一把青伞 2024-11-21 15:55:25

Windows 内核仅支持部分标准 C 运行时。特别是,不支持高级功能(例如文件流、控制台 I/O 和网络)。相反,您需要使用本机内核 API 来实现类似的功能。

stdio.h 包含在 WDK 中的原因是 C 运行时的某些部分是为了您的方便而提供的。例如,您可以使用memcmp(尽管首选本机RtlCompareMemory)。 Microsoft 尚未通过 CRT 标头来 #ifdef 剔除内核模式下不可用的零碎内容。一旦您积累了一些编写内核驱动程序的经验,您就会掌握内核中可能的功能以及可能不起作用的功能。

为了解决您的高级问题:您可能正在寻找一些调试/日志记录机制。您实际上有两个选择:

  1. DbgPrintEx 是最容易使用的。它基本上是 printf 的一个插件(尽管在运行 >=DISPATCH_LEVEL 时需要小心某些类型的字符串插入)。输出将发送到调试器,或者,如果您愿意,也可以发送到 DbgView
  2. WPP 是工业强度选项。最初的学习曲线相当陡峭(尽管 WDK 中有示例)。然而,它非常灵活(例如,您可以创建自己的尖叫声,例如 Print("My IP 地址是:%!IPV4!", ip);),而且速度非常快(Microsoft在大多数 Windows 组件的非调试版本中提供 WPP 跟踪)。

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).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文