Copying data onto unmanaged memory in C#

L

Luk Vloemans

Hi,

I'm trying to copy data onto unmanaged memory in C#.

I found the how you would normally do it in C++:

uint eventMask = 0;
IntPtr uMask = Marshal.AllocHGlobal(Marshal.SizeOf(eventMask));
Marshal.WriteInt32(uMask, 0); //0 is just a random value.

Does anybody know how to do this in C# ?
The problem is, C# (on Compact .Net Framework) doesn't support the
AllocHGlobal property of the class Marshal.

Anybody any idea ?

Thanks in advance!

-Luk Vloemans
IT student
 
N

Neil Cowburn [MVP]

You will need to P/Invoke LocalAlloc (found in coredll.dll) instead of
calling Marshal.AllocHGlobal. Don't forget to call FreeLocal when you are
done to free up the unmanaged memory.

Interestingly, OpenNETCF.org have a custom Marshal class in the works which
supports AllocHGlobal/FreeHGlobal.

HTH
Neil
 

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