views:

184

answers:

1

I have resorted to using the Win32 API calls VirtualAlloc/VirtualFree to allocate and release memory blocks greater than 2GB in size.

I should be able to use the AllocHGlobal function from the System.Runtime.InteropServices.Marshal class to do the same.

However, the following code gives an arithmetic overflow exception (note the explicit cast to long which should instance a 64 bit pointer).


Dim p As IntPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(New IntPtr(CLng(3221225472)))
System.Runtime.InteropServices.Marshal.FreeHGlobal(p)

So my question is, Can I use AllocHGlobal to allocate more than 2GB of memory? If so, how?

A: 

The ArithmeticOverflowException happens in the IntPtr contructor, not in the call to AllocHGlobal. You don't need a Long to hold that value, so try this instead

Marshal.AllocHGlobal(New IntPtr(&HC0000000I))
Mattias S