可以用 Python 编写 Windows 驱动程序吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/981200/
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 12:37:43  来源:igfitidea点击:

Can Windows drivers be written in Python?

pythonwindowsdrivers

提问by Armageddon

Can Windows drivers be written in Python?

可以用 Python 编写 Windows 驱动程序吗?

回答by MSalters

Yes. You cannot create the "classic" kernel-mode drivers. However, starting with XP, Windows offers a User-Mode Driver Framework. They can't do everything, obviously - any driver used in booting the OS obviously has to be kernel-mode. But with UMDF, you only need to implement COM components.

是的。您不能创建“经典”内核模式驱动程序。但是,从 XP 开始,Windows 提供了用户模式驱动程序框架。显然,他们不能做所有事情——任何用于引导操作系统的驱动程序显然都必须是内核模式。但是使用 UMDF,您只需要实现 COM 组件。

Besides boot-time drivers, you also can't write UMDF drivers that:

除了启动时驱动程序,您也不能编写以下 UMDF 驱动程序:

  • Handle interrupts
  • Directly access hardware, such as direct memory access (DMA)
  • have strict timing loops
  • Use nonpaged pool or other resources that are reserved for kernel mode
  • 处理中断
  • 直接访问硬件,例如直接内存访问 (DMA)
  • 有严格的计时循环
  • 使用为内核模式保留的非分页池或其他资源

回答by Judge Maygarden

The definitive answer is not without embedding an interpreter in your otherwise C/assembly driver. Unless someone has a framework available, then the answer is no. Once you have the interpreter and bindings in place then the rest of the logic could be done in Python.

最终的答案并非没有在您的其他 C/汇编驱动程序中嵌入解释器。除非有人有可用的框架,否则答案是否定的。一旦你有了解释器和绑定,剩下的逻辑就可以在 Python 中完成了。

However, writing drivers is one of the things for which C is best suited. I imagine the resulting Python code would look a whole lot like C code and defeat the purpose of the interpreter overhead.

然而,编写驱动程序是 C 最适合的事情之一。我想象生成的 Python 代码看起来很像 C 代码,并且会破坏解释器开销的目的。

回答by MSalters

A good way to gain insight why this is practically impossible is by reading Microsoft's adviceon the use of C++ in drivers. As a derivative of C, the use of C++ appears to be straightforward. In practice, not so.

了解为什么这实际上不可能的一个好方法是阅读Microsoft关于在驱动程序中使用 C++的建议。作为 C 的衍生物,C++ 的使用似乎很简单。在实践中,并非如此。

For instance, you must decide for every function (and really every assembly instruction) whether it's in pageable or non-pageable memory. This requires extensions to C, careful use of new C++ features, or in this case a special extension to the Python language and VM. In addition, your driver-compatible VM would also have to deal with the different IRQLs - there's a hierarchy of "levels" which restrict what you can and cannot do.

例如,您必须决定每个函数(实际上是每个汇编指令)是在可分页还是不可分页内存中。这需要对 C 的扩展,小心使用新的 C++ 功能,或者在这种情况下对 Python 语言和 VM 进行特殊扩展。此外,与驱动程序兼容的 VM 还必须处理不同的 IRQL——“级别”的层次结构限制了您可以做什么和不能做什么。

回答by David Cournapeau

I don't know the restrictions on drivers on windows (memory allocation schemes, dynamic load of libraries and all), but you may be able to embed a python interpreter in your driver, at which point you can do whatever you want. Not that I think it is a good idea :)

我不知道对 Windows 驱动程序的限制(内存分配方案、库的动态加载等等),但是您可以在驱动程序中嵌入 python 解释器,此时您可以为所欲为。并不是说我认为这是个好主意:)

回答by Mark Lutton

Python runs in a virtual machine, so no.

Python 在虚拟机中运行,所以没有。

BUT:

但:

You could write a compiler that translates Python code to machine language. Once you've done that, you can do it.

您可以编写一个编译器,将 Python 代码翻译成机器语言。一旦你做到了,你就可以做到。

回答by Mendelt

Never say never but eh.. no

永远不要说永远,但呃..不

You might be able to hack something together to run user-mode parts of drivers in python. But kernel-mode stuff can only be done in C or assembly.

您也许可以一起破解一些东西以在 python 中运行驱动程序的用户模式部分。但是内核模式的东西只能用 C 或汇编来完成。

回答by JaredPar

No they cannot. Windows drivers must be written in a language that can

不,他们不能。Windows 驱动程序必须用一种语言编写

  1. Interface with the C based API
  2. Compile down to machine code
  1. 与基于 C 的 API 的接口
  2. 编译成机器码

Then again, there's nothing stopping you from writing a compiler that translates python to machine code ;)

再说一次,没有什么能阻止您编写将 python 转换为机器代码的编译器;)