I’m trying to pass an array of integers from c# to c++ via a managed memory file. Text was easy enough to get working, but I’m out of my depths in the c++ environment, and am not sure how to adjust this for an array of integers.
On the c# side, I pass:
pView = LS.Core.Platforms.Windows.Win32.MapViewOfFile(
hMapFile, // Handle of the map object
LS.Core.Platforms.Windows.Win32.FileMapAccess.FILE_MAP_ALL_ACCESS, // Read and write access
0, // High-order DWORD of file offset
ViewOffset, // Low-order DWORD of file offset
ViewSize // Byte# to map to the view
);
byte[] bMessage2 = Encoding.Unicode.GetBytes(Message2 + '\0');
Marshal.Copy(bMessage2, 0, pView2, bMessage2.Length);
Here pView2 is the pointer to the memory mapped file.
On the c++ side, I call:
LPCWSTR pBuf;
pBuf = (LPCWSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
How would I change this to handle an array of integers instead? Thanks!
a) You can copy the int[] into a byte[]. You can use BitConverter.GetBytes for this or bit arithmetic (byte0 = (byte)(i >> 24); byte1 = (byte)(i >> 16); …)
b) You can use unsafe code to bit-copy (blit) the int[] to the target byte[]
c) Maybe you can use Array.Copy. I think it can handle any blittable value type.
As per the comments I will elaborate on b):