tags:

views:

1647

answers:

6

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

+1  A: 

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.

McWafflestix
Otherwise, is there any way to handle my previous code in C#?
Rick
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).
Timo Geusch
+7  A: 

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:

http://stackoverflow.com/questions/75886/c-driver-development

Bubbafat
+11  A: 

Actually, you can write some drivers in C# (see User-Mode Driver Framework (UMDF)). But my recommendation is to use C/C++.

Sergius
A: 

you can't do Drivers And Native Exes with c#. beside that all the world do it with c/c++. why you want to do it with c# because really it is good to do it with c/c++.

mavric
+2  A: 

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.

No hay Problema
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.
Cecil Has a Name
+4  A: 

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.

Ilya