Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello every one
I want to communicate with C++ program with my C#, that is data transfer between C++ and C# using shared memory , Here is my Implementation
I have 2 methods
CreateMemoryShare();
ReleaseMemoryShare();
both on writer and reader side

C#
SafeFileMappingHandle hMapFile = null;
       IntPtr pView;

[DllImport("Kernel32.dll", SetLastError = true)]
           public static extern SafeFileMappingHandle CreateFileMapping(IntPtr hFile, IntPtr lpAttributes, FileProtection flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName);



[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
           public static extern IntPtr MapViewOfFile(SafeFileMappingHandle hFileMappingObject, FileMapAccess dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap);





private void CreateMemoryShare()
       {
           hMapFile = NativeMethod.CreateFileMapping(INVALID_HANDLE_VALUE, IntPtr.Zero, FileProtection.PAGE_READWRITE, 0, MapSize, MapName);
           pView = NativeMethod.MapViewOfFile(hMapFile, FileMapAccess.FILE_MAP_ALL_ACCESS, 0, ViewOffset, ViewSize);
       }


private void ReleaseMemoryShare()
       {
           NativeMethod.UnmapViewOfFile(pView);
           pView = IntPtr.Zero;
           hMapFile.Close();
           hMapFile = null;
       }



At the Write/Sender side


C#
OpenFileDialog objOpenFile = new OpenFileDialog();
           objOpenFile.ShowDialog();
           FileStream fs = new FileStream(objOpenFile.FileName, FileMode.Open);
           BinaryReader objBinaryReader = new BinaryReader(fs);
           byte[] ImageBuffer = new byte[fs.Length];
           objBinaryReader.Read(ImageBuffer, 0, ImageBuffer.Length);
           objBinaryReader.Close();
           fs.Close();
           CreateMemoryShare();
Marshal.Copy(ImageBuffer, 0, pView, ImageBuffer.Length);


           Thread.Sleep(10000);
           ReleaseMemoryShare();



at the reader/receive side
C#
 CreateMemoryShare();
           
            byte[] ReceivedByte = new byte[100000000];
 Marshal.Copy(pView, ReceivedByte, 0, ReceivedByte.Length);

ReleaseMemoryShare();



Code is working fine but here the byte array ReceivedByte is fixed to 100000000 bytes but for some cases while communication the data will be just 1000 of any string data which will be of lesser size.For less data the length that I have given for ReceivedByte is too large.

So how to get the length of data that has been written in memory so that i can dynamically modify the ReceivedByte size.

Tried to get the length using
C#
Marshal.SizeOf(pView);

but it shows only 4 -16 which is not actual data.

Can any one guide or suggest how to get the size of data written inside the memory location.

Advance thanks.
Posted

1 solution

I think I would just create a simple protocol on what I am sending.
Reserving some bytes at the beginning of the memory to indicate the size of the data

|4 bytes | Rest for data|

you have to write the length to those 4 bytes
then write the data to the buffer + 4


int* pLength = (int*)buffer;
*pLength = 100;

char* sharedMem = ((char*)buffer) + 4;
Copy(sharedMem, sourceData, 100)

Then on the receiver side, you read those bytes first.
then read the rest when you have the actual length.

int* pLength = (int*)buffer;
char* sharedMem = ((char*)buffer) + 4;
Copy( ..., ..., *pLength);

That is one way to do it.
 
Share this answer
 
Comments
arunrv 19-Sep-13 4:53am    
Hi Mattias,
I am asking how to change the buffer size based on the data written inside memory location. Before converting the 4 byte to numbers need to read data know for that how do I create the buffer size.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900