Re: share a structure array containing multidimensional char array C#/



"Aykut Ergin" <AykutErgin@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:139D98E6-5572-4BB8-8FAD-E312F04F871A@xxxxxxxxxxxxxxxx
I want to share a structure array containing multi dimensional char array
between C# and C++, by using memory mapped file.

I already have C++ code for handling memory mapped file.
I am passing pointer to the structure array as shown below for C++;
I am able to work with pointer ( for C++ ).
C++ code works fine.

I am working on converting code for memory mapped file in C#.
My problems:
1. How should I define structure array in C# ( for the below structure array
).
2. How should I define pointer to structure array in C# ( for the below
structure array ).

Any help related to this would be highly appreciated.

C++ implementation:

struct msg_file_s
{
unsigned char mb_buffer[1000][1000];
.....
.....
};

struct msg_file_s msg_file[16];
struct msg_file_s *msg_file_ptr;


HANDLE api_map_memory(struct msg_file_s **msg_file_ptr)
{
HANDLE hKernel = CreateFileMapping ( (HANDLE) 0xFFFFFFFF,
NULL,

PAGE_READWRITE,
0,

sizeof( struct msg_file_s ),

_T("TEST"));

*msg_file_ptr = (struct msg_file_s *) MapViewOfFile( hKernel,

FILE_MAP_READ | FILE_MAP_WRITE,
0,
0,
0 );
return(hKernel);
}


map_memory_file(int index)
{
msg_file_ptr = &msg_file[index];
hKernel = api_map_memory(&msg_file_ptr);
}

/****************************************************************************/

C# implementation:

[StructLayout(LayoutKind.Sequential)]
unsafe struct msg_file_s
{
[MarshalAs(UnmanagedType.LPArray, ArraySubType= UnmanagedType.U1,
SizeConst = 100, SizeParamIndex=2)]
byte[,] mb_buffer;
}


--
Aykut ERGİN
Nortel Netaş
Software Design Engineer




Are you sure you want to pass such a huge amount of data through Shared memory (MM file)?
I see in your declaration that one element is already 1000000 bytes, how large are the other elements? and how large is the array of structures you want to share?

Note that passing data through Shared Memory requires a lot more memory because you'll have to marshal the data from unmanaged memory to managed memory before you will be able to access the managed presentation of the unmanaged data. Note that the extra marshaling will reduce the performance advantage of the shared memory "transfer" significantly, if not completely.

Willy.



.



Relevant Pages

  • Re: share a structure array containing multidimensional char array
    ... Note that when mapping a structure to shared memory, ... I need both read and write memory mapped file at both side of C# and C++. ... // use the array of structs... ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: share a structure array containing multidimensional char array
    ... The "writer" has to serialize the structure data and provide the size of the total number of bytes in the shared memory segment to the "reader", for instance as an int value at the start of the MM segment. ... // use the array of structs... ... > I already have C++ code for handling memory mapped file. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Windows XP Shared memory woes
    ... What is shared memory? ... sound like you're talking about a Memory Mapped file. ... You might try another box with more cache, perhaps one of the 16mb xeons.. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: share a structure array containing multidimensional char array
    ... This structure will execute on communication dll. ... I already have C++ code for handling memory mapped file. ... I am passing pointer to the structure array as shown below for C++; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: share a structure array containing multidimensional char array
    ... C++ code doesnot make any serialization. ... I thought that it is very hard to memory map structure array. ... // pBuf is the pointer that points to the shared memory buffer returned ... I am passing pointer to the structure array as shown below for C++; ...
    (microsoft.public.dotnet.languages.csharp)

Loading