Access to driver memory using c#

M

Martin

Hello, I haven't found any answer on this - if it is possible or not.

I have a PCI card with a memory on it. I have also a driver (written in

C++) that maps the memory from the card to user mode - I need very fast

access to this memory, that's why I need to map it directly to my
program.


And now comes my problem - I want to write the control program in c#.


Is there any way how to access to the mapped memory? It is in user
space, I know its size in runtime (not in compile-time), I get its
address using IOCTL from the driver. Now I need to "map" it on Int32
array or something like that...


Martin
 
M

Markus Stoeger

Martin said:
Is there any way how to access to the mapped memory? It is in user
space, I know its size in runtime (not in compile-time), I get its
address using IOCTL from the driver. Now I need to "map" it on Int32
array or something like that...

Have a look at the IntPtr and Marshal classes. IntPtr represents an
address in memory. And Marshal provides methods for reading/writing to
that memory (using the Copy, ReadXXX and WriteXXX methods).

hth,
Max
 
W

Willy Denoyette [MVP]

| Hello, I haven't found any answer on this - if it is possible or not.
|
| I have a PCI card with a memory on it. I have also a driver (written in
|
| C++) that maps the memory from the card to user mode - I need very fast
|
| access to this memory, that's why I need to map it directly to my
| program.
|
|
| And now comes my problem - I want to write the control program in c#.
|
|
| Is there any way how to access to the mapped memory? It is in user
| space, I know its size in runtime (not in compile-time), I get its
| address using IOCTL from the driver. Now I need to "map" it on Int32
| array or something like that...
|
|
| Martin
|

If you can afford the marshaling overhead, you can use the Marshal class to
pass data from the unmanaged to the managed heap, if not, you will have to
resort to unsafe constructs using native pointers.

Willy.
 
M

Martin

Really is there no other way than unsafe and pointers?

In fact in the hardware there are just input and output buffers and
some fifos - and I need fast access to both of the types of memories. I
need only two special types of access to the memory - large data copy
(memcpy in C) and fast access to one address for read/write.

I'm afraid that Marshal class means lots of copying thus it will slow
down the access, am I right?

Martin
 
W

Willy Denoyette [MVP]

| Really is there no other way than unsafe and pointers?
|
| In fact in the hardware there are just input and output buffers and
| some fifos - and I need fast access to both of the types of memories. I
| need only two special types of access to the memory - large data copy
| (memcpy in C) and fast access to one address for read/write.
|
| I'm afraid that Marshal class means lots of copying thus it will slow
| down the access, am I right?
|
| Martin
|

It depends, if you need the unmanaged data to be available as a managed type
(an array of ..), you'll have to copy it somehow.
To do this in C# you have two options, or you can use unsafe code pointers,
or you move the data using Marshal.Copy.
The latter option is your best bet, it's safer, and on V2, it's actually
faster than using unsafe & pointers.
However, if you don't need the unmanaged data to be available as a managed
type, your best option in C# is to access the data directly using unsafe &
pointers.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top