用 C# 编写驱动程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/994600/
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
Writing drivers in C#
提问by Gaurav Aroraa
I have written earlier in C/C++ but currently, I need it to convert into C#.
我之前用 C/C++ 编写过,但目前,我需要将其转换为 C#。
Can anyone tell me the code/way How to write drivers in C#?
谁能告诉我如何在 C# 中编写驱动程序的代码/方式?
Actually currently I have some problems with my old application written in C++ and we have to write the drivers of our LPT1,COM Printers and other USB drivers in C#.
实际上,目前我用 C++ 编写的旧应用程序存在一些问题,我们必须用 C# 编写 LPT1、COM 打印机和其他 USB 驱动程序的驱动程序。
采纳答案by No hay Problema
Simply you can't. C# produces intermediate language that is interpreted by a virtual machine (.NET). All these stuff runs in user mode and WDM drivers run in kernel mode.
只是你不能。C# 生成由虚拟机 (.NET) 解释的中间语言。所有这些东西都在用户模式下运行,而 WDM 驱动程序在内核模式下运行。
There is a DDK but it is not supported in VStudio either (but you can make a makefile project for compilation though).
有一个 DDK,但它在 VStudio 中也不支持(但您可以制作一个 makefile 项目进行编译)。
Driver development is complex, prone to blue screen and requires a good understanding of C , kernel structures and mem manipulation. None of those skills are required for C# and .NET therefore there is a long and painful training path.
驱动程序开发很复杂,容易出现蓝屏,需要对 C、内核结构和内存操作有很好的理解。C# 和 .NET 不需要这些技能,因此有一个漫长而痛苦的培训路径。
回答by Paul Sonier
You can't write drivers in C#; drivers need to run with elevated privilege to be able to talk to hardware; managed code cannot be run in the appropriate environment.
你不能用 C# 编写驱动程序;驱动程序需要以提升的权限运行才能与硬件通信;托管代码无法在适当的环境中运行。
回答by Robert Horvick
You cannot write kernel mode drivers in C# (the runtime executes in user mode therefore you can't get into ring0). This SO Q/A provides some links you may find helpful:
您不能在 C# 中编写内核模式驱动程序(运行时在用户模式下执行,因此您无法进入 ring0)。此 SO Q/A 提供了一些您可能会觉得有用的链接:
回答by Sergey Podobry
Actually, you can write some drivers in C# if you use UMDF because it runs in usermode (see Getting Started with UMDF). But my recommendation is to use C/C++.
实际上,如果您使用 UMDF,您可以在 C# 中编写一些驱动程序,因为它在用户模式下运行(请参阅UMDF 入门)。但我的建议是使用 C/C++。
回答by Ilya
It's unclear from your description whether you intend to develop Windows device drivers or to interact with hardware through existing device drivers.
从您的描述中不清楚您是打算开发 Windows 设备驱动程序还是通过现有设备驱动程序与硬件交互。
For example, to interact with devices connected to your serial port, you don't need to write your own driver and in fact, you can access it through .NET's SerialPort
class.
例如,要与连接到串行端口的设备进行交互,您不需要编写自己的驱动程序,事实上,您可以通过 .NET 的SerialPort
类访问它。
Even USB devices can be accessed from user space (and, ultimately, managed code) through frameworks such as libusb-win32, WinUSBetc.
甚至 USB 设备也可以通过libusb-win32、WinUSB等框架从用户空间(最终是托管代码)访问。