Hi Willy,
Thank you very much for your work.
C++ code doesnot make any serialization.
So at runtime C# code gives an serialization error at
"msg_file_s[] sa = (msg_file_s[]) bf.Deserialize( ms);"
I thought that it is very hard to memory map structure array.
I need both read and write memory mapped file at both side of C# and C++.
I am looking other ways to share memory between C++ and C#.
Best Regards,
Aykut ERGİN
Nortel NetaÅŸ
Software Design Engineer
"Willy Denoyette [MVP]" wrote:
Thank you very much for your work.
C++ code doesnot make any serialization.
So at runtime C# code gives an serialization error at
"msg_file_s[] sa = (msg_file_s[]) bf.Deserialize( ms);"
I thought that it is very hard to memory map structure array.
I need both read and write memory mapped file at both side of C# and C++.
I am looking other ways to share memory between C++ and C#.
Best Regards,
Aykut ERGİN
Nortel NetaÅŸ
Software Design Engineer
"Willy Denoyette [MVP]" wrote:
Oh, but 256*256 is only 64.000 bytes!
>
You can declare your structure like this:
>
[Serializable]
[StructLayout(La youtKind.Sequen tial)]
public struct msg_file_s
{
public byte[,] mb_buffer;
public byte[] other;
}
>
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. Without this length
info there is no way to know the size and number of the individual
structures stored in the MM segment.
>
Then, the reader needs to deserialize the memory buffer as follows:
>
// pBuf is the pointer that points to the shared memory buffer returned
by the MapViewOfFile Win32 API.
>
static void GetObjectData2( IntPtr pBuf)
{
// get size of data bytes in MM segment
int len = Marshal.ReadInt 32(pBuf, 0);
// copy the MM data to a managed array
byte[] data = new byte[len];
Marshal.Copy(ne w IntPtr((int)pBu f + sizeof(int)), data, 0, len);
// deserialize the managed byte[] to the struct representation
BinaryFormatter bf = new BinaryFormatter ();
MemoryStream ms = new MemoryStream(da ta);
msg_file_s[] sa = (msg_file_s[]) bf.Deserialize( ms);
// use the array of structs...
foreach(msg_fil e_s s in sa)
{
// use structure data
int x = s.mb_buffer.Get UpperBound(0);
int y = s.mb_buffer.Get UpperBound(1);
...
}
>
}
}
>
>
Willy.
>
>
"Aykut Ergin" <AykutErgin@dis cussions.micros oft.comwrote in message
news:6EEA35B1-D8CF-499B-AA9D-40873B3C732E@mi crosoft.com...
>
>
>
>
You can declare your structure like this:
>
[Serializable]
[StructLayout(La youtKind.Sequen tial)]
public struct msg_file_s
{
public byte[,] mb_buffer;
public byte[] other;
}
>
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. Without this length
info there is no way to know the size and number of the individual
structures stored in the MM segment.
>
Then, the reader needs to deserialize the memory buffer as follows:
>
// pBuf is the pointer that points to the shared memory buffer returned
by the MapViewOfFile Win32 API.
>
static void GetObjectData2( IntPtr pBuf)
{
// get size of data bytes in MM segment
int len = Marshal.ReadInt 32(pBuf, 0);
// copy the MM data to a managed array
byte[] data = new byte[len];
Marshal.Copy(ne w IntPtr((int)pBu f + sizeof(int)), data, 0, len);
// deserialize the managed byte[] to the struct representation
BinaryFormatter bf = new BinaryFormatter ();
MemoryStream ms = new MemoryStream(da ta);
msg_file_s[] sa = (msg_file_s[]) bf.Deserialize( ms);
// use the array of structs...
foreach(msg_fil e_s s in sa)
{
// use structure data
int x = s.mb_buffer.Get UpperBound(0);
int y = s.mb_buffer.Get UpperBound(1);
...
}
>
}
}
>
>
Willy.
>
>
"Aykut Ergin" <AykutErgin@dis cussions.micros oft.comwrote in message
news:6EEA35B1-D8CF-499B-AA9D-40873B3C732E@mi crosoft.com...
Hi,
My original structure is like shown below
struct msg_file_s
{
unsigned char mb_buffer[256][256];
unsigned char other[64];
};
struct msg_file_s msg_file[16];
This structure will execute on communication dll ( written with C++ ).
Applications will use this dll.
We have not the chance to edit it because we use for several projects for
15
years.
We use this MMF with VC++ 5 / VC++ 6 / BCB 6 on NT4 / 2000 / 2003 / XP.
We do not have any problem.
Best Regards
--
Aykut ERGİN
Nortel NetaÅŸ
Software Design Engineer
"Willy Denoyette [MVP]" wrote:
My original structure is like shown below
struct msg_file_s
{
unsigned char mb_buffer[256][256];
unsigned char other[64];
};
struct msg_file_s msg_file[16];
This structure will execute on communication dll ( written with C++ ).
Applications will use this dll.
We have not the chance to edit it because we use for several projects for
15
years.
We use this MMF with VC++ 5 / VC++ 6 / BCB 6 on NT4 / 2000 / 2003 / XP.
We do not have any problem.
Best Regards
--
Aykut ERGİN
Nortel NetaÅŸ
Software Design Engineer
"Willy Denoyette [MVP]" wrote:
"Aykut Ergin" <AykutErgin@dis cussions.micros oft.comwrote in message
news:139D98E6-5572-4BB8-8FAD-E312F04F871A@mi crosoft.com...
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 = CreateFileMappi ng ( (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_pt r);
}
/*************** *************** *************** *************** *************** */
C# implementation:
[StructLayout(La youtKind.Sequen tial)]
unsafe struct msg_file_s
{
[MarshalAs(Unman agedType.LPArra y, ArraySubType= UnmanagedType.U 1,
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.
>
>
>
>
news:139D98E6-5572-4BB8-8FAD-E312F04F871A@mi crosoft.com...
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 = CreateFileMappi ng ( (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_pt r);
}
/*************** *************** *************** *************** *************** */
C# implementation:
[StructLayout(La youtKind.Sequen tial)]
unsafe struct msg_file_s
{
[MarshalAs(Unman agedType.LPArra y, ArraySubType= UnmanagedType.U 1,
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.
>
>
>
>
>
>
Comment