C#驱动开发?

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

C# driver development?

提问by Adam Davis

Before I jump headlong into C#...

在我一头扎进 C# 之前......

I've always felt that C, or maybe C++, was best for developing drivers on Windows. I'm not keen on the idea of developing a driver on a .NET machine.

我一直觉得 C 或 C++ 最适合在 Windows 上开发驱动程序。我并不热衷于在 .NET 机器上开发驱动程序的想法。

But .NET seems to be the way MS is heading for applications development, and so I'm now wondering:

但是 .NET 似乎是 MS 进行应用程序开发的方式,所以我现在想知道:

  • Are people are using C# to develop drivers?
  • Do you have to do a lot of API hooks, or does C# have the facilities to interface with the kernel without a lot of hackery?
  • Can anyone speak to the reliability and safety of running a C# program closer to Ring 0 than would normally be the case?
  • 人们是否正在使用 C# 开发驱动程序?
  • 你是否必须做很多 API 钩子,或者 C# 是否有与内核接口的设施而没有很多hacky?
  • 任何人都可以谈谈运行 C# 程序比通常情况更接近 Ring 0 的可靠性和安全性吗?

I want my devices to be usable in C#, and if driver dev in C# is mature that's obviously the way to go, but I don't want to spend a lot of effort there if it's not recommended.

我希望我的设备可以在 C# 中使用,如果 C# 中的驱动程序开发成熟,那显然是要走的路,但如果不推荐,我不想在那里花费太多精力。

  • What are some good resources to get started, say, developing a simple virtual serial port driver?
  • 什么是入门的好资源,例如,开发一个简单的虚拟串口驱动程序?

-Adam

-亚当

采纳答案by Lasse V. Karlsen

You can not make kernel-mode device drivers in C# as the runtime can't be safely loaded into ring0 and operate as expected.

您不能在 C# 中制作内核模式设备驱动程序,因为运行时不能安全地加载到 ring0 中并按预期运行。

Additionally, C# doesn't create binaries suitable for loading as device drivers, particularly regarding entry points that drivers need to expose. The dependency on the runtime to jump in and analyze and JIT the binary during loading prohibits the direct access the driver subsystem needs to load the binary.

此外,C# 不会创建适合作为设备驱动程序加载的二进制文件,特别是关于驱动程序需要公开的入口点。在加载过程中对运行时跳转和分析和 JIT 二进制文件的依赖禁止了驱动程序子系统加载二进制文件所需的直接访问。

There is work underway, however, to lift some device drivers into user mode, you can see an interview herewith Peter Wieland of the UDMF (User Mode Driver Framework) team.

然而,正在开展工作,将一些设备驱动程序提升到用户模式,您可以在这里看到对 UDMF(用户模式驱动程序框架)团队的 Peter Wieland的采访。

User-mode drivers would be much more suited for managed work, but you'll have to google a bit to find out if C# and .NET will be directly supported. All I know is that kernel level drivers are not doable in only C#.

用户模式驱动程序更适合托管工作,但您必须在谷歌上搜索一下才能确定是否直接支持 C# 和 .NET。我所知道的是内核级驱动程序仅在 C# 中是不可行的。

You can, however, probably make a C/C++ driver, and a C# service (or similar) and have the driver talk to the managed code, if you absolutely have to write a lot of code in C#.

但是,如果您绝对必须用 C# 编写大量代码,您可能可以制作 C/C++ 驱动程序和 C# 服务(或类似服务)并使驱动程序与托管代码对话。

回答by Mladen Jankovi?

It's not direct answer to your question but if you're interested you might look at Singularity project.

这不是您问题的直接答案,但如果您有兴趣,您可以查看Singularity 项目

回答by scable

This shall help you in a way: Windows Driver Kit

这将在某种程度上帮助您:Windows 驱动程序工具包

回答by Robert Gatliff

Microsoft has a number of research projects in the area of having a managed-code OS, in other words kill with Win32 API.

微软在托管代码操作系统领域有许多研究项目,换句话说,用 Win32 API 杀死。

See Mary Jo Foley's article: Rebuilding a Legacy

参见 Mary Jo Foley 的文章:重建遗产

回答by FlySwat

Can anyone speak to the reliability and safety of running a C# program closer to Ring 0 than would normally be the case?

任何人都可以谈谈运行 C# 程序比通常情况更接近 Ring 0 的可靠性和安全性吗?

C# runs in the .NET Virtual Machine, you can't move it any closer to Ring 0 than the VM is, and the VM runs in userspace.

C# 运行在 .NET 虚拟机中,您不能将它移动到比 VM 更靠近 Ring 0 的位置,并且 VM 运行在用户空间中。

回答by Thomas Danecker

Writing device drivers in .net makes no sense for current versions of windows.

在 .net 中编写设备驱动程序对于当前版本的 Windows 没有意义。

<speculation>
Rumors are that MS is investing a lot of money in bringing Singularity to the next level. Just look for Midori. But that's 2015+
</speculation>

<推测> 有
传言称 MS 正在投入大量资金将 Singularity 提升到一个新的水平。只需寻找绿。但那是 2015 年+
</speculation>

回答by Jeffrey Hantin

If you're willing to have a go at a proprietary framework, Jungo's WinDriver toolkitsupports user-mode driver development (even in managed code) for USB, PCI, and PCI-E devices.

如果您愿意尝试专有框架,Jungo 的 WinDriver 工具包支持 USB、PCI 和 PCI-E 设备的用户模式驱动程序开发(甚至在托管代码中)。

回答by unknown6656

If I remember it correctly, the Dokan Projectis a user-mode file system driver, which also allows .NET code to be executed by a system driver: https://github.com/dokan-dev/dokan-dotnet.

如果我没记错的话,Dokan 项目是一个用户模式文件系统驱动程序,它也允许 .NET 代码由系统驱动程序执行:https: //github.com/dokan-dev/dokan-dotnet

So, you could develop a C# "driver" (user-mode application really), which is then called/invoked by a C++ kernel-mode driver. The kernel-driver could simply pass everything along without manipulating the data and act as a simple wrapper.
Needless to mention, that it is very unsafe and you would most likely end with a BSOD (I tried it).

因此,您可以开发一个 C#“驱动程序”(实际上是用户模式应用程序),然后由 C++ 内核模式驱动程序调用/调用。内核驱动程序可以简单地传递所有内容而无需操作数据并充当简单的包装器。
不用说,这是非常不安全的,你很可能会以 BSOD 结束(我试过了)。



Mildly related:

轻度相关:

The Cosmos Projectis an open-source Operating system, which is developed in C# and runs
"(kernel) drivers" and user-level applications written completely in C#/F#/VB.NET/...

宇宙计划是一个开放源码的操作系统,这是在C#和运行发展
“(内核)驱动程序”,并在C#/ F#/ VB.NET /完全写入用户级应用程序...

Though these are technically kernel-level drivers, the OS is no longer Windows but your own, so I guess that this is not a correct answer ......

虽然这些在技术上是内核级驱动程序,但操作系统不再是 Windows 而是你自己的,所以我想这不是一个正确的答案......