C# driver development?
Asked Answered
W

8

18

Before I jump headlong into 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.

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

  • 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?

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.

  • What are some good resources to get started, say, developing a simple virtual serial port driver?

-Adam

Wein answered 16/9, 2008 at 19:14 Comment(1)
I doubt we will see managed code drivers until the OS is built on top of the framework - which is not the case at presentAhders
Q
28

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

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.

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

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#.

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#.

Quit answered 16/9, 2008 at 19:32 Comment(1)
I'd like to add that some driver are not allowed by MS to be kernel mode drivers anyway, so in that case a user mode managed or unmanaged code would be fine.Blent
R
4

This shall help you in a way: Windows Driver Kit

Rycca answered 16/9, 2008 at 19:23 Comment(0)
R
2

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

Raskin answered 16/9, 2008 at 19:18 Comment(0)
V
2

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

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.

Victoir answered 16/9, 2008 at 19:38 Comment(2)
There is no .NET Virtual Machine. The .NET execution environment does not employ a virtual machine, unlike Java, for example. CIL is always compiled to native code prior to execution. No virtualization involved.Homeland
Emphasis on prior to execution. Otherwise, the .NET CLR is very similar to a traditional virtual machine like the JVM.Tsunami
E
2

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

Elconin answered 18/11, 2009 at 3:31 Comment(0)
E
1

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

See Mary Jo Foley's article: Rebuilding a Legacy

Easygoing answered 16/9, 2008 at 19:24 Comment(0)
E
1

Writing device drivers in .net makes no sense for current versions of 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>

Entomostracan answered 16/9, 2008 at 20:1 Comment(0)
M
1

If I remember it correctly, the Dokan Project is 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.

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).


Mildly related:

The Cosmos Project is 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/...

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 ......

Meddlesome answered 12/5, 2018 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.