Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get around Marshal.Copy (32bit) length limit?

Tags:

c#

interop

c#-4.0

I'm trying to move data back/forth between managed (C#) and unmanaged (C++ Win32) code. I can use Marshal.Copy and it works just fine until the datasets get bigger > 2GB as Marshal.Copy has a signed 32 bit int (2GB) limit for length.

Any idea how to get around this? Currently I use AllocHGlobal(IntPtr) on the managed side and .ToPointer() on the unmanaged side. If I can't use Marshal.Copy to move large data (> 2GB) back/forth what can I use?

like image 351
Jack Thefullname Avatar asked Feb 01 '12 21:02

Jack Thefullname


1 Answers

My first reaction was: why are your copying 2GB+ of data?

Perhaps your application constraints won't allow it, but it seems to me that if your data set is that larger than what is allowed by the framework you should not be looking for trick to get around the framework. How about another method of access altogether?

There are numerous ways around this problem. For starters you could wrap the memory in a stream and pull the data into the unmanaged code. You could also create your own interface to bring the data in piece meal. Memory mapped files come to mind as well.

Without knowing the specific contstraints of the application, maybe you cannot change the unmanaged code, I would suggest finding another method rather than working around the framework.

like image 139
Ken Brittain Avatar answered Oct 17 '22 07:10

Ken Brittain