windows 使用简单的 C++ 进行内核模式编程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8391151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:35:58  来源:igfitidea点击:

Kernel mode programming using simplistic c++?

c++windowskernelwdk

提问by devjeetroy

I am about to delve into kernel land. My question relates to the programming language. I have seen most tutorials to be written in C. I currently program in C++ and Assembly. I also studied C before C++, but I didn't use it a lot. Would it be possible to program in kernel mode using simplistic C++without using any advanced constructs? Basically I am trying to avoid the minor differences that exist between the two languages(like no boolin C, no automatic returning of 0 from main, really minor differences). I won't be using templates, classes and the like. So would it be possible to program in kernel mode using simplistic C++ without any major annoyances?

我即将深入研究内核领域。我的问题与编程语言有关。我看过大多数教程都是用 C 编写的。我目前用 C++ 和汇编编写程序。在C++之前我也学过C,不过我用的不多。是否可以使用简单的 C++ 在内核模式下编程而不使用任何高级构造?基本上,我试图避免两种语言之间存在的细微差别(例如boolC 中的 no,没有从 0 自动返回main,非常细微的差别)。我不会使用模板、类等。那么是否有可能在内核模式下使用简单的 C++ 进行编程而没有任何大的烦恼呢?

回答by Thierry Franzetti

Even if not officially supported, you can use C++ as the development language for Windows kernel development. You should be aware of the following things :

即使没有官方支持,您也可以使用 C++ 作为 Windows 内核开发的开发语言。您应该注意以下事项:

  • you MUST define the new and delete operator to map to ExAllocatePoolWithTag and ExFreePool.

  • try to avoid virtual functions. It seems not possible to control the location of the vtable of the object and this may have unexpected results if it is in a pageable portion and you code is called with IRQL >= DISPATCH_LEVEL.

  • if you still need to use virtual methods table than lock .rdata segment before using it on IRQL >= DISPATCH_LEVEL.

  • 您必须定义 new 和 delete 运算符以映射到 ExAllocatePoolWithTag 和 ExFreePool。

  • 尽量避免虚函数。似乎无法控制对象的 vtable 的位置,如果它位于可分页部分并且您的代码使用 IRQL >= DISPATCH_LEVEL 调用,则这可能会产生意外结果。

  • 如果在 IRQL >= DISPATCH_LEVEL 上使用之前,您仍然需要使用虚拟方法表而不是锁定 .rdata 段。

Apart from these kinds of limitations, you can use C++ for your driver development.

除了这些限制之外,您还可以使用 C++ 进行驱动程序开发。

回答by Peter

Add two links if you want to do C++ in WDK. It's a one time setup effort.

如果要在 WDK 中执行 C++,请添加两个链接。这是一次设置工作。

The NT Insider:Guest Article: C++ in an NT Driver

NT 内幕:客座文章:NT 驱动程序中的 C++

The NT Insider:Global Relief Effort - C++ Runtime Support for the NT DDK

NT 内幕:全球救援工作 - NT DDK 的 C++ 运行时支持

Have seen kernel codes use lots of auto-locks/smart-pointers; although they make the code neat, I feel it has a learning curve for beginner to fully understand, and if abused, lots of construct/destruct codes slow things down.

已经看到内核代码使用了大量的自动锁/智能指针;虽然它们使代码整洁,但我觉得它有一个初学者完全理解的学习曲线,如果被滥用,许多构造/破坏代码会减慢速度。

回答by Alexey Frunze

If you write your code carefully, knowing what exactly stands behind each definition, operator, call, etc, then there should be no problem writing kernel code in C++. The Microsoft document mentioned in the comments above is a good reading precisely because it describes situations in which C++ isn't as transparent as C or doesn't provide similar important guarantees and from that you know what to avoid.

如果您仔细编写代码,了解每个定义、运算符、调用等背后的确切含义,那么用 C++ 编写内核代码应该没有问题。上面评论中提到的 Microsoft 文档正是一本很好的读物,因为它描述了 C++ 不如 C 透明或不提供类似重要保证的情况,并且您知道应该避免什么。

回答by Andreas Magnusson

Microsoft has written a guide. Basically they tell us to steer clear of anything but using C++'s relaxed rules of variable declarations...sigh. Anything else and you're on your own. Anyway it can't be all that bad but here are some examples of what you need to remember:

微软已经写了一个指南。基本上,他们告诉我们除了使用 C++ 的变量声明的宽松规则之外,不要使用任何东西......叹气。其他任何事情,你就靠自己了。无论如何它不可能那么糟糕,但这里有一些你需要记住的例子:

  • Memory allocated in the paged pool can get paged out. If you try to access it when IRQLis above PASSIVE_LEVEL you're screwed (or at least you will be every once in a while when your customer complains about your driver BSODding their system)! Test your driver on a low memory system under load!
  • The non-paged pool is limited, you most likely cannot allocate all your needs from it.
  • Stack is much smaller than in user mode ~12-24K.
  • Anything you do involving floating point path in the kernel must be protected by KeSaveFloatingPointStateand KeRestoreFloatingPointState
  • C++ exceptions: No
  • 在分页池中分配的内存可以被分页。如果您在IRQL高于 PASSIVE_LEVEL时尝试访问它,那么您就完蛋了(或者至少当您的客户抱怨您的驱动程序 BSODding 他们的系统时,您会时不时遇到)!在负载下的低内存系统上测试您的驱动程序!
  • 非分页池是有限的,您很可能无法从中分配所有需求。
  • 堆栈比用户模式小得多~12-24K。
  • 任何你涉及浮点路径在内核中必须加以保护KeSaveFloatingPointStateKeRestoreFloatingPointState
  • C++ 异常:

Read the guide for more. Now if you can make sure that the generated code follows the rules, go ahead and use C++.

阅读指南了解更多信息。现在,如果您可以确保生成的代码符合规则,请继续使用 C++。