问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/9103958/how-to-get-around-marshal-copy-32bit-length-limit