Writing drivers in C#
Asked Answered
I

5

32

I have written earlier in C/C++ but currently, I need it to convert into C#.

Can anyone tell me the code/way How to write drivers in 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#.

Idelson answered 15/6, 2009 at 5:33 Comment(3)
I heard the best language for writing drivers is JavaScript nowadays (irony)Goodsell
Can you please share some links?Idelson
Even if you were able to use C#, the nature of driver development requires C++ skills and all involved code is C-related, perhaps assembly related (pointers, segments, paging etc), C# is too high level for that.Vrablik
O
30

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.

There is a DDK but it is not supported in VStudio either (but you can make a makefile project for compilation though).

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.

Odd answered 16/6, 2009 at 14:25 Comment(3)
Considering that code must be signed before execution, it's not possible to make a signed driver either (although IL may be) if the code is JITted. (On the Xbox 360 all XNA/.NET code runs in user space contrary to native code games, so apparently Microsoft has no "solution" for this). There are some C# OSes, though, in which drivers are done in C#, pretty neat.Maurizia
Not all device drivers on Windows require kernel mode to run. The video drivers, and I believe the audio drivers, now run in user mode in both Windows Vista and Windows 7. Also there's now a User-Mode Driver Framework available from Microsoft. Note sure if it works with .NET, probably is still C/C++ based. microsoft.com/whdc/driver/wdf/UMDF.mspxSuchta
This is a Wi-Fi Adapter driver written in C# github.com/ixy-languages/ixy.csOpera
C
30

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

Centriole answered 15/6, 2009 at 7:15 Comment(3)
That link no longer goes directly to UMDF, and my Google-fu turns up nothing that indicates you can write user-mode drivers in C# today.Marciemarcile
@Oran: Thanks, I've updated the link. I think C# is not used for UMDF because Microsoft doesn't provide out-of-the-box support and you need to do a lot of things by yourself (declaring functions, constants, struct and etc).Centriole
Updated 2019: learn.microsoft.com/en-us/windows-hardware/drivers/usbcon/…. "A UMDF-based client driver is implemented as an in-process COM server (DLL), and C++ is the preferred language for writing a client driver for a USB device."Latrell
O
30

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.

There is a DDK but it is not supported in VStudio either (but you can make a makefile project for compilation though).

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.

Odd answered 16/6, 2009 at 14:25 Comment(3)
Considering that code must be signed before execution, it's not possible to make a signed driver either (although IL may be) if the code is JITted. (On the Xbox 360 all XNA/.NET code runs in user space contrary to native code games, so apparently Microsoft has no "solution" for this). There are some C# OSes, though, in which drivers are done in C#, pretty neat.Maurizia
Not all device drivers on Windows require kernel mode to run. The video drivers, and I believe the audio drivers, now run in user mode in both Windows Vista and Windows 7. Also there's now a User-Mode Driver Framework available from Microsoft. Note sure if it works with .NET, probably is still C/C++ based. microsoft.com/whdc/driver/wdf/UMDF.mspxSuchta
This is a Wi-Fi Adapter driver written in C# github.com/ixy-languages/ixy.csOpera
B
12

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# driver development?

Betwixt answered 15/6, 2009 at 5:39 Comment(0)
M
9

It's unclear from your description whether you intend to develop Windows device drivers or to interact with hardware through existing device drivers.

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.

Even USB devices can be accessed from user space (and, ultimately, managed code) through frameworks such as libusb-win32, WinUSB etc.

Marucci answered 28/6, 2009 at 14:19 Comment(0)
W
1

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.

Weisbart answered 15/6, 2009 at 5:38 Comment(2)
Otherwise, is there any way to handle my previous code in C#?Idelson
Well, we don't really know what your code does. Are you talking about writing device drivers in C#, which isn't possible (as mentioned) or are you talking about opening the devices in C# and sending data to them (which should be possible as you should be able to open them as a file and just read/write to the handle).Myrna

© 2022 - 2024 — McMap. All rights reserved.