probably at the beach
probably at the beach

Reputation: 15207

c# shared memory between managed and unmanaged code

Is it possible on windows for unmanaged code (c++ / c) to write to an area in memory that is then accessed by managed .Net code (c#) (separate processes) I have a c program that is writing data to an circular memory buffer and I want to process the buffer with unmanaged code.

Upvotes: 3

Views: 3600

Answers (4)

Kent Boogaart
Kent Boogaart

Reputation: 178660

Yes, take a look at the unsafe and fixed keywords.

Upvotes: 1

Stephen Cleary
Stephen Cleary

Reputation: 456507

If you're looking for a solution where an unmanaged process and managed process can share memory, then you can use the MemoryMappedFile class (introduced in .NET 4.0).

If you're looking to share memory between unmaanged and managed code in the same process, then you can use GCHandle to pin a managed array in memory, and pass it to unmanaged code which can access it.

Upvotes: 4

Michael Stoll
Michael Stoll

Reputation: 1344

Yes,

you should use the Marshall class, especially Marshal.AllocHGlobal..

Upvotes: 1

Nate Noonen
Nate Noonen

Reputation: 1371

I think you're looking for Marshaling

Upvotes: 3

Related Questions