views:

560

answers:

2

Can standard pointers in .Net do this? Or does one need to resort to P/invoke?

Note that I'm not talking about object references; I'm talking about actual C# pointers in unsafe code.

+3  A: 

C#, as a managed and protected run time engine, does not allow low level hardware access and the memory locations associated with actual hardware are not available.

You'll need to use a port driver or write your own in C++ or C with the proper Windows API to access the memory mapped I/O regions of interest. This will run in a lower ring than the C# programs are capable of.

This is why you don't see drivers written in C#, although I understand many are writing access routines with C++, but the main driver logic in C#. It's tricky, though, as crashes and restarting can become tricky, not to mention synchronization and timing issues (which are somewhat more concrete in C++ at a lower ring, even though windows is far from a real-time system).

Adam Davis
Not to mention the high overhead for PInvoke
Rick Minerich
+2  A: 

To expand on Adam's answer, you can't even perform memory-mapped I/O from a Win32 application without the cooperation of a kernel driver. All addresses a Win32 app gets are virtual addresses that have nothing to do with physical addresses.

You either need to write a kernel driver to do what you're talking about or have a driver installed that has an API that'll let you make requests for I/O against particular physical addresses (and such a driver would be a pretty big security hole waiting to happen, I'd imagine). I seem to recall that way back when some outfit had such a driver as part of a development kit to help port legacy DOS/Win16 or whatever device code to Win32. I don't remember its name or know if it's still around.

Michael Burr